Skip to content

Commit

Permalink
Monster cleanup and gem-ified
Browse files Browse the repository at this point in the history
  • Loading branch information
jakedouglas committed Jan 13, 2009
1 parent ae5fee6 commit 3ff7580
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 266 deletions.
66 changes: 66 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Ruby-Cisco
==========
This tool aims to provide transport-flexible functionality, for easy communication
with Cisco devices. It currently allows you to execute commands on a device and get
back the output of those commands.

The library provides two styles to use:

cisco = Cisco::Base.new(:host => "10.0.0.1", :password => "accesspass")
cisco.cmd("sh ver")
cisco.enable("enablepass")
cisco.cmd("sh run")
output = cisco.run

This will return an array of results, one string for the output of each command. The
following block style usage returns the same results, though some may prefer it:

output = cisco.run do |x|
x.cmd("sh ver")
x.enable("enablepass")
x.cmd("sh run")
end

SSH and Telnet should be working OK at this time. Unfortunately due to the asynchronous
design of the Net::SSH library's API, it is not easy to provide an interface for
operating conditionally on output data in the middle of a session like you may be
used to, for example:

output = device.cmd("show version")
if output =~ "IOS"
device.cmd("run some ios command")
else
device.cmd("run some catos command")
end

Similar behavior could be achieved by setting up the SSH session yourself. You can peek
at the #run method in ssh.rb for how to do this. Instead, the interface of this library
allows you to simply execute a series of commands all in one run, and get the output at
the end. I have limited the synchronous advantage of Net::Telnet by conforming it to this
model, but I wanted to have them both be used in the same fashion.

The Base class should be used unless you know what you're doing and what I have provided
is not adequate. Telnet is used by default. To use SSH, you must specify:

cisco = Cisco::Base.new(:host => "10.0.0.1", :user => "admin", :password => "accesspass", :transport => :ssh)

You can also pass an array of direct arguments that are used to instantiate the transport object.
This is useful, if for instance, you want to use public key authentication with SSH:

cisco = Cisco::Base.new(:directargs => ["10.0.0.1", "admin", :auth_methods => ["publickey"]])

In the future, I would like to provide subclasses to retrieve, present and set configuration
parameters in an OO fashion, like:

router.int["fa0/4"].speed
=> 100
router.int["fa0/4"].speed = 10
router.apply!

I have yet to come up with a good way for implementing this that will scale across
a wide variety of devices. Please let me know if you have input and want to help.

[email protected]
yakischloba on Freenode

Thanks to Jamis Buck for creating Net::SSH and helping me understand how to do this.
20 changes: 0 additions & 20 deletions README.txt

This file was deleted.

21 changes: 21 additions & 0 deletions cisco.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Gem::Specification.new do |s|
s.name = "cisco"
s.version = "0.0.1"
s.date = "2009-01-13"
s.authors = ["Jake Douglas"]
s.email = "[email protected]"
s.rubyforge_project = "cisco"
s.has_rdoc = false
s.summary = "Library for accessing Cisco devices via Telnet and SSH"
s.homepage = "http://www.github.com/yakischloba/ruby-cisco"
s.description = "This tool aims to provide transport-flexible functionality, for easy communication
with Cisco devices. It currently allows you to execute commands on a device and get
back the output of those commands."
s.files = ["README",
"lib/cisco.rb",
"lib/cisco/common.rb",
"lib/cisco/telnet.rb",
"lib/cisco/base.rb",
"lib/cisco/ssh.rb"]
s.add_dependency('net-ssh')
end
110 changes: 0 additions & 110 deletions lib/base.rb

This file was deleted.

5 changes: 5 additions & 0 deletions lib/cisco.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$:.unshift File.expand_path(File.dirname(File.expand_path(__FILE__)))
require 'cisco/common'
require 'cisco/telnet'
require 'cisco/ssh'
require 'cisco/base'
57 changes: 57 additions & 0 deletions lib/cisco/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Cisco

class CiscoError < StandardError
end

class Base

attr_reader :transport

def initialize(options)
@transport = options[:transport] || :telnet
options[:prompt] ||= /[#>]\s?\z/n
options[:password] ||= ""
options[:autoinit] ||= true
@transport = Cisco.const_get(Cisco.constants.find {|const| const =~ Regexp.new(@transport.to_s, Regexp::IGNORECASE)}).new(options)
extra_init("terminal length 0")
end

def host
@transport.host
end

def password
@transport.password
end

def prompt
@transport.prompt
end

def clear_cmd
@transport.clear_cmd
end

def extra_init(*args)
@transport.extra_init(*args)
end

def clear_init
@transport.clear_init
end

def cmd(*args)
@transport.cmd(*args)
end

def run(&block)
@transport.run(&block)
end

def enable(*args)
@transport.enable(*args)
end

end

end
29 changes: 29 additions & 0 deletions lib/cisco/common.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Cisco

module Common

attr_accessor :host, :password, :prompt

def enable(password, pwprompt = nil)
@pwprompt = pwprompt || @pwprompt
old = @prompt
cmd("enable", @pwprompt)
cmd(password, old)
end

def extra_init(*args)
cmd(*args)
@extra_init << @cmdbuf.pop
end

def clear_init
@extra_init = []
end

def clear_cmd
@cmdbuf = []
end

end

end
Loading

0 comments on commit 3ff7580

Please sign in to comment.