I created a script for a Linux jumpbox to automate collection of logs from devices. I had flaky standard ssh access, so I had to find a way to get the jumpbox to log all the data itself (and I could pick it up later instead of capturing to my putty window).
There is a requirement to have expect installed, how to do this can be found here:
http://expect.sourceforge.net/
Installation is beyond the scope of this how-to.
On the Linux box, run the command vi your_script_name.sh
#!/usr/bin/expect
set host [lindex $argv 0]
set user "your_username_here"
set pass "your_password_here"
puts "Starting the script. Please wait...."
spawn $env(SHELL)
expect ">" {
send "ssh -oStrictHostKeyChecking=no $user@$host | tee -a $host.txt\r"
}
expect "password:"
send "$pass\r"
expect ">"
sleep 5
send "request support information| no-more\r"
expect "*user@*"
sleep 30
send "show configuration |no-more\r"
expect "*user@*"
sleep 1
send "show virtual-chassis status |no-more\r"
expect "*user@*"
sleep 1
send "show log messages |last 50 | no-more\r"
sleep 1
interact
(Save the file with <esc> :wq <enter>)
There is a reason I had to use the following bit in the line
spawn $env(SHELL)
expect ">" {
send "ssh -oStrictHostKeyChecking=no $user@$host | tee -a $host.txt\r"
}
Without it, you will be unable to tee and save the ssh log to a file. I tried just spawning and piping to tee, but the script would fail, and various google searches later, it is working as expected. A workaround was to spawn a new copy of shell, and this works well.
Modify the script wherever it says "*user@*" to "your_username@". for example if your login name was test, you'd change it to "test@"
My script stops here as I require to run additional commands and personally examine other command output, but feel free to modify it further to suit your needs.
Note: When you exit out from the device, ensure you exit your shell again (you're running a bash shell inside bash), and you'll be back to your original shell.
I'm sure there are some additional things I could do to tweak this script, but for me, this script works really well and will save a load of time when I have to collect logs from a vast number of switches.
If you have suggestions for a better script, let me know in the comments.