public
Last active

Tool I use to log into cisco or F5 devices.

  • Download Gist
ssh.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.