Table of Contents

Cisco Login Script

Here is a little script I am using for logging in to some Cisco routers and switches.

All you need is expect and telnet. Look for the packages on your distri…

What does it do?

It connects via telnet to the target device and waits for a Username and/or Password prompt. When the prompt is shown the adequate information is sent over.

After the normal login worked it sends “enable” and enteres the enable password at its prompt. (Currently login pw and enable pw have to be the same)

When you are fully logged in the script gives you full control with interact.

Install

Copy the code into a textfile, e.g. ~/bin/router, and set the file to executable.

chmod +x ~/bin/router

Edit the User and Password definition inside the file.

Make sure that directory is in PATH (if you want this).

You can connect to any Cisco device by running:

router myrouter.company.com

The script works with either User/Pw or single Pw prompts.

Code

Use it without any warranty and please respect the license. :-)

router.exp
#!/usr/bin/expect -f
#######################################
## Cisco Login Script
## by Lazyfrosch.de
## http://www.lazyfrosch.de/linux/ciscologinscript
## licensed under: http://creativecommons.org/licenses/by-nc-sa/2.0/
#######################################
set host [lindex $argv 0]
set user root
set pw XXXX
trap exit {SIGINT SIGTERM}
spawn telnet $host
 
expect {
"Username: " {
        send "$user\r"
        exp_continue
}
"Password: " {
        send "$pw\r"
}
timeout {
        send_user "\n\ntimeout...\n"
        exit 1
}
}
 
expect {
"% " {
        send_user "\n\nlogin failed.\n"
        exit 1
}
">" {
        send "enable\r"
}
}
 
expect {
"Password: " {
        send "$pw\r"
}
timeout {
        send_user "\n\ntimeout...\n"
        exit 1
}
}
 
expect {
"% " {
        send_user "\n\nenable failed.\n"
        exit 1
}
"#" {
        send_user "\nlogged in...\n"
        send "\n"
}
}
 
interact
linux/ciscologinscript.txt · Last modified: 2009/07/27 11:05 by markus