Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # Execute a shell script on remote servers via ssh based on a password file
- # Password file contains these fields (tab separated)
- # <hostname1> <user1> <password1>
- # <hostname2> <user2> <password2>
- # ...
- # <hostnamen> <usern> <passwordn>
- #
- # +---------------#
- # | HEADER #
- # +---------------#
- use strict;
- sub clean_and_exit(@);
- # +---------------------------#
- # | CHECK COMMAND LINE #
- # +---------------------------#
- if ( $#ARGV != 1 ) {
- print "Usage : ssh_askpass_multi.pl <servers password file> <script>\n";
- clean_and_exit(1);
- }
- my $PASSWORDFILE=$ARGV[0];
- my $SCRIPTFILE=$ARGV[1];
- unless ( -r $PASSWORDFILE ) {
- print "Cannot access $PASSWORDFILE\n";
- print "Usage : ssh_askpass_multi.pl <servers password file> <script>\n";
- clean_and_exit(2);
- }
- unless ( -r $SCRIPTFILE ) {
- print "Cannot access $SCRIPTFILE\n";
- print "Usage : ssh_askpass_multi.pl <servers password file> <script>\n";
- clean_and_exit(3);
- }
- unless (open PASSWDFILE,"$PASSWORDFILE") {
- print "Could not open $PASSWORDFILE\n";
- clean_and_exit(4);
- }
- # +-------------#
- # | MAIN #
- # +-------------#
- my $line;
- while(defined($line=<PASSWDFILE>)) {
- chomp($line);
- my ($serverhost,$user,$password) = split(/\t/,$line);
- print "------ $serverhost ------\n";
- unless ( open TMPECHOPASS, ">tmpechopass.sh" ) {
- print "Could not create tmpechopass.sh\n";
- clean_and_exit(5);
- }
- print TMPECHOPASS '#!/bin/bash' . "\n";
- print TMPECHOPASS "echo $password\n";
- close TMPECHOPASS;
- chmod 0700, "tmpechopass.sh";
- unless ( open SSHCONN, ">sshcon.sh" ) {
- print "Could not create sshcon.sh\n";
- clean_and_exit(6);
- }
- print SSHCONN '#!/bin/bash' . "\n";
- print SSHCONN "export SSH_ASKPASS=./tmpechopass.sh\n";
- print SSHCONN "export DISPLAY=y\n";
- print SSHCONN "./setsid.pl ssh $user\@$serverhost \"bash -s\" < $SCRIPTFILE\n";
- close SSHCONN;
- chmod 0700, "sshcon.sh";
- system("./sshcon.sh");
- }
- close PASSWDFILE;
- clean_and_exit(0);
- # +-------------#
- # | SUBS #
- # +-------------#
- sub clean_and_exit(@) {
- my ($exit_code) = @_;
- if ( -f "tmpechopass.sh") { unlink "tmpechopass.sh"; }
- if ( -f "sshcon.sh") { unlink "sshcon.sh"; }
- if (defined($PASSWORDFILE)) {
- print "\nDo not forget to erase $PASSWORDFILE when your operations are finished !\n";
- print "File containing unencrypted passwords must not stay on filesystems.\n";
- }
- exit $exit_code;
- }
- # setsid.pl source
- ##!/usr/bin/perl
- #use POSIX(setsid);
- #if(fork()) { wait; } else { setsid; exec {$ARGV[0]} @ARGV; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement