SSH and Telnet Session Logging

From Internetworkpro

Jump to: navigation, search

erlielc

This page or section provides device configuration instructions
Please note that the information on this page has not been checked for accuracy and is not intended as a replacement to documentation. Please ensure you understand your desired objectives before attempting to apply any examples listed.
See more examples at Category:Configuration

Logging network configuration changes is part of a "best practice" methodology. These shell scripts function as telnet, or ssh command line utilities that automatically log your session on network devices.

Use them as if you were using either tool, and your session will be logged. The log file is based on the year/month/day/hour/second, and placed in /var/log. Also you can specify the optional -z flag (before the arguments), to manually name the log.

Note: the beginning of the session, i.e Username: and password: is not logged to the file. this data is sent to the tty line, and cannot be captured with a shell script (afik). Also, the scripts call the telnet, and ssh programs, so if you wish to name them ssh, and telnet, you will have to do a little tinkering.

you could automate displaying the last log file generated. for now, I, cd /var/log, ls | tail -1

I've been using these for a week or so, and haven't run into any problems. If you have any improvements, or suggestions, please let me know. thanks!

Telnet with auto-logging

#!/bin/bash
 
if [ "$#" == "0" ]
then
        echo "Usage: same as telnet except with an optional -z flag : -z logfile
name"
        echo "logs to /var/log use the -z switch before telnet arguments"
exit
fi
 
 
if [ "$1" == "-z" ]
then
        logname=$2
        shift 2
        echo "Trying $3"
else
        logname=$(date +%y%d%h%M%S)
        echo "Trying $1"
fi
 
#log data with date, or user input
telnet $* | tee -a /var/log/$logname
#!/bin/bash
 
if [ "$#" == "0" ]
then
        echo "Usage: same as ssh except with an optional -z flag : -z logfile
name"
        echo "logs to /var/log, use -z first if naming your file"       
fi
 
#check for logfile name option, if so shift arguments
if [ "$1" == "-z" ]
then
   logname=$2
   shift 2
else
   logname=$(date +%y%d%h%M%S)
fi
 
ssh $* | tee -a /var/log/$logname
Personal tools