Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Expect Script with Perl

Status
Not open for further replies.
Oct 27, 2002
21
0
0
US
I am trying to use a Perl Script to log in to a Netscreen Firewall. Since the Firewall requires interactive mode only, I need to use an Expect script or Net::SSH:Expect I think. I have no idea how to feed the expect script information into the perl script. Have looked on alot of sites that explain expect and have hit brick walls. The expect script is at the bottom of the script below. I want to log in to the device and run a command then log into the next device and run the same command and have the perl script display the results. The script runs but never does anything with the expect portion and I am assuming it is because I have used the wrong syntax or just scripted in wrong entirely. I would greatly appreciate any assistance. Thanks in Advance

Wally Steadman
SCRIPT BELOW:
++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/usr/bin/perl -w

require "cgi-lib.pl";
use Date::Calc qw:)all);
use Text::Wrap;
use CGI::pretty qw/:standard :cgi-lib/;
use Getopt::Long;
use CGI::Carp qw(fatalsToBrowser);
use expect;

@fwlog='firewall1a,firewall1b';

&ReadParse(*input);
print header();
print start_html(-title=>'Firewall BGP Checks',
-author=>'me@my.com',
-style=>{'src'=>'/netcheck/css/default.css'},
-id=>'form',
);


print end_html;
print "<pre>";


$username=$input{'username'};
$password=$input{'password'};
$firewall=$input{'firewall'};


GetOptions(
"username=s" =>\$username,
"password=s" => \$password,
"firewall=s" => \$firewall,
);

@fwlog = split(/,/,$fwlog);

if ($firewall =~ m/myloc10myslot15/)
{
$fwid = "65001";
print "$firewall \n";
print "ID IS $fwid \n";
}
elsif ($firewall =~ m/myloc13myslot31/)
{
$fwid = "65002";
print "$firewall \n";
print "ID IS $fwid \n";
}
elsif ($firewall =~ m/myloc10myslot32/)
{
$fwid = "65003";
print "$firewall \n";
print "ID IS $fwid \n";
}
elsif ($firewall =~ m/myloc5myslot7/)
{
$fwid = "65004";
print "$firewall \n";
print "ID IS $fwid \n";
}
elsif ($firewall =~ m/myloc13myslot26/)
{
$fwid = "65005";
print "$firewall \n";
print "ID IS $fwid \n";
}
elsif ($firewall =~ m/myloc13myslot26/)
{
$fwid = "65006";
print "$firewall \n";
print "ID IS $fwid \n";
}
else
{
print "Firewall not listed";
}

### THIS IS THE PART I AM LOST ON ###
foreach $fwlog (@fwlog)
{
my $ssh = Net::SSH::Expect->new (
host => "$firewall",
password=> '$password',
user => '$username',
raw_pty => 1);
$ssh->run_ssh() or die "SSH process couldn't start: $!";
($ssh->read_all(2) =~ />\s*\z/) or die "where's the remote prompt?"
my $command = $ssh->exec("get vr CC-vr protocol bgp rib-in | include $fwid");
print ($command);
}

++++++++++++++++++++++++++++++++++++++++
SCRIPT END
 
as far as I know there is no expect module, the module is named Expect. The uppercase E is important on most operating systems. Maybe the incorrect use of quotes is also a problem:

Code:
user => '$username',

remove the single-quotes from the above line.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,
Thanks for the tips. I had already fixed the uppercase E but didn't change the ' '

I notice you show only the username as being in the wrong quotes, and then I looked at the other inputs and one has "$firewall" is in double quotes and 'password' is in single quotes. because they are variables, do any of them need to be in quotes? Again, thanks alot for pointing those things out.

Wally

 
Kevin,
Just one more question of curiosity. Does the ADC in your name represent where you work? Such as a Data Center in some place like Austin?

 
There is actually almost never a need to put any quotes around a single scalar, but single-quotes treats the '$' literally so there is no variable interpolation. I missed '$password'. Those lines are best written like so:

Code:
my $ssh = Net::SSH::Expect->new (
host => $firewall,
password=> $password,
user => $username,
raw_pty => 1);

99.9999% of the time the correct thing to do with a single scalar is to not quote it.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Notice he slyly bypasses the ADC question.. :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
ADC means nothing to anyone but me. Its the initials of my youngest three children.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Cool deal. Just curious as I work in a place called ADC so figured I would ask :)

Thanks for your tips

Wally
 
That's cool Kevin. [2thumbsup]

I have an uncle that does QA for ADC.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top