Skip to content

Instantly share code, notes, and snippets.

@KINGSABRI
Created April 26, 2013 22:29
Show Gist options
  • Save KINGSABRI/5470878 to your computer and use it in GitHub Desktop.
Save KINGSABRI/5470878 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'net/ssh'
host = "192.168.1.2"
user = "user"
pass = "password"
su_user = "boss"
#commands = ["sudo bash" , "whoami" , "cd /tmp/" , "pwd" , "echo 'Ruby from LUA!!' > ruby.txt" , "cat ruby.txt", "exit" , "exit"]
commands = ["sudo bash" ,
"awk -F: '$3 >= 500 {print $1,$3,$6}' /etc/passwd" ,
"lastlog -u emerg | grep -v Username | awk '{print $6\"-\"$5\"-\"$9\" @ \"$7\"(\"$8\")\"}'" ,
"/sbin/ip addr | grep -i inet | grep -v -e inet6 -e 127.0.0.1 | awk '{print $2}'" , "exit" , "exit"]
#commands = ["/sbin/ifconfig -a" , "hostname" , "exit" , "exit"]
#Net::SSH.start(host, user , :password => pass , :port => 15000 , :timeout => 7 , :verbose => :debug) do |ssh|
data = ""
# Start SSH connection
Net::SSH.start(host, user , :password => pass , :port => 15000 , :timeout => 7) do |ssh|
# Open Session the SSH connection
ssh.open_channel(type="session") do |session|
# Open PTY(Pseudo-tty)
session.request_pty(:modes => { Net::SSH::Connection::Term::ECHO => 0 }) do |pty , success|
commands.each do |cmd|
p session.send_data("#{cmd}\n")
sleep 0.1
# Open Shell
session.send_channel_request("shell") do |shell, success|
session.on_extended_data do |ch, type, data|
puts "got stderr: #{data.inspect}"
end
session.on_data do |ch1, data_|
sleep 0.3
data << data_
#puts data
#$stdout.print data
#puts "#{data}\n"
#puts "---in on_data---"
end
end # end of shell
end
end # end of pty
# Tell me that Session closed successfully!
session.on_close { puts "#--> Closing Session\t[Done]\n\n" }
end # end of session
ssh.loop 0.1
end
puts "#--> Data is\n"
info = data.split("[")
p data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment