Jake's Blog

Scripting Login To The Lab

Now at the end of the Weekend. I needed a break from Cisco, but I wanted to make it easier to do my labs. I’ve been using PuTTY, but the windows don’t really snap together in a nice way, and you can’t use tabs. I don’t like MTPuTTY (Multi-Tabbed PuTTY). I do want to try out SecureCRT because it looks pretty good and has cool features, but I don’t have the money now to buy it. Looking around I found WinSSHTerm, which I have used in the past for messing around with linux. However, I’m not finding any clear way to use telnet on it, since the old Cisco 2511 is a product made prior to the ssh era and only uses telnet.

I remotely access my Linux VM via SSH, maybe I can find a way to make that work by scripting it. I ended up finding some new useful tools. Expect scripts, and the AutoExpect script creator. I got 98% of it working with just installing and running AutoExpect while I logged into the Cisco 2511 from my Linux VM. I just had to add 2 small tweaks to make it work. Looking through the manual and some google searching led me to these 2 answers:

1) Set the terminal environment in the config as per this part of the ‘expect’ man page.

Telnet  (verified only under SunOS 4.1.2) hangs if TERM is not set.
This is a problem under cron, at and in cgi scripts, which do not define TERM.
Thus, you must set it explicitly - to what type is usually irrelevant.
It just has to be set to something!
The following probably suffices for most cases.
set env(TERM) vt100

2) Make the script know that the user provides the input now, by putting ‘interact’ at the end of the script.

Here is the expect script, but I recommend making your own so you have more control over it.

Login Expect Script
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Aug 12 02:48:58 2019
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script.  It
# necessarily has to guess about certain things.  Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts.  If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character.  This
# pacifies every program I know of.  The -c flag makes the script do
# this in the first place.  The -C flag allows you to define a
# character to toggle this mode off and on.
set force_conservative 0  ;# set to 1 to force conservative mode even if
			  ;# script wasn't run conservatively originally
if {$force_conservative} {
	set send_slow {1 .1}
	proc send {ignore arg} {
		sleep .1
		exp_send -s -- $arg
	}
}

#
# 2) differing output - Some programs produce different output each time
# they run.  The "date" command is an obvious example.  Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer.  If this causes a problem, delete these patterns or replace
# them with wildcards.  An alternative is to use the -p flag (for
# "prompt") which makes Expect only look for the last line of output
# (i.e., the prompt).  The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don

set timeout -1
spawn telnet 172.16.25.11
set env(TERM) vt100
match_max 100000
expect -exact "Trying 172.16.25.11...\r
Connected to 172.16.25.11.\r
Escape character is '^\]'.\r
\r
\r
User Access Verification\r
\r
Username: "
send -- "myusername\r"
expect -exact "myusername\r
Password: "
send -- "mypassword\r"
expect -exact "\r
R_2511#"
send -- "r1\r"
interact

With WinSSHTerm I was able to get it setup and working. It is easier to keep track of which device I’m controlling (especially during factory resets in the lab). WinSSHTerm also allows you to save IPs and configure a command to run on the session after it connects to the remote host. Now I can automatically connect to any of my 5 cisco lab devices.

WinSSHTerm Scripted Login