Need help assigning an IP address to a Varible

Hello to the ruby world :slight_smile: I was hoping to get a bit of guidance… I
have the following code below that logs int to a cisco router and
performs a command:

#!/usr/bin/env ruby
require ‘rubygems’
require ‘net/ssh’

HOST = ‘143.132.16.43’
USER = ‘’
PASS = ‘’

puts “Give me the ip address” #(user enters 159.202.161.0)
@ipadd = gets

Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
ssh.exec('sho ip route ’ + @ipadd)

end

Below is the output form the above code (without the ##):

AMP_STL_Intra_SW4#sho ip route 159.202.161.0
Routing entry for ##159.202.161.0##/25
Known via “connected”, distance 0, metric 0 (connected, via interface)
Routing Descriptor Blocks:

  • directly connected, via Vlan307
    Route metric is 0, traffic share count is 1

AMP_STL_Intra_SW4#

I would like to scan this output and assign only the IP address between
the ##'s to a variable for further use. I am new to programming so I’m
sure this is amateur :slight_smile: Any help is mush appreciated.

Jason Ballou wrote in post #1048047:

Hello to the ruby world :slight_smile: I was hoping to get a bit of guidance… I
have the following code below that logs int to a cisco router and
performs a command:

You may find Net::SSH::Telnet easier to use than Net::SSH with exec,
especially when it comes to exchanging multiple commands and responses.
Also you can use the same code with both Net::Telnet and
Net::SSH::Telnet

I would like to scan this output and assign only the IP address between
the ##'s to a variable for further use.

Look at the methods of class String, expecially String#scan

str
=> “AMP_STL_Intra_SW4#sho ip route 159.202.161.0\nRouting entry for
159.202.161.0/25\n Known via “connected”, distance 0, metric 0
(connected, via interface)\n Routing Descriptor Blocks:\n * directly
connected, via Vlan307\n Route metric is 0, traffic share count is
1\n”

str.scan(/Routing entry for ([0-9.]+)/)
=> [[“159.202.161.0”]]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs