CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Help making a module

 



MysticRyuujin
New User

Mar 8, 2014, 12:48 AM

Post #1 of 3 (1523 views)
Help making a module Can't Post

Hello - Please be genital with me, I am very new to Perl... I am trying to make a module for various codes that we use over and over with Cisco devices. I have code for logging into a Cisco device that I want to put into the module but I'm having trouble. Basically I want to be able to do something like:


Code
use lib '/scripts/modules/'; 
use cisco;

cisco::login($UNAME, $PWD, $ENABLE, $IP);


and have it call a module that consists of:


Code
use Expect; 
my $exp = new Expect;

package cisco;

sub login{

( $UNAME, $PWD, $ENABLE, $IP ) = @ARGV;

my $command = "ssh $UNAME" . "@" . "$IP";
$exp->spawn($command) or die "Cannot spawn $command: $!\n";

my $connect = $exp->expect
(
30,
[ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes\n"); exp_continue; } ],
[ qr/assword:\s*$/ => sub { $exp->send("$PWD\n"); } ],
);
$result = $exp->expect(30, "#", ">");
if ($result == 2)
{
$exp->send("enable\n");
$result = $exp->expect(30, "assword:", "#");
if ($result == 1)
{
$exp->send("$ENABLE\n");
$result = $exp->expect(30, "assword:", "#");
if ($result == 1)
{
print LOG "Enable Password Rejected\n";
exit();
}
}
}
$exp->send("term len 0\n");
$exp->expect(10,"#");
}


My biggest problem seems to be that as soon as the login subroutine is done the connection is done. When it returns to the Perl script after running login(); it's like it's not connected.

Any help would be appreciated, and if anyone has any suggestions for cleaner login code that would be appreciated as well.

It is the way that it is now because a few of our devices require us to type Enable to get into privileged mode while a few others require us to also enter an enable password. I'm trying to cover all three cases with the one code.


(This post was edited by MysticRyuujin on Mar 8, 2014, 7:22 PM)


Laurent_R
Veteran / Moderator

Mar 8, 2014, 3:15 PM

Post #2 of 3 (1431 views)
Re: [MysticRyuujin] Help making a module [In reply to] Can't Post


In Reply To
Hello - Please be genital with me, I am very new to Perl...


I am ,really sorry, but I do not wish to be genital with you.

Wink


MysticRyuujin
New User

Mar 9, 2014, 6:08 PM

Post #3 of 3 (1184 views)
Re: [Laurent_R] Help making a module [In reply to] Can't Post

Ok so here's the actual code I'm working with right now. I was trying to follow the guide over at perlmonks but it's still the same basic issue. The connection doesn't seem to pass to the main script.

Script:

Code
#!/usr/bin/perl -w 

use lib '/enm01/custom/scripts/modules';
use Expect;
use cisco qw(&login_auth);;

my $exp = new Expect;

# Parces the BASH arugments into Perl variables
( $UNAME, $PWD, $ENABLE, $DNAME, $IP, $LOG ) = @ARGV;

# This logs in and authenticates to the device
login_auth(@ARGV);

# This checks to make sure the hostname is correct
namecheck();

sub namecheck
{
if ( $DNAME !~ /U....WX[0-9][0-9]/ )
{
$exp->send("show run | include hostname\n");
@hostintval = $exp->expect( 10,"#");
$newmatch = $hostintval[3];
@hostname = split(m[ |\n],$newmatch);
foreach(@hostname)
{
if ( ($_ !~ m/^hostname$|^show$|^run$|\||^include$|^$
+|\cM$/) )
{
if ( $DNAME ne $_ )
{
print LOG "$DNAME" . " is inco
+rrect please change to " . "$_\n";
$DNAME = $_;
}
}
}
}
}


Module:


Code
package cisco; 

use strict;
use Exporter;
use Expect;

use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK $UNAME $PWD $ENABLE $DNA
+ME $IP $LOG $command $result $exp );

$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(login_auth);

( $UNAME, $PWD, $ENABLE, $DNAME, $IP, $LOG ) = @ARGV;

$exp = new Expect;

open( LOG, ">>$LOG" );

sub login_auth
{
$command = "ssh $UNAME" . "@" . "$IP";
$exp->spawn($command) or die "Cannot spawn $command: $!\n";

my $connect = $exp->expect
(
30,
[ qr/\(yes\/no\)\?\s*$/ => sub { $exp->send("yes\n");
+exp_continue; } ],
[ qr/assword:\s*$/ => sub { $exp->send("$PWD\n"); } ],
);
$result = $exp->expect(30, "#", ">");
if ($result == 2)
{
$exp->send("enable\n");
$result = $exp->expect(30, "assword:", "#");
if ($result == 1)
{
$exp->send("$ENABLE\n");
$result = $exp->expect(30, "assword:", "#");
if ($result == 1)
{
print LOG "Enable Password Rejected\n"
+;
exit();
}
}
}
$exp->send("term len 0\n");
$exp->expect(10,"#");
}

1;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives