Hi, I am new to Perl so I am not sure whether this is stupid question, I will ask it anyway... I have about 100 Linux VMs on which My task is to find the uptime on all the servers. I am using expect module. My expect command works fine when I am logging to just one server. However, I have got a list of ip addresses saved in a file and want my expect script to loop through it . This is where it is failing. Here is my Perl code:
#!/usr/bin/perl #use strict; use warnings; use Expect; open FILE, "ip_address"; #while (<FILE>) my @ip_array = <FILE>; foreach (@ip_array) { chomp ($_); my $command = "ssh"; my @parameters = ('$_', 'uptime'); my $exp = Expect->spawn($command, @parameters) or die "Cannot spawn $command: $!\n"; $exp->expect( [ qr/yes/ => sub { my $exp = shift; $exp->send("yes\n"); exp_continue; }], [qr/assword/i => sub { my $exp = shift; $exp->send("1234\n"); exp_continue; }] ) }
WHERE: ip_address: is the list of my ip addresses 1234: password for the user
and the output I get is:
ssh: Could not resolve hostname $_: Name or service not known ssh: Could not resolve hostname $_: Name or service not known
from what I understand, the expect module takes two arguments: $command and @parameters. I am missing something in the while loop while passing the individual ip address values to the Expect object. I know my below code works fine when I explicitly provide the ip address of the server I want to login. So I need help on figuring out while loop in above code. Here is the code which works fine:
for the Expect syntax, I have been referring the "perldoc Expect" please excuse my English. Many thanks.#!/usr/bin/perl #use strict; use warnings; use Expect; my $command = "ssh"; my @parameters = ('10.10.10.10', 'uptime'); my $exp = Expect->spawn($command, @parameters) or die "Cannot spawn $command: $!\n"; $exp->expect( [ qr/yes/ => sub { my $exp = shift; $exp->send("yes\n"); exp_continue; }], [qr/assword/i => sub { my $exp = shift; $exp->send("1234\n"); exp_continue; }] )
In reply to working with Expect module by slayedbylucifer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |