slayedbylucifer has asked for the wisdom of the Perl Monks concerning the following question:

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:

#!/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; }] )
for the Expect syntax, I have been referring the "perldoc Expect" please excuse my English. Many thanks.

Replies are listed 'Best First'.
Re: working with Expect module
by roboticus (Chancellor) on Dec 27, 2010 at 18:44 UTC

    slayedbylucifer:

    Change this:

    my @parameters = ('$_', 'uptime');

    to this:

    my @parameters = ($_, 'uptime');

    Your version is using THE NAME of variable $_ as an IP address, rather than THE CONTENTS. Single (') and double (") quotes are different, so if you used:

    my @parameters = ("$_", 'uptime');

    your code would have worked, because inside double quotes, the variables contents would replace its name in the string. However, just putting quotes around a variable name is discouraged.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Hi roboticus, thanks you very much. it's working now. BTW, i have posted a follow-up (but different) question @ http://www.perlmonks.org/?node_id=879388 Would really appreciate your help. thanks again.

        slayedbylucifer:

        It looks like ahmad may have already found it for you. By the way, when you know the node ID of a node, it's easy to link to it like this: [id://879388] gives you: Expect module and running "su". If you want to give the link a different name, just put a vertical bar and the new name before the closing bracket, so [id://879388|the other question] gives you a link to the other question.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.