Screen and Reverse Telnet With Macbook Pro

Read­ing Time: 5 min­utes

As a lot of read­ers already know, from com­ments and dis­cus­sions on oth­er forums, I recent­ly made the switch from PC to Mac for my main per­son­al com­put­er.  This wasn’t some sort of Microsoft-hat­ing fren­zy, but rather a case where I had more com­pelling rea­sons to use a Mac at home (Apple TV, iTunes, wife’s Mac, etc.) than I did to stick with the PC (games).  I still have the PC for when I get the gam­ing urge, but since most of my time these days is spent study­ing, gam­ing has tak­en a back burn­er for the fore­see­able future.

I won’t both­er you with my ini­tial reac­tions to the Mac as they’ve been most­ly pos­i­tive and prob­a­bly not entire­ly dif­fer­ent than many oth­er people’s expe­ri­ences.  What I will say, though, is that as a long time user of var­i­ous fla­vors of Unix, it’s nice to be oper­at­ing on a plat­form that is Unix based again.  Specif­i­cal­ly, it’s nice to not have to down­load bolt-on soft­ware in order to be able to use SSH, Fin­ger, TFTP, etc.

One of the tools that I have used exten­sive­ly in the past is GNU Screen.  For those unfa­mil­iar with the pro­gram, you can check out a brief overview over at Wikipedia: http://en.wikipedia.org/wiki/GNU_Screen , or at the GNU Screen web­site here: http://www.gnu.org/software/screen/ .

Despite using Screen since I was first intro­duced to it back around 1990 or so, if I recall cor­rect­ly, I’ve only ever used it real­ly for one thing: con­nect­ing from a Unix Machine to some­thing using the ser­i­al port or a modem.  Even the modem bit I didn’t use all the time as I tend­ed to favor Mini­com.  So, when a cou­ple of friends sug­gest­ed that I use Screen for solv­ing a prob­lem I was hav­ing, it made sense.

As part of my Cis­co lab I have some four­teen dif­fer­ent devices that I need to access at any one time.  I have all of those devices set up to my main ter­mi­nal serv­er (3945 which is also my main a‑lot-of-oth­er-things as well) using reverse tel­net.  In this way I can do a lot of things, but typ­i­cal­ly I would SSH to the ter­mi­nal serv­er, con­nect to a host by name, then use Ctrl-Shift‑6 x to get back to the ter­mi­nal serv­er and do the same thing again for anoth­er host.

This is all made pos­si­ble using an Async card:

NAME: "High Speed Wan Interface card with 16 RS232 async ports(HWIC-16A) on Slot 0 SubSlot 3", DESCR: "High Speed Wan Interface card with 16 RS232 async ports(HWIC-16A)"

 

With a per­ti­nent con­fig­u­ra­tion like so: 

interface Loopback0
ip address 172.16.0.1 255.255.255.0
ip host s2 2054 172.16.0.1
ip host s1 2053 172.16.0.1
ip host s4 2056 172.16.0.1
ip host s3 2055 172.16.0.1
ip host r5 2063 172.16.0.1
ip host r2 2060 172.16.0.1
ip host r6 2064 172.16.0.1
ip host r4 2062 172.16.0.1
ip host r3 2061 172.16.0.1
ip host r1 2059 172.16.0.1
ip host r8 2066 172.16.0.1
ip host r7 2065 172.16.0.1
ip host ASA 2051 172.16.0.1
ip host AP1 2052 172.16.0.1
ip host bb2 2057 172.16.0.1
ip host bb1 2058 172.16.0.1
line 0/3/0 0/3/15
session-timeout 120
no exec
transport input telnet

 

Still, open­ing four­teen hosts can get tedious, as you might imag­ine.

What this con­fig­u­ra­tion also allows for, how­ev­er, that I had only been using occa­sion­al­ly is log­ging in to any device by open­ing a tel­net con­nec­tion to the port.  So, I can either tel­net to the par­ent device’s IP address (in this case it’s 10.7.68.221) or to the loop­back address (if I have rout­ing) and adding on the port num­ber like so:

telnet 172.16.0.1 2056

 

which, as we see from above, would take me to the device labeled “s4”.   That’s all good, but still requires me to open each tel­net ses­sion by hand, right?  Not nec­es­sar­i­ly: enter Screen and the mag­ic of con­fig­u­ra­tion files.

I have two con­fig­u­ra­tion files for Screen: one is a .screen­rc file which sits in my home direc­to­ry as is stan­dard on Unix sys­tems.  Note that I could also put this in my /etc direc­to­ry and change the name if I want­ed a sys­tem-wide default for all users.  The oth­er file is a cus­tom one that I only use when I’m doing lab-work, and I call that one screen.ccie.

If I want to open just Screen by itself, from a ter­mi­nal (I use iTerm2) I just type “screen –A” which opens up Screen with all pos­si­ble emu­la­tions enabled (just in case).  If I want to open my ccie lab con­fig­u­ra­tion I type “Screen –Ac screen.ccie” to ref­er­ence the start­up file.  I also use “screen –A /dev/tty.KeySpansomething” to use my USB/Serial adapter if I’m at the con­sole some­where.

Below you’ll find the screen.ccie file, with com­ments, as well as a screen shot of what it looks like in action.  I didn’t include the .screen­rc file because it is iden­ti­cal, except for the last sec­tion that opens up all of the dif­fer­ent tel­net ses­sions.  I’m sure there are a lot of ways to do this, some bet­ter than oth­ers, and infi­nite com­bi­na­tions of col­ors, stats, etc. that can be con­fig­ured, but this is what I have for now.

 

 

# SomeClown's .screenrc file.  Cribbed from a lot of places.
# Credit to both @IPv6Freely and @jay25f

# Next and Previous tabs shortcuts
bindkey "^e" next       # Next
bindkey "^w" prev       # Previous

# Various options
escape ^Zz                      # Control-A is needed elsewhere
nethack on                      # Just 'cuz
maptimeout 5            # Fixes Vi
vbell off                   # Pavlov
startup_message off     # Meh
defscrollback 30000     # Lots o' logging room
autodetach on           # Go away, come back.
shelltitle ""

# Status at bottom of screen; tabs, etc.  The code's a FusterCluck, but looks nice
hardstatus alwayslastline "%-Lw%{= BW}%50>%n%f* %t%{-}%+Lw%< %=%D %M %d %c"
hardstatus string '%{= kK}%-Lw%{= KW}%50>%n%f %t%{= kK}%+Lw%< %{=kG}%-= %d%M %c:%s%{-}'

# Specific for CCIE Lab.  Standard .screenrc file doesn't include text below

# Create named tabs for each device
screen -t Stuff 0                       # General
screen -t R1    1
screen -t R2    2
screen -t R3    3
screen -t R4    4
screen -t R5    5
screen -t R6    6
screen -t R7    7
screen -t R8    8
screen -t S1    9
screen -t S2    10
screen -t S3    11
screen -t S4    12

# For each tab above, do stuff
at R1 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2059 \015"
at R2 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2060 \015"
at R3 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2061 \015"
at R4 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2062 \015"
at R5 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2063 \015"
at R6 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2064 \015"
at R7 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2065 \015"
at R8 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2066 \015"
at S1 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2053 \015"
at S2 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2054 \015"
at S3 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2055 \015"
at S4 stuff " echo 'Starting some shite...' && sleep 10 && telnet 10.7.68.221 2056 \015"