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.10/Cisco.pm

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('?')

-- 
Posted via http://www.ruby-forum.com/.