need expect and send script

nicky s
nicky s used Ask the Experts™
on
hi


need to a script to copy a file from one server to a bunch of server

will user ip, password and list of each servers in a file.

i don`t want to implement this by exchanging keys

input user/password/file containing list of srvers

for each server in <server-list-file>
ssh user@server
password
sudo su - xyx
cd /app/abc
scp user@source:/sss/copyfile.txt .
end for loop
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You could use expect for this purpose (either native tool or just a module for Perl).

For Perl the code will look something like the following:
#!/usr/bin/perl -w
use Expect;


open SRVLST, "<myserverlist.txt" ;

while ($server=<SRVLST>) {

my $exp = new Expect;

my $command = 'ssh user@$server';

$exp->spawn($command) or die "Cannot spawn $command: $!\n";

my $patidx = $exp->expect(30, 'Password:');
$exp->send("password\n");

$patidx = $exp->expect(30, '#');
$exp->send("sudo su - xyx\n");

$patidx = $exp->expect(30, '#');
$exp->send("cd /app/abc\n");

$patidx = $exp->expect(30, '#');
$exp->send("scp user@source:/sss/copyfile.txt .\n");

$patidx = $exp->expect(30, '#');
$exp->send("exit\n");

}

close SRVLST;

Open in new window


This is just a draft, it was not started on the real system, so it requires some polishing.

Here is supposed, that the command line prompt is "#" on every server - You could change it to whatever You like.
Commented:
Minor corrections to the above solution:
1) You cannot interpolate variables within single quotes, so a better way for line 11 is:
my $command = "ssh user\@$server";

Open in new window

2) You need to escape the @ sign within double quotes in line 25:
$exp->send("scp user\@source:/sss/copyfile.txt .\n");

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial