Connecting to Cisco device using Ruby

Discussion in 'Ruby' started by Peny Swain, Feb 26, 2009.

  1. Peny Swain

    Peny Swain Guest

    Hi,

    I'm new to Ruby and want to use Ruby for my automation.I'm trying out a
    small program now.Basically, what I'm trying to do is:

    1)Connect to the cisco switch via terminal server which has a public IP
    address and a port number. (I'm using a fake ip here).

    2)Login to the switch giving the right username and password.

    3)After login, issue a switch command and get the output back.


    The below program works if I comment out the "waitfor" but not
    consistent and other times it fails.Can someone please point out whats
    the issue here or if possible can someone share the piece of code that
    does the above task.


    Thanks
    s_k





    require 'net/telnet'
    require 'socket'

    localhost = Net::Telnet::new(
    "Host" => "10.10.10.20",
    "Port" => 2033,
    "Binmode" => true,
    "Output_log" => 'c:\RubyL\output.txt',
    # "Dump_log" => "dump_log",
    "Prompt" => /[$%#>:] \z/n,
    "Telnetmode" => true,
    "Timeout" => 50,
    "Waittime" => 30
    # "Proxy" => proxy
    ) { |c| print c }


    localhost.cmd("\r") {|c| print c }

    if localhost.waitfor({"Match" => 'vcctest-30-6k>' , "Timeout" => 50})
    localhost.cmd("\r enable \r"){|c| print c }
    localhost.cmd("cisco"){|c| print c }
    else
    begin

    # Adding username and password
    localhost.waitfor({"Match" => 'Username:' , "Timeout" => 50})
    localhost.cmd("cisco"){|c| print c }
    localhost.waitfor({"Match" => 'Password:' , "Timeout" => 50})
    localhost.cmd("cisco"){|c| print c }
    rescue Exception => e
    puts e.message
    retry
    #Adding enable password
    localhost.waitfor({"Match" => 'vcctest-30-6k>' , "Timeout" =>
    50})
    localhost.cmd("\r enable \r"){|c| print c }
    localhost.cmd("cisco"){|c| print c }
    localhost.cmd("exit"){|c| print c }

    ensure
    localhost.close

    end
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Peny Swain, Feb 26, 2009
    #1
    1. Advertising

  2. Peny Swain

    Peter Booth Guest

    Did you see http://groups.google.com/group/ruby-talk-google/browse_thread/thread/096cabb08c34182c
    ?

    On Feb 26, 2009, at 2:57 PM, Peny Swain wrote:

    > Hi,
    >
    > I'm new to Ruby and want to use Ruby for my automation.I'm trying
    > out a
    > small program now.Basically, what I'm trying to do is:
    >
    > 1)Connect to the cisco switch via terminal server which has a public
    > IP
    > address and a port number. (I'm using a fake ip here).
    >
    > 2)Login to the switch giving the right username and password.
    >
    > 3)After login, issue a switch command and get the output back.
    >
    >
    > The below program works if I comment out the "waitfor" but not
    > consistent and other times it fails.Can someone please point out whats
    > the issue here or if possible can someone share the piece of code that
    > does the above task.
    >
    >
    > Thanks
    > s_k
    >
    >
    >
    >
    >
    > require 'net/telnet'
    > require 'socket'
    >
    > localhost = Net::Telnet::new(
    > "Host" => "10.10.10.20",
    > "Port" => 2033,
    > "Binmode" => true,
    > "Output_log" => 'c:\RubyL\output.txt',
    > # "Dump_log" => "dump_log",
    > "Prompt" => /[$%#>:] \z/n,
    > "Telnetmode" => true,
    > "Timeout" => 50,
    > "Waittime" => 30
    > # "Proxy" => proxy
    > ) { |c| print c }
    >
    >
    > localhost.cmd("\r") {|c| print c }
    >
    > if localhost.waitfor({"Match" => 'vcctest-30-6k>' , "Timeout" => 50})
    > localhost.cmd("\r enable \r"){|c| print c }
    > localhost.cmd("cisco"){|c| print c }
    > else
    > begin
    >
    > # Adding username and password
    > localhost.waitfor({"Match" => 'Username:' , "Timeout" => 50})
    > localhost.cmd("cisco"){|c| print c }
    > localhost.waitfor({"Match" => 'Password:' , "Timeout" => 50})
    > localhost.cmd("cisco"){|c| print c }
    > rescue Exception => e
    > puts e.message
    > retry
    > #Adding enable password
    > localhost.waitfor({"Match" => 'vcctest-30-6k>' , "Timeout" =>
    > 50})
    > localhost.cmd("\r enable \r"){|c| print c }
    > localhost.cmd("cisco"){|c| print c }
    > localhost.cmd("exit"){|c| print c }
    >
    > ensure
    > localhost.close
    >
    > end
    > end
    > --
    > Posted via http://www.ruby-forum.com/.
    >
     
    Peter Booth, Feb 26, 2009
    #2
    1. Advertising

  3. Peny Swain

    Peny Swain Guest

    Peter Booth wrote:
    > Did you see
    > http://groups.google.com/group/ruby-talk-google/browse_thread/thread/096cabb08c34182c
    > ?


    Thanks Peter.It worked.Heres's what I did:



    t = Net::Telnet::new( "Host" => "10.10.10.20","Port" => 2044,"Timeout"
    => 20, "Output_log" => 'c:\RubyL\output.txt',
    "Telnetmode"=> true,"Waittime"=>2) {|c| print c }

    log = lambda{ |c| STDERR.print c }

    #needs initial enter to bring the username promt
    t.puts("\r")

    t.waitfor(/Username:/, &log)
    t.puts("cisco")
    t.waitfor(/Password:/, &log)
    t.puts("cisco")

    t.waitfor(/vcctest-30-6k>/, &log)
    t.puts("enable")
    t.waitfor(/Password:/, &log)
    t.puts("cisco")
    t.waitfor(/vcctest-30-6k#/, &log)
    t.puts("exit")
    t.cmd(""){|c| print c}
    --
    Posted via http://www.ruby-forum.com/.
     
    Peny Swain, Feb 27, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    441
    santosh
    Nov 27, 2006
  2. Carl
    Replies:
    1
    Views:
    490
    Alvin Bruney [MVP]
    Dec 31, 2007
  3. Carl
    Replies:
    0
    Views:
    666
  4. Carl
    Replies:
    0
    Views:
    163
  5. Carl
    Replies:
    0
    Views:
    160
Loading...

Share This Page