Created
July 4, 2010 19:32
-
-
Save zuigon/463688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('?') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment