Skip to content

Instantly share code, notes, and snippets.

@KINGSABRI
Created April 24, 2012 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KINGSABRI/2480734 to your computer and use it in GitHub Desktop.
Save KINGSABRI/2480734 to your computer and use it in GitHub Desktop.
Switch-sessions
#!/usr/bin/ruby
##
## just to test remote cmds on the switches
##
#
# gem install net-scp colorize net-ssh net-scp logger
#
require 'rubygems'
require 'colorize'
require 'socket'
require 'logger'
mark_Red = "[+]".red
mark_Green = "[+]".green
mark_yellow = "[+]".yellow
##################################
def logger
log = Logger.new('log.txt', 10, 1024000) # Filename: log.txt , Number of rotation: 10 , Rotation Size= 125MB
end
############################
# HP Switch connection
############################
host = 'xx.xx.xx.xx'
user = "test"
pass = "test"
port = 23
hpcmd = ["configure terminal" , "interface 17 enable" , "interface 17 disable" , "show interfaces brief 17" , "logout"]
s = Net::Telnet::new("Host" => host ,"Port" => port ,"Timeout" => 130)
sleep 0.4
s.write "\r\n"
s.puts(user)
s.puts(pass)
s.cmd(hpcmd[0]) do |l0|
print l0
s.cmd(hpcmd[1]) do |l1|
print l1
s.cmd(hpcmd[2]) do |l2|
print l2
s.cmd(hpcmd[4]) do |l3|
print l3
if l3.inspect.include? "Do you want to log out"
s.print "y"
end
if l3.inspect.include? "Do you want to save current configuration"
s.print "y"
end
end
end
end
end
############################
# Huawei Switch connection
############################
# host = 'xx.xx.xx.xx'
# user = "test"
# pass = "test"
# port = 23
# hcmd = ["system-view" , "Int GigabitEthernet1/0/42" , "Un Shut" , "Shut" , "Dis b int GigabitEthernet1/0/42" , "quit"]
# # hucmd = ["system-view" , "Int GigabitEthernet1/0/42" , "Shut" , "Dis b int GigabitEthernet1/0/42"] #ifdown
#
# s = Net::Telnet::new("Host" => host ,"Port" => port ,"Timeout" => 130)
# sleep 0.5
# s.puts(user) { |u| print u }
# s.puts(pass) do |p|
# print p
# if p.inspect.include? "Invalid password"
# logger.info "Wrong Password!"
# exit
# else
# logger.info "Logged Successfully!"
# end
# end
#
# s.cmd(hcmd[0]) do |l0| # Priv escalation
# print l0
# s.cmd(hcmd[1]) do |l1| # Interface config level
# print l1
# s.cmd(hcmd[2]) do |l2| # ifup/ifdown s.cmd(sw1.type.cmd[])
# print l2
# s.cmd(hcmd[4]) do |l3| # ifstatus
# print l3
# s.cmd(hcmd[5]) do |q| # Quit
# print q # Quit l3
# s.puts "quit" # Quit l2
# end # Quit
# end # l3
# s.cmd(hcmd[5]) do |q| # Quit
# s.puts "quit" # Quit l1,l0
# end
# end # l2
# end # l1
# end # l0
# # s.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment