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

I need to connect to a remote host and run a command. I have the following script (Which is not complete) and I am failing somewhere. I am receiving the following error when I run the script:
expect(): not enough arguments, should be expect(timeout, patterns...) at login_audit line 24
I pretty sure I am overlooking something so any help would be great.
#!/usr/bin/perl -w use Expect; use strict; #list of Global Variables my $password = "password"; my $timeout = 10; #Array for server list my @ServerList; #Server list import my $ServerListFile = "serverlist.txt"; open my $handle, '<', $ServerListFile or die "Error: Unable to open fi +le for processing."; chomp(@ServerList = <$handle>); close $handle or die "Error: Unable to close file for Server Import."; #End Server list import foreach (@ServerList) { my $spawn = expect->spawn("ssh $_") or die "Couldn't open ssh session to $_, $!\n"; my $ret = $spawn->expect($timeout, [qr/Password:/ => sub { $spawn->send("$password\n"); +exp_continue; } ], [qr/3par/ => sub { $spawn->send("showsys\n"); } +], ); }


I can't install any modules on this box so Net::SSH is out of the question.

Replies are listed 'Best First'.
Re: Expect.pm Help with Automating SSH
by hippo (Archbishop) on Apr 14, 2016 at 08:36 UTC
     my $spawn = expect->spawn("ssh $_")

    You have a typo in the above which the docs say should be

    my $spawn = Expect->spawn("ssh $_")

    If you hadn't imported the expect sub then strict would have picked this up. ie:

    $ cat et.pl #!/usr/bin/env perl use strict; use warnings; use Expect (); my $spawn = expect->spawn("foo $_"); $ perl et.pl Use of uninitialized value $_ in concatenation (.) or string at et.pl +line 6. Can't locate object method "spawn" via package "expect" (perhaps you f +orgot to load "expect"?) at et.pl line 6.
Re: Expect.pm Help with Automating SSH
by cavac (Prior) on Apr 14, 2016 at 06:20 UTC

    I can't install any modules on this box so Net::SSH is out of the question.

    That in itself is not necessarily true. You can just make a directory with all the required files (for example a copy of your development box perl library), and use something like this:

    BEGIN { unshift @INC, "/home/username/src/sitelib/"; } use Net::SSH;

    That said, if you can't install the correct software to do your work, your company policy and/or server setup is incorrect in my opinion.

    "For me, programming in Perl is like my cooking. The result may not always taste nice, but it's quick, painless and it get's food on the table."