ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to write a perl script that will ssh to a switch/router and execute some commands. If the ssh connection is refused, it should execute a small block of code to push the IP to an array. Once all the SSH sessions are complete, it should run a telnet sub and use the array created above as input. All is well with the exception of pushing elements to the array. Here is the code:
Code:
#!/usr/bin/perl
use warnings;
use strict;
use Net::SSH::Expect;
use Net::Telnet::Cisco;
my %SwitchIPs = (
##Large hash of IP => hosts
);
my %RouterIPs = (
##Large hash of IP => hosts
);
sub _SSH
{
my @Telnet;
foreach my $key ( keys %SwitchIPs)
{
open(LOG,">/tmp/perl_ssh.$key") or die "Can't open file for writing: $!";
my $ssh= Net::SSH::Expect->new(
host => $key,
password => 'pass',
user => 'user',
raw_pty => 1,
timeout => 5
);
my $login_output = $ssh->login();
if($login_output =~ /refused/)
{
print $login_output;
push(@Telnet, $key);
}
$ssh->exec("term length 0");
print LOG "Output of show except: \n";
print LOG my @Except = $ssh->exec("sh except");
print LOG "\n\n";
print LOG "Output of show service: \n";
print LOG my @Service = $ssh->exec("sh service");
print LOG "Output of show log: \n\n";
print LOG my @Log = $ssh->exec("sh log");
print LOG "Output of show interface: \n\n";
print LOG my @Int = $ssh->exec("sh interface");
print LOG my @Count = $ssh->send("clear count\r");
if("$key" eq "<special_ip>")
{
$ssh->exec("debug dialer");
}
$ssh->send("undebug all");
$ssh->close();
}
#_TELNET(\@Telnet);
# print \@Telnet;
close(LOG);
}
sub _TELNET
{
my @Telnet = @{$_[0]};
foreach my $host (@Telnet)
{
my $telnet = Net::Telnet::Cisco->new(host => "$host");
$telnet->login('user','pass');
print $host;
}
}
_SSH();
For some reason after the login attempt fails, the script dies. According to the examples on CPAN, this shouldn't be happening.
I am using expect within that perl script. I have used expect in shell scripts for years now. I want to move everything to perl. I guess i could put the devices that only support telnet into their own hash...
Ok, I had to dig up a real old script, it works but every ugly
Code:
#!/usr/bin/perl
#
#############################################################################
#
#
# SIFIZM 2006, January 08
# www.sifizm.com
# sifizm(at)gmail.com
#
# Released under the terms and conditions of SIFIZM
#
# $Source: "http://www.pageresource.com/cgirec/ptut14_1.htm"
# $Revision: 1.13 $
# $Date: 2006/01/08 $
# $Author: SeeFor $
#############################################################################
#
use Symbol;
use Net::Telnet ();
#
#############################################################################
#Open the Data File and put it in a list in array, comma separated.
#
#############################################################################
#
$data_file="test.dat";
open(DAT, $data_file) || die("Could not open the popHD.dat file, please create it!");
@raw_data=<DAT>;
close(DAT);
#
#############################################################################
#Useing the split method to create the variables we need each time through the loop.
#Since we used the pipe symbol as the separator, that is the character we will use to split the data.
#Notice that the pipe symbol needs to be escaped with a \ character since it is a special character in Perl:
#############################################################################
#
foreach $hostname (@raw_data)
{
# chop($tracedata);
# ($hostname,$port,$path,$ch)=split(/\,/,$tracedata);
#
#############################################################################
# define commands
# list in array, comma separated,
# double quote if multiple strings
#############################################################################
#
@CMD_LIST = (
"sh interface",
"sh log"
);
$username = "perl";
$passwd = "script";
open(LOG, ">> SDlecs.log");
$t = Net::Telnet->new(-errmode => 'return');
$t->open($hostname);
$t->login($username, $passwd);
foreach $session_cmd (@CMD_LIST) {
@lines = $t->cmd($session_cmd);
print LOG "\n","-" x 80, "\n", "Host IP address:", $hostname, "\n", @lines;
}
}
close LOG;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.