teuk

ssh_askpass_multi.pl

Jun 9th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.54 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # Execute a shell script on remote servers via ssh based on a password file
  4.  
  5. # Password file contains these fields (tab separated)
  6.  
  7. # <hostname1> <user1> <password1>
  8. # <hostname2> <user2> <password2>
  9. # ...
  10. # <hostnamen> <usern> <passwordn>
  11.  
  12. #
  13.  
  14. # +---------------#
  15. # |    HEADER     #
  16. # +---------------#
  17.  
  18. use strict;
  19.  
  20. sub clean_and_exit(@);
  21.  
  22. # +---------------------------#
  23. # |    CHECK COMMAND LINE     #
  24. # +---------------------------#
  25.  
  26. if ( $#ARGV != 1 ) {
  27.     print "Usage : ssh_askpass_multi.pl <servers password file> <script>\n";
  28.     clean_and_exit(1);
  29. }
  30.  
  31. my $PASSWORDFILE=$ARGV[0];
  32. my $SCRIPTFILE=$ARGV[1];
  33.  
  34. unless ( -r $PASSWORDFILE ) {
  35.     print "Cannot access $PASSWORDFILE\n";
  36.     print "Usage : ssh_askpass_multi.pl <servers password file> <script>\n";
  37.     clean_and_exit(2);
  38. }
  39.  
  40. unless ( -r $SCRIPTFILE ) {
  41.     print "Cannot access $SCRIPTFILE\n";
  42.     print "Usage : ssh_askpass_multi.pl <servers password file> <script>\n";
  43.     clean_and_exit(3);
  44. }
  45.  
  46. unless (open PASSWDFILE,"$PASSWORDFILE") {
  47.     print "Could not open $PASSWORDFILE\n";
  48.     clean_and_exit(4);
  49. }
  50.  
  51. # +-------------#
  52. # |    MAIN     #
  53. # +-------------#
  54. my $line;
  55.  
  56. while(defined($line=<PASSWDFILE>)) {
  57.     chomp($line);
  58.     my ($serverhost,$user,$password) = split(/\t/,$line);
  59.    
  60.     print "------ $serverhost ------\n";
  61.    
  62.     unless ( open TMPECHOPASS, ">tmpechopass.sh" ) {
  63.         print "Could not create tmpechopass.sh\n";
  64.         clean_and_exit(5);
  65.     }
  66.     print TMPECHOPASS '#!/bin/bash' . "\n";
  67.     print TMPECHOPASS "echo $password\n";
  68.     close TMPECHOPASS;
  69.     chmod 0700, "tmpechopass.sh";
  70.    
  71.     unless ( open SSHCONN, ">sshcon.sh" ) {
  72.         print "Could not create sshcon.sh\n";
  73.         clean_and_exit(6);
  74.     }
  75.     print SSHCONN '#!/bin/bash' . "\n";
  76.     print SSHCONN "export SSH_ASKPASS=./tmpechopass.sh\n";
  77.     print SSHCONN "export DISPLAY=y\n";
  78.     print SSHCONN "./setsid.pl ssh $user\@$serverhost \"bash -s\" < $SCRIPTFILE\n";
  79.     close SSHCONN;
  80.     chmod 0700, "sshcon.sh";
  81.     system("./sshcon.sh");
  82. }
  83.  
  84. close PASSWDFILE;
  85.  
  86. clean_and_exit(0);
  87.  
  88. # +-------------#
  89. # |    SUBS     #
  90. # +-------------#
  91. sub clean_and_exit(@) {
  92.     my ($exit_code) = @_;
  93.     if ( -f "tmpechopass.sh") { unlink "tmpechopass.sh"; }
  94.     if ( -f "sshcon.sh") { unlink "sshcon.sh"; }
  95.     if (defined($PASSWORDFILE)) {
  96.         print "\nDo not forget to erase $PASSWORDFILE when your operations are finished !\n";
  97.         print "File containing unencrypted passwords must not stay on filesystems.\n";
  98.     }
  99.     exit $exit_code;
  100. }
  101.  
  102. # setsid.pl source
  103. ##!/usr/bin/perl
  104. #use POSIX(setsid);
  105. #if(fork()) { wait; } else { setsid; exec {$ARGV[0]} @ARGV; }
Add Comment
Please, Sign In to add comment