I am by no means a developer. I would describe my self as a jack of all trades, I understand enough about the many things I deal with day to day to get by and I’m happy at that. Generally I try new things that help me do my job quickly and more efficiently (the two are not the same). Recently I have spent some time getting to grips with stuff that saves me from logging in to 100 devices and making the same change on each, this involves using expect scripts.
An expect script put simply is a script that fires a series of commands and then waits for a predefined response before continuing, you tell the script what to ‘expect’ before continuing. If the out put is not as expected it does not continue. Sounds simple? It is simple enough to get your head around and to make changes without need too much knowledge of scripting especially if you can find a good resource to get you started. Once you have a firm hold of the basics you can start to get clever.
I found a great post by a Network Architect called Paul Gerard Porter, his post can be found here and its a great example of to incorporate the often ring fenced world of scripting in to a network engineering role. For the most part I have used Paul’s script as is but have made a few tweaks in terms of logging and adding a notification method to send me a slack notification when a script has finished (rolling tacacs to an estate of 100 routers takes some time but is quicker than hand configuring but I would rather not sit around and watch matrix style screens all day!)
The logging tweaks were mostly based on Paul’s suggestions but for some reason it wasn’t rotating the log correctly but was adding everything to a single log file. I wanted a new log file timestamped each time I can a script so that I could easily review or send the results without swamping the reader with all the previous scripts. This was done by adding the following code to the .sh element of the expect script
# Logfile Rotation timestamp=`date +%d-%m-%H%M` logdir=Some/Location/cisco-expect/logs # This will rotate and append date stamp... logfile=$logdir/results.log newlogfile=$logfile.$timestamp mv $logfile $newlogfile
This would create a new file from results.log and append the date and time the script was run. No I have a folder full of logs that are sorted by the time/date they were ran. This isn’t perfect as I would like to reference the project I was working on at the time in the log file name but I can’t find a pretty was of doing this currently.
In addition to the logging modifications I added a very simple way of notifying me when a script completes. The script below uses jarjar (found here) and a webhook to send a message in to slack and send me a private message with some pleasantries and a reference to the log file created, following the completion of the script (I could have used $newlogfile but chose to use $logmessage in case I wanted to modify the message without affecting the logging portion of the script)
# Send log notification to Slack logmessage=results.log.$timestamp jarjar -e -m "Hey $(whoami), Looks like your script has finished, grab a :beer: and go check: $logmessage"
It was relatively trivial to set all that up and it is not overly complex if you have some knowledge of how bash scripting works. Most of my work is done mostly on a Mac and Linux servers to the scripts run happily on both environments. I have a few plans to expand on my scripting adventure and will hopefully post some original work soon!