Accessing Cisco Routers Programmatically
A few weeks ago a friend asked me if I could make webapp for monitoring all the active calls on a CUBE, and showing it in some logical way. In our case we had 5 call legs per call so it was pretty convoluted to look at it all. I wrote a quick script that can login to a router via Telnet or SSH in ruby that any of you can use to run commands. I figured I would share that since its pretty useful. I have a script like this for example that runs against a CSV file of usernames, passwords and IP Addresses in order to pull show runs…
Overview of the code
As you can see below it supports both telnet and ssh. Since these are protocols that have to wait for a response, you can see that there is a timeout and a wait-time to get the complete response. You may have to mess with these a bit based on where your routers are located.
Another important not is this… “Match” => /#/” This is basically saying what Char we should match the end on… in my case I am looking for the hostname# prompt of the routers 😉
Lastly, make sure to disconnect at the end of the script or you will tie up all the sessions 🙂
protocol = "telnet" command = "sh run" username = "test" password = "Test" ip = "10.10.10.10" if protocol == "telnet" require 'net/telnet' localhost = Net::Telnet::new("Host" => ip, "Timeout" => 30, "Waittime" => 0.2, "Prompt" => /Username:/) localhost.cmd("String" => username, "Prompt" => "Password:") { |c| print c } localhost.cmd("String" => password, "Match" => /#/) { |c| print c } localhost.cmd("String" => "term len 0", "Match" => /#/) { |c| print c } @response = localhost.cmd("String" => command, "Match" => /#/){ |c| print c } localhost.close elsif params[:protocol] == "ssh" require 'net/ssh' session = Net::SSH.start(params[:ip],username,:password => password) t = Net::SSH::Telnet.new("Session" => session, "Timeout" => 30, "Waittime" => 0.2,"Prompt" => /#/) t.cmd 'term len 0' @response = t.cmd command end
Conclusion
You can do some pretty bad-ass things with this. I do lot’s of projects where we have changes on 100’s of routers, and we use tools like these to automate those rollouts and confirmation of successful changes.
5 responses to “Accessing Cisco Routers Programmatically”
New to this so correct me if I’m wrong, but shouldn’t the variable listed as IP be the same later on in the script? Line 5 has i defined as “IP”, but in line 9 its “ip”
Your completely right and I have edited the code. Many times I pull code from our rails apps we sell just to share tidbits with the public. This is one that was in a rails web app I just edited on the fly. I am always open to fixes and suggestions, so thanks!
Chad
Your correct. I will update it… This was a rails web app that I pulled out and turned into a standalone script on the fly :). Thanks!!
Sometimes do the same thing but with TCL+Expect )
Even though we use TCL for some really customized integrations on the routers (because that’s all it supports), I would certainly never use it as a language optionally. The state machine idea just kills me, but that is just preference.