Guest User

Untitled

a guest
Jun 20th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #!/usr/bin/perl
  2. # pssh
  3. # -- "persistent SSH" wrapper for autossh
  4. #
  5. # Copyright 2010 Greg Boyington. All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without modification, are
  8. # permitted provided that the following conditions are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright notice, this list of
  11. # conditions and the following disclaimer.
  12. #
  13. # 2. Redistributions in binary form must reproduce the above copyright notice, this list
  14. # of conditions and the following disclaimer in the documentation and/or other materials
  15. # provided with the distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY GREG BOYINGTON ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
  20. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. #
  27. # The views and conclusions contained in the software and documentation are those of the
  28. # authors and should not be interpreted as representing official policies, either expressed
  29. # or implied, of Greg Boyington.
  30. #
  31.  
  32. use strict;
  33. use IO::Socket::INET;
  34.  
  35. my $host = shift @ARGV or die "usage: $0 [user@]hostname [ SCREEN_LABEL ]\n";
  36.  
  37. my $autossh_path = 'autossh';
  38.  
  39. # the port range we should examine
  40. my @port_range = 20000 .. 30000;
  41.  
  42. # the IP we should try to bind to when checking for unused ports
  43. my $bind_ip = '127.0.0.1';
  44.  
  45. # map to corresponding AUTOSSH_* environment variables
  46. my %config = (
  47. poll => undef,
  48. gatetime => undef,
  49. logfile => undef,
  50. debug => undef,
  51. path => undef,
  52. );
  53. my ( $bind, $listen, $open );
  54. while ( @port_range ) {
  55. $open=0;
  56. $bind = shift @port_range;
  57. $listen = shift @port_range or last;
  58. for ( $bind, $listen ) {
  59. my $socket = IO::Socket::INET->new(
  60. PeerAddr => $bind_ip,
  61. PeerPort => $_,
  62. Proto => 'TCP',
  63. Timeout => 3
  64. );
  65. $open++ unless defined $socket;
  66. $socket=undef;
  67. }
  68. last if $open==2;
  69. }
  70. die "No available ports detected.\n" unless $open;
  71.  
  72. # use environment variables to configure autossh
  73. $ENV{ 'AUTOSSH_'.$_ } = $config{ $_} foreach grep { defined $config{$_} } keys %config;
  74.  
  75. # use a unique label for this screen session
  76. my $label = shift @ARGV || getpwuid( $> ) . $bind;
  77.  
  78. # invoke autossh
  79. my $cmd = sprintf( q( %s -M %d -t %s "screen -e^Zz -D -R -S %s"), $autossh_path, $bind, $host, $label );
  80. exec $cmd;
  81.  
  82. =head1 NAME
  83.  
  84. pssh - create a 'persistent' ssh connection with autossh and screen
  85.  
  86. =head1 SYNOPSIS
  87.  
  88. pssh user@hostname
  89.  
  90. =head1 DESCRIPTION
  91.  
  92. pssh is a small script that manages autossh and screen for "persistent" SSH sessions. It searches a
  93. given port range looking for two consecutive available ports, and passes them to autossh. autossh
  94. takes care of monitoring the SSH connection and reconnecting to the remote screen session as needed.
  95.  
  96. Screen sessions are labeled (via -S) with the effective username and the local port being used, so
  97. one can correlate existing screen sessions on the remote host with the local autossh processes. You
  98. can force screen to reattach to an existing session by specifying the label when calling pssh:
  99.  
  100. pssh user@hostname joe20004
  101.  
  102. =head1 SEE ALSO
  103.  
  104. autossh, ssh
  105.  
  106. =head1 AUTHOR
  107.  
  108. pssh was written by Greg Boyington <greg@automagick.us>, inspired by the 'rscreen' script that ships
  109. with autossh.
  110.  
  111. =head1 COPYRIGHT NOTICE
  112.  
  113. Copyright 2010 Greg Boyington. All rights reserved.
  114.  
  115. pssh is distributed under the Simplified BSD License; see file header for details.
  116.  
  117. =cut
Add Comment
Please, Sign In to add comment