Hi,
I''m occasionally experiencing hangs on Net:SSH:Session#exec!
My setup: I''m using four Linux machines:
1: my testserver which will be executing the Ruby code
2: a gateway (linux router)
3: a linux computer
4: an embedded linux machine (telnet only)
To execute commands on a shell on the linux computer and embedded
linux machine, I use SSH connection and local port forwarding. While
alternatively executing commands on the embedded machine and the linux
computer, the commands on the linux computer hang.
I noticed that the command in question is actually not send to the
machine, but that the Net:SSH library assumes that it is, and waits
for the return indefinitely. Using the Net:SSH:Session#exec method
stops the hangs, but then the commands are not executed.
I have the feeling this is a timing issue, maybe I''m using the library
wrongly :(
Code below.
Best regards,
Nicky
--------- used versions:
OS: Ubuntu Fiesty
ruby: 1.8.5
net:ssh: 2.0.1
-------- code:
require ''net/ssh''
require ''net/telnet''
require ''thread''
def test1
# set up ssh connectoin to the gateway
puts "starting connection to gateway"
gw = Net::SSH.start("10.0.0.200",
"####",
:password => "####")
# start a ssh tunnel for the ssh connection to the cpe
puts "starting tunnel to cpe"
Thread.new do
gw.forward.local(8022, "192.168.253.38", 22)
gw.loop {true}
end
# set up ssh connection to the cpe
puts "starting connection to cpe"
cpe = Net::SSH.start("127.0.0.1",
"####",
:password => "####",
:port => 8022)
# start a ssh tunnel for the telnet connection to the modem
puts "starting tunnel to modem"
Thread.new do
cpe.forward.local(8023, "192.168.1.1", 23)
cpe.loop {true}
end
# set up a telnet connection to the modem
puts "starting connection to modem"
modem = Net::Telnet.new("Host" => "127.0.0.1",
"Port" => 8023)
modem.login("Name" => "####",
"Password" => "####")
command = "uname -a"
puts "command= " + command
puts modem.cmd(command)
puts cpe.exec!(command)
modem.close
cpe.forward.cancel_local(8023)
# start a ssh tunnel for the telnet connection to the modem
puts "starting tunnel to modem"
Thread.new do
cpe.forward.local(8023, "192.168.1.1", 23)
cpe.loop {true}
end
# set up a telnet connection to the modem
puts "starting connection to modem"
modem = Net::Telnet.new("Host" => "127.0.0.1",
"Port" => 8023)
modem.login("Name" => "####",
"Password" => "####")
command = "cat /proc/uptime"
puts "command= " + command
puts modem.cmd(command)
puts cpe.exec!(command)
modem.close
cpe.forward.cancel_local(8023)
# start a ssh tunnel for the telnet connection to the modem
puts "starting tunnel to modem"
Thread.new do
cpe.forward.local(8023, "192.168.1.1", 23)
cpe.loop {true}
end
# set up a telnet connection to the modem
puts "starting connection to modem"
modem = Net::Telnet.new("Host" => "127.0.0.1",
"Port" => 8023)
modem.login("Name" => "####",
"Password" => "####")
command = "ls -l"
puts "command= " + command
puts modem.cmd(command)
puts cpe.exec!(command)
# close it all up
modem.close
cpe.forward.cancel_local(8023)
cpe.close
gw.forward.cancel_local(8022)
gw.close
end
test1