1

I need to automate telnet to a Cisco router and get a "show version" result. The problem is, I need to first to telnet to A, from there telnet to B and then only be able to telnet to the Cisco router.

I've tried below, but didn't work.

root@linux:~#cat command
myName
myPassword
telnet 2.2.2.2
myName2
myPassword2
telnet 3.3.3.3
cisco
ciscoPass
show version

root@linux:~#nc 1.1.1.1 23 -i 1 < command
2
  • Everytime I see cisco (or any other manufacturer) and telnet in the tags, I can't help but to think WHY??? S...S...H... why on earth do you want to use a clear text protocol on network devices?
    – Alex
    Dec 14, 2011 at 13:56
  • 1
    migration to SSH is in the way. but some of our legacy monitoring and network management software were only setup to use telnet.
    – zaidwaqi
    Dec 14, 2011 at 14:17

3 Answers 3

0

You can try TCL/Expect. It's very handy on this situations. Here follows a sample script (you'll probable have to tweak it a little):

#!/usr/bin/expect --

if {$argc != 1} {
        puts "Wrong parameters."
        puts "Usage: $argv0 <ip address router 1> <ip address router 2>"

        exit 1
}

# Telnet to router 1
spawn telnet [lindex $argv 0]

expect {
        "Username:"     {send "myuser\r"}
}

expect {
        "assword:" {
                send "mypasswd"
                send "\r"
        }
}

expect "\>"

# Telnet on router 2
# Telnet to router 1
send "telnet [lindex $argv 1]"

expect {
        "Username:"     {send "myuser\r"}
}

expect {
        "assword:" {
                send "mypasswd"
                send "\r"
        }
}

expect "\>"

send "sh ver\r"
expect "\>"

# Quiting router 2
send "quit\r"
expect "\>"

# Quiting router 1
send "quit\r"
expect "Connection closed by foreign host."

wait
exit 0
1
  • looks simple enough to use. i'll give it a try first for my case. thanks.
    – zaidwaqi
    Dec 27, 2011 at 6:39
1

this will work

(
  sleep 1
  echo user ; sleep 1
  echo pass ; sleep 1
  echo telnet router2
  sleep 1
  echo user ; sleep 1
  echo pass ; sleep 1
  echo telnet router3
  sleep 1
  echo user ; sleep 1
  echo pass ; sleep 1
  echo sh ver ; sleep 1
) | telnet router1
2
  • thanks mario, but i seem to have timing issue. i've tried removing sleep, also sleep 1,2,3 etc with inconsistent success.
    – zaidwaqi
    Dec 14, 2011 at 14:40
  • ok - i have tested this script and it works fine in my network. maybe you had to increment the sleep value. try to wait 5 seconds.
    – mario
    Dec 14, 2011 at 14:47
0

Perl library Net::Telnet::Cisco is great to handle this kind of scripts.

You only have to worry about commands and not the telnet communication with the router.

Simple example :

#!/usr/bin/perl -w

use Net::Telnet::Cisco;

if ($#ARGV == -1) {
        print "Usage : $0 <ip address>\n";
}
else {

        my $host = $ARGV[0];
        my $session = Net::Telnet::Cisco->new(Host => $host);

        $session->login('username', 'password');

        my @uptime = $session->cmd('sh ver | i uptime');
        print @uptime;

        $session->close;
}

$ ./uptime_cisco.pl router.mydomain
 router uptime is 3 weeks, 1 day, 16 hours, 55 minutes

Netcat is a great tool, but the ease of use of such a library is such that I don't use netcat any more for any automated script.

1
  • hi petrus, this is great script, but i need help to use it to telnet to the second router and run sh ver command at that router.
    – zaidwaqi
    Dec 14, 2011 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .