nicky s
asked on
SSH Script Needed
script needed
Urgent:
I need a script to ssh to different severs with same UID and PWD and execute same set of commands across different servers..
Note : i'm able to ssh among these servers ..
Need a script such that it take 3 inputs ( uid, passwd,serverslist file) and ssh the servers in a server list file and execute a set of commands
Urgent:
I need a script to ssh to different severs with same UID and PWD and execute same set of commands across different servers..
Note : i'm able to ssh among these servers ..
Need a script such that it take 3 inputs ( uid, passwd,serverslist file) and ssh the servers in a server list file and execute a set of commands
Passing plain text password on the command line is unsafe and I doubt ssh supports it. Instead you should go for private/public key authentication using agent forwarding.
Put the commands you want to execute on each server into a file say, cmds.sh, and copy that file first to each server using scp. Then ssh to the server, chmod +x cmds.sh and run the file. Assuming you want to copy the file to your HOME/bin directory, copy the script below as execssh.pl and run as execssh.pl <serverlist.txt:
#!/usr/bin/perl
system ("scp cmds.sh server:~/bin");
system ("ssh <userid>@$server chmod +x ~/bin/cmds.sh;~/bin/cmds.s h") while $server <>;
Put the commands you want to execute on each server into a file say, cmds.sh, and copy that file first to each server using scp. Then ssh to the server, chmod +x cmds.sh and run the file. Assuming you want to copy the file to your HOME/bin directory, copy the script below as execssh.pl and run as execssh.pl <serverlist.txt:
#!/usr/bin/perl
system ("scp cmds.sh server:~/bin");
system ("ssh <userid>@$server chmod +x ~/bin/cmds.sh;~/bin/cmds.s
Dear,
Something like this should do the work :
my $ssh = Net::SSH::Perl->new("host1 ");
$ssh->login("user1", "pass1");
$ssh->cmd("foo");
Best Regards
Something like this should do the work :
my $ssh = Net::SSH::Perl->new("host1
$ssh->login("user1", "pass1");
$ssh->cmd("foo");
Best Regards
ASKER
woolmilkporc:
i want to use expect ?
Can you help me with the script for that ?
i want to use expect ?
Can you help me with the script for that ?
ASKER
MadShiva:
Getting the following errors following your procedure
Can't locate object method "new" via package "Net::SSH::Perl" (perhaps you forgot to load "Net::SSH::Perl"?) at ./sample.sh line 2.
Getting the following errors following your procedure
Can't locate object method "new" via package "Net::SSH::Perl" (perhaps you forgot to load "Net::SSH::Perl"?) at ./sample.sh line 2.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
MadShiva:
hey i did not try this script but a question .. I need the serers to be read from a file .. i want to put all the servers in a file and the uid and pwd for all these servers are same .... instead of giving the hostname everytime....
hey i did not try this script but a question .. I need the serers to be read from a file .. i want to put all the servers in a file and the uid and pwd for all these servers are same .... instead of giving the hostname everytime....
Hi,
I've altered MAdShiva's code for you a little so that it reads in from a file.
I've altered MAdShiva's code for you a little so that it reads in from a file.
#! /usr/bin/perl -w
use strict;
use Net::SSH::Perl;
use Net::SSH::Perl::Auth;
use Net::SSH::Perl;
if ($#ARGV != 2) {
print "usage: perl $0 user password /path/listofservers.txt\n";
exit;
}
$user = $ARGV[0];
$pass = $ARGV[1];
$inputfile = $ARGV[2];
open(INFILE, "$inputfile") || die "Error opening input $inputfile: $!";
while (<INFILE>){
chomp;
my $host=$_;
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
print "$host:\n";
print "$stdout\n";
$ssh=undef;
}
close(INFILE);
ASKER
APNFSSC:
can you explain me .. how it works ?
so that i can try execute it tomorrow
can you explain me .. how it works ?
so that i can try execute it tomorrow
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if you copy and paste the code into a script called remote_execute.pl
then execute and alter the values of username/password/fileloca tion
perl remote_execute.pl username password /path/to/listofhosts.txt
You will also need to ensure you have perl and the Net::SSH::Perl module installed on the server you are executing the script on.
Depending on your linux distribution installing Net::SSH::Perl can either be easy or hard.
then execute and alter the values of username/password/fileloca
perl remote_execute.pl username password /path/to/listofhosts.txt
You will also need to ensure you have perl and the Net::SSH::Perl module installed on the server you are executing the script on.
Depending on your linux distribution installing Net::SSH::Perl can either be easy or hard.
ASKER
i dont have this intsalled in unix Net::SSH::Perl
but i want to close this as it has been opened for so many days
but i want to close this as it has been opened for so many days
ssh will never read passwords from stdin or from a file, nor from the command line.
You will either have to establish public key authentication or use something like expect.
wmp