-
-
Save heathseals/33df4498a9356b0de281 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 'rubygems' | |
require 'net/ssh' | |
require 'net/ssh/telnet' | |
class SSH | |
attr_accessor :errors | |
def initialize(creds) | |
begin | |
@ssh_session = Net::SSH.start(creds[:host], creds[:user], :password => creds[:password], :keys => []) | |
@ssh = Net::SSH::Telnet.new("Session" => @ssh_session, "Prompt" => creds[:prompt]) | |
@errors = false | |
rescue Exception => e | |
@errors = e | |
end | |
end | |
def cmd(command) | |
@ssh.cmd(command) | |
end | |
def close | |
@ssh.close | |
end | |
end | |
class Cisco_ssh < SSH | |
attr_accessor :config, :users | |
def initialize(creds) | |
@host, @users = creds[:host], [] | |
creds[:prompt] = /.*>|.*#/ | |
super(creds) | |
unless @errors | |
@ssh.cmd("en\r#{creds[:enable]}") | |
@ssh.cmd("term len 0") | |
@config = @ssh.cmd("show run") | |
get_users | |
end | |
end | |
def enable(pass) | |
@ssh.cmd("en\r#{pass}") | |
end | |
def termlen=(length) | |
@ssh.cmd("term len #{length}") | |
end | |
def close | |
termlen=24 | |
@ssh.close | |
end | |
def update_config | |
@config = @ssh.cmd("show run") | |
get_users | |
end | |
private | |
def get_users | |
@config.lines.each do |line| | |
if line.include? "username" | |
user = {} | |
user[:username] = line.split[1] | |
user[:password] = line.match(/(password|secret).\d.*/)[0].split[2] | |
@users.push(user) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment