One of the worst things about doing network engineering, is that it’s nearly impossible to parse the output of the commands over telnet. Sure there’s some support in IOS and others for searching for strings, but nothing close to the power of a Linux shell.
cl.sh is a script I wrote to fix this problem. It involves a couple of other tools to make it effective, but once you’re on board, I SWEAR it will change the way you do network administration.
To tempt your tummy with some delicious advice, I’m going to show you a screen shot of what I’m talking about in action. In this screen shot of Terminator, on the left side, you’ll see a regular telnet session in progress. On the right, cat last has magically parsed the output of the telnet session in the other window. In this case, I wanted to find out which microwaves were up. Knowing a port label with a microwave attached ends in -DW, a regex “DW.*Up” is just what I need. Cat last has identified the telnet output I’m interested in, highlighted it, and cut out all the uninteresting garbage for me.
What wizardry am I using do to this? Meh, standard unix tools and pipes. Here’s how it works…
Remember my jump script? Remember how it records our jump sessions? Yea, those log files are stored locally and we can molest them as we see fit. Cat Last will open the last modified log file in ~/.scripts and tail the last X lines. If supplied as arguments, strings can be egrep’d or piped after cl.sh to whatever Linux utility you can imagine.
Have multiple jump sessions opened and want to parse a particular stream? No problem, just hit enter in the session you want to parse to update the log file’s last update time and cat last will figure it out.
One additional tip… Cat Last will default it’s tail of the log file to X lines. You can modify this on the fly by exporting CLINES= with whatever positive, non-zero integer you like.
Here is the current version of cl.sh:
#~/bin/bash
# v 1.0 2011-02-01
# http://Julian.Tosh.us/
#
# cl script requires jump script (See wiki)
# cl script will find the last modified script in ~/.scripts for parsing.
# limited parsing is available by specifying strings as arguments.
# These strings are concatenated and deliniated with '|' and used with egrep.
# Alternatively, you can simple use cl | egrep/grep/awk/sed/etc.
# The purpose for this is to easily parse important data from a ssh/telnet session.
# No of lines to tail from the latest .scripts log
DEFAULTCLINES=5000
# Display usage
USAGE () {
echo "Usage: $0 [str1 ... strx ] | sed/awk/grep/etc"
echo " Will 'tail -n $DEFAULTCLINES' the last modified ~/.script file."
echo " If no pipe exists, arguments can be simple text to grep."
echo " To change the number of lines, export CLINES in your environment."
echo " strx is a simple string to be grep'd and colored in the output."
echo " supplying strings shortens the command to do simple greps."
}
# Concatenate arguments and deliniate with pipes.
# i.e. setup multiple strings for egrep
REGEX=$(echo $@ | tr " " "|")
# No of lines to tail can be modified with this env variable
if [ -z $CLINES ]; then
CLINES=100
fi
# Nifty feature to check for connected pipes or terminals.
if [ -t 0 ]; then
echo "stdin is a terminal" > /dev/null
else
echo "stdin is a pipe" > /dev/null
fi
# Only display usage if no pipes are connected and no arguments supplied.
if [ -t 1 ]; then
echo "stdout is a terminal" > /dev/null
if [ $# -eq 0 ]; then
USAGE
fi
else
echo "stdout is a pipe" > /dev/null
fi
# Caution, magic is happening here
# Finding the latest session
FN=$(/bin/ls -tr -1 ~/.scripts/*.log | tail -n 1)
# display a header to segment parsed data for easy reading
echo "Using $FN"
echo -en '\e[00;31m'
printf "%$(tput cols)s\n"|tr ' ' '='
printf "%$(tput cols)s\n"|tr ' ' '='
tput sgr0
# Dump the session log
# If arguments were supplied, grep them - or it all goes to stdout.
if [ $# -gt 0 ]; then
{
tail -n $CLINES $FN
} | egrep --color "$REGEX"
else
tail -n $CLINES $FN
fi
