[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

cannot get expect to work with perl script

Hello,

i have a script that works with no problem on another linux box (Redhat 4.1)
its a perl script, also running Expect.

I installed tcl 8.5.9 & expect5.45 and tested them individually with no problem and they both work.

when I run the script on a mirrored linux box from the one thats working, I get this error:

 
Can't locate Expect.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./master.pl line 3.
BEGIN failed--compilation aborted at ./master.pl line 3.

Open in new window


here is the script I am trying to get working:

 
#!/usr/bin/perl

use Expect;

print "what are the first 3 octets?";
chomp ($f3oct = <>);
print "what is your username?";
chomp ($Uname = <>);
print "what is your password?";
chomp ($Upass = <>);
print "Running $f3oct.0 network\n";

$username = "$Uname";
$password = "$Upass";

$timeout = 4;

my $used_ips;

# Insert commands desired here.
@commands =
(
#'commands that write to switches',
#'commands that write to switches',
'end',
#'wr mem',
'exit',

);

$subnet ="$f3oct",

my @lastoct = (2 .. 254);
while(scalar(@lastoct) > 0)
{
my $i = shift(@lastoct);

@addresses = (
"$i",

);

foreach (@addresses)
        {
                $hostname = shift;
                $sshcommand = shift;
                $hostname = "$subnet.$_";
                $sshcommand = "ssh -o ConnectTimeout=5 -l $username $hostname";
                print("$sshcommand\n");
                switchupdate();
        }

#foreach (@addresses)
#       {
#               my $hostname = shift;
#               $hostname = "192.168.3.$_";
#               switchupdate($hostname);
#       }


sub switchupdate {

        my $switch = Expect->spawn($sshcommand) or die "Cannot spawn $sshcommand: $!\n";

        $switch->expect(3,
                [ qr/yes/i,
                sub {
                        my $cmd = shift;
                        $cmd->send("yes\n");
                }
                ],
        );

        $switch->expect(3,
                [ qr/assword:/i,
                sub {
                        my $cmd = shift;
                        $cmd->send("$password\n");
                }
                ],
        );

        $switch->expect(3,
                [ qr/#/i,
                sub {
                        my $cmd = shift;
                        $cmd->send("conf t\n");
                }
                ],
        );

        $switch->expect(30,
                [ qr/#/i,
                sub {
                        my $cmd = shift;
                        foreach (@commands)
                                {
                                        $cmd->send("$_\n");
                                }
                }],
        );

        $switch->expect(3,
                [ qr/#/i,
                sub {
                        my $cmd = shift;
                        $cmd->send("exit\n");
                }
                ],
        );


        $switch->expect(3,
                [ qr/#/i,
                sub {
                        my $cmd = shift;
                        $cmd->send("wri mem\n");
                }
                ],
        );

        $switch->expect(3,
                [ qr/#/i,
                sub {
                        my $cmd = shift;
                        $cmd->send("exit\n");
                }
                ],
        );

        $switch->soft_close();
}
}

Open in new window


any help would be greatly appreciated
0
icewiper
Asked:
icewiper
  • 3
1 Solution
 
farzanjCommented:
You are missing Perl module called Expect.pm.  You can download it from CPAN.

What kind of Linux do you have?

Here is the link of the Perl module http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod

Issue command

cpan

to see if you have cpan package to be able to download this package.  Follow instructions in this link to get this package.
http://www.thegeekstuff.com/2008/09/how-to-install-perl-modules-manually-and-using-cpan-command/
0
 
farzanjCommented:
I would prefer installing Perl using CPAN.  So take a look at the command given in the above link
0
 
icewiperAuthor Commented:
farzanj,

thanks so much for the help and responding back so fast.

right after I posted the question, I did find that i needed to download the Expect.pm.
I downloaded Expect-1.20 and kept running into more .pm's that were needed.

I downloaded the latest Expect (the link you send me) and it works perfect now.

again thanks for help!!!


0
 
farzanjCommented:
I am so glad to hear that.  You are most welcome.  Looking forward to be assisting your in the future!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now