Forum: Ruby Ruby & Telnet & Cisco

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2ad503241c90715ab728ee528207f163?d=identicon&s=25 Pablo Zorzoli (Guest)
on 2007-03-30 17:48
(Received via mailing list)
Ruby friends,

Is there any approach similar to perl's Net:Telnet:Cisco [1] in ruby?
Has anyone nice experiences in this mix?

I usually interact with my routers/switches with perl but I would like
to switch to ruby ;-)

Regards,

Pablo

[1] http://search.cpan.org/~joshua/Net-Telnet-Cisco-1....
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-03-31 10:36
(Received via mailing list)
On Sat, Mar 31, 2007 at 12:48:22AM +0900, Pablo Zorzoli wrote:
> Is there any approach similar to perl's Net:Telnet:Cisco [1] in ruby?
> Has anyone nice experiences in this mix?

Not that I'm aware of. But if you discover something please let me know
-
I'm going to have to do this myself in the next few weeks too.

I was planning just to use plain Net::Telnet, which I think is good
enough
for what I need. I was also planning to try to make a wrapper for
Net::SSH
to make it provide an IO object that can be passed into Net::Telnet.

Regards,

Brian.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-03-31 15:40
(Received via mailing list)
On 3/31/07, Brian Candler <B.Candler@pobox.com> wrote:
> On Sat, Mar 31, 2007 at 12:48:22AM +0900, Pablo Zorzoli wrote:
> > Is there any approach similar to perl's Net:Telnet:Cisco [1] in ruby?
> > Has anyone nice experiences in this mix?
>
> Not that I'm aware of. But if you discover something please let me know -
> I'm going to have to do this myself in the next few weeks too.
>
> I was planning just to use plain Net::Telnet, which I think is good enough
> for what I need.
Please kindly let me know about this, I was trying to do this with
Catalyst 29xx series Switches and a 2600 router, no luck so far, I
guess the prompt, timeout selection is quite tricky :(

Cheers
Robert
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-04-01 09:49
(Received via mailing list)
On Sat, Mar 31, 2007 at 10:40:15PM +0900, Robert Dober wrote:
> >Not that I'm aware of. But if you discover something please let me know -
> >I'm going to have to do this myself in the next few weeks too.
> >
> >I was planning just to use plain Net::Telnet, which I think is good enough
> >for what I need.
> Please kindly let me know about this, I was trying to do this with
> Catalyst 29xx series Switches and a 2600 router, no luck so far, I
> guess the prompt, timeout selection is quite tricky :(

The best documentation is the source, /usr/lib/ruby/1.8/net/telnet.rb

Make sure you call your code with a debug block, e.g. (untested)

   n = Net::Telnet(...)
   blk = proc { |str| $stderr << str }
   n.login("user", "password", &blk)
   n.cmd("show users", &blk)

By adding &blk to all of the Net::Telnet methods, you'll get debugging
output to the screen and you can see how far it's getting.

IIRC, the default regexp for a prompt looks for '# ' and '> ' (i.e. with
a
trailing space). Typical Cisco prompt is 'router>' without the trailing
space. So easy enough to fix: just pass in a suitable prompt regexp.

Brian.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-04-01 09:58
(Received via mailing list)
On 4/1/07, Brian Candler <B.Candler@pobox.com> wrote:
> The best documentation is the source, /usr/lib/ruby/1.8/net/telnet.rb
>
> IIRC, the default regexp for a prompt looks for '# ' and '> ' (i.e. with a
> trailing space). Typical Cisco prompt is 'router>' without the trailing
> space. So easy enough to fix: just pass in a suitable prompt regexp.
>
> Brian.
>
Brian that is sound advice, but actually the problem is more or less
the subtle details about the Cisco interface(1), like getting rid of
the '---More---' or nonstandard login without user.

I think that's why there are some modules around in other languages. I
think it is a good possibility to share some findings and I might have
a look at the Perl module some of these days.

Cheers Robert
>
(1) used net/telnet for some remote control of old Unix systems
without ssh quite often, the code is really easy to factorize, maybe
it is too for Cisco, but given the complexity of the Cisco OS I doubt
it :(, So the problem is not within Ruby but within Cisco.
7b56484f1e9d9af7b4c2c7ef16142197?d=identicon&s=25 Martin Boese (Guest)
on 2007-04-01 10:55
(Received via mailing list)
I don't know perl's cisco library, but this (see attached) is what I
wrote to
send commands to cisco routers and switches or fetch some values.

To get it's running config for example I do this:

------------------------------------
require 'ciscotelnet'

c = CiscoTelnet.new("Host" => '10.253.1.8',
                    "Password" => "users-password",
                     "Enable" => "enable-password",
                     "User" => "martin")

c.open
c.login
c.enable
c.cmd "terminal length 0"
puts c.cmd("show run", 20)
------------------------------------

Martin
5609f020eec393b57385851485ac692e?d=identicon&s=25 Hector Quiroz (hector)
on 2007-04-01 23:17
Pablo Zorzoli wrote:
> Ruby friends,
>
> Is there any approach similar to perl's Net:Telnet:Cisco [1] in ruby?
> Has anyone nice experiences in this mix?
>
> I usually interact with my routers/switches with perl but I would like
> to switch to ruby ;-)
>
> Regards,
>
> Pablo
>
> [1] http://search.cpan.org/~joshua/Net-Telnet-Cisco-1....

I don't know what perl's Net:Telnet:Cisco does, but I wrote this script
that allows me to run commands on our Cisco fiber switches without
having to login.

I'm not sure this is what your looking for but I hope it helps out.
I can provide the 'hec_getopts' module if you're intrested.

Hector


require 'pty'
require 'expect'
require 'hec_getopts'

# Global variables
$expect_verbose = true    # Writes all characters read from the I/O
stram to STDOUT.

class MyExpect
  def initialize(switch='cisco1')
    @switch     = switch
    STDOUT.sync = true
    STDERR.sync = true
    @passwd     =
File.new("/root/.system/.fraces/#{@switch}_dm").gets.chomp
    @uid        =
File.new("/root/.system/.fraces/#{@switch}_dm_user").gets.chomp
  end

  # This is where RubyExpect takes over
  def showInfo(cmd)
    PTY.spawn("ssh #{@uid}@#{@switch}") do |r_f, w_f, pid|    # Spaw a
process and create filehandles to it's input/output
      w_f.sync = true
      r_f.expect(/^password:/io) { w_f.puts @passwd }
      r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "terminal length
0\n" }   # Make terminal length infinite!!!

      case cmd
        when /^rc$/
          r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "show
running-config\n" }
        when /^sc$/
          r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "show
startup-config\n" }
        when /^az$/
          r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "show zoneset
active\n" }
        when /^zs$/
          r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "show
zoneset\n" }
        when /^fl$/
          r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "show flogi
database\n" }
        when /^pi$/
          r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "show port
internal info\n" }
        when /^td$/
          r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "show tech
detail\n" }
        else
          r_f.expect(/(switch\d+|1301)#\s+/io) { w_f.puts "#{cmd}\n" }
      end
      r_f.expect(/(switch\d+|1301)#\s+/io)     { w_f.puts "exit\n" }
      puts
    end
  end
end

class Help
  def showHelp(help)
    help_string = <<-EOHelp

  #{$PROG_NAME} lists configuration information for the Cisco directors.
  Syntax: #{$PROG_NAME} [-s[cisco1|cisco2|tcisco1|tcisco2]] <command>


-Options-

  -s cisco_switch_name
    Selects a Cisco switch to pull information from(Cisco1, Cisco2,
tcisco1, tcisco2).
    If no switch number is specified on the command-line, Cisco1 is the
default.


[ Commands: ]
  rc - Shows running-config
  az - Shows active zone
  zs - Shows zoneset
  fl - Shows flogi database
  pc <slot> <port> - Shows port config for <slot> and <port>
  td - Show tech detail


[ Examples: ]
  #{$PROG_NAME} rc
    Displays currently running configuration for Cisco1

  #{$PROG_NAME} -scisco2 az
    Displays currently active zone for Cisco2
EOHelp

    (help == '?' or help == 'h') and puts(help_string)
    help and exit(0)
  end
end

$PROG_NAME = File.basename($0).freeze

# ? = help, h = help, s = switch_name
cmdln = GetOpts.new('?hs:')
Help.new.showHelp(cmdln.orHas?('?', 'h'))
cmdln.has?('s') ? exp = MyExpect.new(cmdln.has?('s')) : exp =
MyExpect.new()
#cmdln.argv[0] ? exp.showInfo(cmdln.argv[0], cmdln.argv[1],
cmdln.argv[2]) : Help.new.showHelp('?')
cmdln.argv[0] ? exp.showInfo("#{cmdln.argv.join(' ')}") :
Help.new.showHelp('?')
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-04-02 09:32
(Received via mailing list)
On Sun, Apr 01, 2007 at 04:58:22PM +0900, Robert Dober wrote:
> Brian that is sound advice, but actually the problem is more or less
> the subtle details about the Cisco interface(1), like getting rid of
> the '---More---' or nonstandard login without user.

If all the devices are under your control, it's probably reasonable to
standardise on "login local" so that you get prompted for both username
and
password (since this can then be extended to RADIUS/TACACS without
changing
the interaction)

As for the ---More--- prompt, simply send "term length 0" as the first
command after logging in and that problem goes away :-)

Regards,

Brian.
This topic is locked and can not be replied to.