netauto has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I'm new to Perl but have exposure to Expect.
Please help me understand what I'm doing wrong here.
I have 2 working login processes one for SSH and one for Telnet.
What I want to do is to Try SSH (connection of Choice) if that is not supported by the device I want to try Telnet. This is where I'm having the issue I can't seem to get that piece to work.
When I try the SSH process it fails in the first line returning "SSHProcessError The ssh process was terminated." to my screen I have tried multiple things and still seem to fail. Below is the script and some of what I've tried, most of which is commented out, with my most current attempt in tact.
#!/usr/bin/perl use warnings; use strict; use Net::Telnet(); use Net::SSH::Expect; open(OUTPUT, ">>output.log"); my $host = "10.10.10.10"; my $username = "myuser"; my $passwd = "mypass"; my $enpasswd = "myenpass"; my @output = ""; my @newarray = ""; my $Prompt = ""; my $tempstring = ""; my $msg = ""; my $Chassis = ""; my $access_method = ""; my $login_result = ""; my $ssh = Net::SSH::Expect->new ( host => $host, password => $passwd, user => $username, raw_pty => 1, debug => 1, timeout => 5 ); my $telnet = new Net::Telnet ( Timeout => 20, Errmode => 'die', Prompt => "/#|login:|username:|>/i" ); sub cisco_telnet_login { $telnet->open($host); $telnet->login($username, $passwd); $telnet->print("term length 0"); my @output = $telnet->waitfor('/(.*[#>])/i'); #create an array "out +put" so we can grab the real prompt from it my $device_prompt = $output[$#output]; # now take the array "output +" and grab the last position in the array using "$#" $Prompt = $output[$#output]; $access_method = "cisco_telent"; $tempstring = shift(@output); if ( $tempstring =~ m/#/ ) { $telnet->print("term length 0"); @output = $telnet->waitfor('/(.*[\$%#>]) $/i'); #create an array + "output" so we can grab the real prompt from it $device_prompt = $output[$#output]; # } if ( $tempstring =~ m/>/ ) { $telnet->print("enable"); @output = $telnet->waitfor('/password:|passcode:/i'); $telnet->print("$enpasswd"); @output = $telnet->waitfor('/(.*[\$%#>]) $/i'); $telnet->print("term length 0"); @output = $telnet->waitfor('/(.*[\$%#>]) $/i'); #create an array + "output" so we can grab the real prompt from it $device_prompt = $output[$#output]; # } print @output; print($Prompt); @output = ""; # Reset array @output - this is not actually empty it + contain on item and it is empty # print("\n"); # $telnet->prompt(/$Prompt/); $telnet->errmode("die"); # print(@output); }; sub cisco_ssh_login { my $login_output = $ssh->login(); if( $login_output !~ /\#\s*\z/ ) { print "\nInside Not Like Section\n"; my $enable = $ssh->exec( "enable" ); # if we have a password prompt after enable, send the password if( $enable =~ /[Pp]assword:/ ) { my $enablepass = $ssh->exec( $enpasswd ); # did the enable password fail? if( $enablepass !~ /\#\s*\z/ ) { $msg = "Enable password failed."; $ssh->close( ); return( 249, $msg ); } } # we didn't require a password, but did we get the enable prompt +? elsif( $enable !~ /\#\s*\z/ ) { $msg = "Enable mode prompt not found."; $ssh->close(); return( 249, $msg ); } } $access_method = "cisco_ssh"; if ($login_output =~ /Nexus/) { print"\nFound Device is Nexus During Login\n\n"; } #### Un-Comment next Line to see Login #print "$login_output\n"; @output = $ssh->exec("\n"); $tempstring = shift(@output); @newarray = split(/\r\n/,$tempstring); # $tempstring may only have +new lines "\n", could be why I did not get prompt $Prompt = $newarray[1]; $Prompt =~ s/#//; $Prompt =~ s/\r//; $Prompt =~ s/\n//; print"\nPrompt Value: ",$Prompt,"\n\n"; }; ### Nexus System Info Gathers info from "show version" and sub nexus_sys_info { #### Issue Command it either Telnet or SSH Session, depending on wh +ich was estabished if ( $access_method eq "cisco_telent" ) { @output = $telnet->cmd("show ver"); @newarray = @output; } if ( $access_method eq "cisco_ssh" ) { $ssh->timeout( 4 ); @output = $ssh->exec("show ver"); $tempstring = shift(@output); @newarray = split(/\r\n/,$tempstring); } #### Issue Command it either Telnet or SSH Session, depending on wh +ich was estabished #### Iterate through output (@newarray) array Line by Line my $counter = 0; $Chassis = ""; foreach my $line(@newarray) { chomp($line); # print "\n$line\n"; $counter++; if($line =~ m/cisco ([^\s]*) Chassis/) { $Chassis = $1; print "\nChassis: ",$Chassis,"\n\n"; print OUTPUT "Chassis:\t$Chassis\n"; } #### adding for testing before IOS version is in place if($line =~ m/cisco\s+([^\s]*)\s+([^\s]*)\s+processor/i) { $Chassis = $1; print "\nChassis: ",$Chassis,"\n\n"; print OUTPUT "Chassis:\t$Chassis\n"; } } $ssh->timeout( 2 ); }; # $login_result = &cisco_telnet_login() or die "Telnet Process Couldn' +t Start: $!"; # $login_result = &cisco_ssh_login() or die "SSH Process Couldn't Star +t: $!"; # $login_result = &cisco_ssh_login() or $login_result = &cisco_telnet_ +login(); # if ( &cisco_ssh_login() eq "false") { $login_result = &cisco_telnet_ +login();} # print"\nReturn Value: $login_result\n\n"; # if( $login_result =~ m/The ssh process was terminated/ ) { # print"\nMatched on The ssh process was terminated\n\n"; # $login_result = &cisco_telnet_login(); # } $login_result = &cisco_ssh_login(); if ( $login_result ne "1") { $login_result = &cisco_telnet_login();} print "\nReturned from Login\n\n"; if ( $access_method eq "cisco_telent" ) { # @output = $telnet->cmd("term length 0"); # this is commented out +because it was set in cisco_telnet_login @output = $telnet->cmd("term width 512"); } if ( $access_method eq "cisco_ssh" ) { $ssh->timeout( 2 ); $ssh->exec("terminal width 512"); $ssh->exec("terminal length 0"); } my $system_result = &nexus_sys_info();
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Net::SSH::Expect Connection Refused
by Illuminatus (Curate) on Dec 18, 2012 at 17:06 UTC | |
by netauto (Initiate) on Dec 18, 2012 at 19:01 UTC |