#!/usr/bin/ruby
require 'rubygems'
require 'net/ssh'
host = "10.20.30.45"
user = "king"
pass = "Badp@ssw0rd"
commands = ["cd /tmp/" , "pwd" , "echo 'Ruby' > ruby.txt" , "cat ruby.txt"]
Net::SSH.start(host, user , :password => pass , :port => 15000 , :timeout => 7 ) do |ssh|
ssh.open_channel(type="session") do |channel|
channel.request_pty(:modes => { Net::SSH::Connection::Term::ECHO => 0 }) do |pty, success|
commands.each do |cmd|
channel.on_data do |ch1, data|
puts "Command is: #{cmd}"
channel.send_data(cmd + "\n")
sleep 0.3
puts data
end
end
puts channel.exec("sudo bash")
end # end of pty
end # end of channel
ssh.loop 0.1
end
#Command is: cat ruby.txt
#]0;root@pdbosl02:~
#Command is: cat ruby.txt
#
#Command is: cat ruby.txt
#[root@pdbosl02 ~]#
#Command is: cat ruby.txt
#cat: ruby.txt: No such file or directory
#]0;root@pdbosl02:~
#Command is: cat ruby.txt
#[root@pdbosl02 ~]#
#Command is: cat ruby.txt
#cat: ruby.txt
#Command is: cat ruby.txt
#: No such file or directory
#Command is: cat ruby.txt
#]0;root@pdbosl02:~
#Command is: cat ruby.txt
#
#Command is: cat ruby.txt
#[root@pdbosl02 ~]#
#Command is: cat ruby.txt
#cat: ruby.txt