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 = ""

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 }
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


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.