S
saga99x
I try to use Net::SSH to exec command on remote host. A session was
openned and a few command should be exec and get the output.
It always failed to get the output of second cmd no matter what
command it is. Here is the code
def do_cmd(s, cmd) #session, command
s.open_channel do |channel|
channel.on_data do |ch, data|
puts "#{data}"
end
channel.exec cmd, true
end
end
session = Net::SSH.start(h,u,p) #hostname, userid, passwd
do_cmd(session, "version") # first cmd
do_cmd(session, "version") # 2nd cmd
session.loop
session.close
Here is the error:
/usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/packet-stream.rb:
202:in `<<': can't convert nil into String (TypeError)
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
packet-stream.rb:202:in `read'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
packet-stream.rb:153:in `get'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
packet-stream.rb:147:in `synchronize'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
packet-stream.rb:147:in `get'
from /usr/local/lib/ruby/site_ruby/1.8/needle/lifecycle/
proxy.rb:60:in `__send__'
from /usr/local/lib/ruby/site_ruby/1.8/needle/lifecycle/
proxy.rb:60:in `method_missing'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
session.rb:247:in `wait_for_message'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
session.rb:242:in `loop'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
session.rb:242:in `wait_for_message'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/connection/
driver.rb:148:in `process'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/connection/
driver.rb:138:in `loop'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/session.rb:
164:in `loop'
Could anyone give me a hint?
openned and a few command should be exec and get the output.
It always failed to get the output of second cmd no matter what
command it is. Here is the code
def do_cmd(s, cmd) #session, command
s.open_channel do |channel|
channel.on_data do |ch, data|
puts "#{data}"
end
channel.exec cmd, true
end
end
session = Net::SSH.start(h,u,p) #hostname, userid, passwd
do_cmd(session, "version") # first cmd
do_cmd(session, "version") # 2nd cmd
session.loop
session.close
Here is the error:
/usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/packet-stream.rb:
202:in `<<': can't convert nil into String (TypeError)
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
packet-stream.rb:202:in `read'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
packet-stream.rb:153:in `get'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
packet-stream.rb:147:in `synchronize'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
packet-stream.rb:147:in `get'
from /usr/local/lib/ruby/site_ruby/1.8/needle/lifecycle/
proxy.rb:60:in `__send__'
from /usr/local/lib/ruby/site_ruby/1.8/needle/lifecycle/
proxy.rb:60:in `method_missing'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
session.rb:247:in `wait_for_message'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
session.rb:242:in `loop'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/transport/
session.rb:242:in `wait_for_message'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/connection/
driver.rb:148:in `process'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/connection/
driver.rb:138:in `loop'
from /usr/local/lib/ruby/site_ruby/1.8/net/ssh/session.rb:
164:in `loop'
Could anyone give me a hint?