Ssh – How to use SSH to run Cisco IOS commands in Global config mode

ciscoiossshswitch

I know about Kron, but there is supposedly no way to run global config commands:

http://www.techrepublic.com/article/schedule-commands-with-cisco-ios-kron/

If I connect using Putty, Putty is able to run several commands after login and that works like a charm. The thing is I want to automatically enter into a switch each night at 11pm and shut down a range of interfaces.

I am trying with sshpass but it seems to only allow 1 command at a time.

There is also a passwordless way to enter cisco switches but from IOS 15.0 on:

https://www.m00nie.com/2010/12/password-less-ssh-login-using-pki-to-cisco-ios/

Best Answer

It happens I have had the same kind of job to do, a few years ago. Here's what I did at the time, I hope that can help :

#!/usr/bin/expect -f

set ipadr [lindex $argv 0]
set cmd [lindex $argv 1]
set fich [lindex $argv 2]

if { ${cmd} == 1 } then {set comm "sh flash | tee tftp://TFTP-IP-ADDRESS/essai\r"} else {set comm "copy flash:${fich} tftp://TFTP-IP-ADDRESS\r"}

spawn ssh niji@${ipadr}
expect {
"password:"  { send "YOURPASSWORDHERE\r" }
"(yes/no)?"  { send "yes\r"; expect { "password:" { send "YOURPASSWORDHERE\r"; }}}
"Name:"      { send "YOURUSERNAMEHERE\r"; sleep 3 ; send "YOURPASSWORDHERE\r"; }
"Connection refused" { exit }
}

expect {
">" { send "en\r" ; sleep 3; send "EN-PASSWD\r";}
"#" { send "\r" }
}


expect {
"#" { send "${comm}" ; sleep 5; send "\r" ;send "\r" }
}

expect {
"#" { send "exit\r"; send "quit\r" }
}

This connects an IOS device with the IP Address provided as an argument. The passwords and usernames are hard coded, so I suppose you'd want to improve that.

The command sent to the router is "copy run tftp" or something of the like, but you can change it to whatever you'd need too.

That was a while ago, I suppose I would do some re-work if I had to re-use it now, but that could be a good base to start with.

Cheers,