Link to home
Start Free TrialLog in
Avatar of nicky s
nicky sFlag for United States of America

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

 
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Hi,
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
 
Avatar of Justin Mathews
Justin Mathews

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.sh") while $server <>;



Dear,

Something like this should do the work :

my $ssh = Net::SSH::Perl->new("host1");
    $ssh->login("user1", "pass1");

    $ssh->cmd("foo");

Best Regards
Avatar of nicky s

ASKER

woolmilkporc:
i want to use expect ?
Can you help me with the script for that ?
Avatar of nicky s

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.
 
ASKER CERTIFIED SOLUTION
Avatar of Tobias
Tobias
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nicky s

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....
Hi,

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);

Open in new window

Avatar of nicky s

ASKER

APNFSSC:

can you explain me .. how it works ?

so that i can try execute it tomorrow
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you copy and paste the code into a script called remote_execute.pl

then execute and alter the values of username/password/filelocation

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.

Avatar of nicky s

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