#!/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 incorrect please change to " . "$_\n"; $DNAME = $_; } } } } } #### package cisco; use strict; use Exporter; use Expect; use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK $UNAME $PWD $ENABLE $DNAME $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;