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.

In reply to working with Expect module by slayedbylucifer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.