Telnet to cisco router via SSH ‘hop’ server
Posted on september 9, 2010
Filed Under Perl | Leave a Comment
Here’s a very short example how to telnet to a Cisco router using Net::Telnet by first hopping to a SSH server using Net::OpenSSH.
This example opens an SSH session to a linux server, the server opens a telnet session inside the SSH tunnel and returns the PTY handle which can be used in Net::Telnet.
use strict;
use Net::OpenSSH;
use Net::Telnet;
my $lhost = "debian";
my $luser = "mwallraf";
my $lpass = "";
my $chost = "cisco";
my $cuser = "cisco";
my $cpass = "cisco";
my $prompt = '/(?:Username: |Password: |[>#])/m';
my @commands = ("term len 0", "show clock\r", "show version\r", "show users\r");
my $ssh = Net::OpenSSH->new($lhost,
'user' => $luser,
#'password' => $lpass,
'master_opts' => [ '-t' ],
#'async' => 1 # if enabled then password cannot be set here
);
my ($pty, $err, $pid) = $ssh->open2pty("telnet cisco");
my $t = new Net::Telnet(
-telnetmode => 0,
-fhopen => $pty,
-prompt => $prompt,
-cmd_remove_mode => 1,
-output_record_separator => "\r",
# -dump_log => "debug.log",
);
my $end = 0;
while (!$end) {
my ($pre, $post) = $t->waitfor($prompt);
print "pre = $pre, post = $post\n";
if ($post =~ /Username/m) {
# send username
$t->print("$cuser");
}
elsif ($post =~ /Password: /m) {
# send password
$t->print("$cpass");
}
elsif ($post =~ /[>#]/ && @commands) {
# send commands
my $cmd = shift(@commands);
# commands without output do not contain break
if ($cmd !~ /[\r\n]/) {
$t->print($cmd);
}
else {
print $t->cmd($cmd);
}
}
else {
# finish it
$end = 1;
$t->print("exit");
}
}
$t->close();
Useful OneLiners
Posted on maart 21, 2008
Filed Under Perl One-Liners | Leave a Comment
# add first and penultimate columns
perl -lane 'print $F[0] + $F[-2]'
# just lines 15 to 17
perl -ne 'print if 15 .. 17' *.pod
# in-place edit of *.c files changing all foo to bar
perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c
# command-line that prints the first 50 lines (cheaply)
perl -pe 'exit if $. > 50' f1 f2 f3 ...
Perl “One-Liners”
Posted on maart 21, 2008
Filed Under Perl One-Liners | Leave a Comment
Perl “One Liners” are -surprise surprise- little perl programs written on one line.
Here are a few simple examples :
Guess what this one does
$ perl -e 'print "Hello";' -e 'print " World\n"'
DOS to Unix text convert
$ perl -i -pe 's/\r//g' file
Unix to DOS text convert
$ perl -i -pe 's/\n/\r\n/' file
‘Fun’ with columns:
$ echo a b c | perl -lane 'print $F[1]'
b
$ echo a b c | perl -lane 'print "@F[0..1]"'
a b
$ echo a b c | perl -lane 'print "@F[-2,-1]"'
b c
More one-liners will follow…
Managing IP networks for free
Posted on januari 4, 2008
Filed Under OpenSource, Network Monitoring | Leave a Comment
Here’s a great tutorial on how you could manage your ip network for free! All you need is a Linux box (surprise surprise) and of course an ip network.
The tutorial discusses some popular tools and scripts to perform these tasks :
- Configuration change measurement: storing the configuration history of your network in a useful way;
- Automatic network configuration audits: self-consistency checks wired to loud alarm bells;
- Automatic generation of DNS records for router interfaces, in the interests of traceroute beautification;
- Automatic generation of graphical maps to help operators visualise live topologies as their networks evolve;
- Gathering data from BGP peers to help in choosing “maximum-prefix” session limits.
Here’s the link : tutorial
Cisco::Accounting 1.01 on CPAN
Posted on augustus 20, 2007
Filed Under Cisco-Accounting, Perl | Leave a Comment
Also our perl module Cisco::Accounting is now available on CPAN.
This module parses and aggregates IP Accounting information from remote Cisco devices or from remote hosts running the IPCAD daemon.
Several other actions are possible like checking and changing the accounting setting on interfaces, clear ip accounting, etc.
The module summarizes all data that it has parsed and also keeps a history.
A front-end to this module is our CIPAT - Cisco IP Accounting Tool.
More information at our products page.