I’m trying to connect, using Net::SSH, to a server that immediately after
login executes a script that requires input from user. The user has to enter “1” or “2” and will receive some data via in the terminal afterwards.
My problem is that, although I am able to connect, I can not figure out a way to send “1\n” to the server and to receive the output.
The following code stops at “INFO — net.ssh.connection.session[80906b74]: channel_open_confirmation: 0 0 0 32768”.
Using channel.exec( “1\n” ) instead of channel.send_data unsurprisingly does not work either.
Net::SSH.start('host', 'user', :password => "pass", :auth_methods => ["password"], :verbose => :debug) do |session|
session.open_channel do |channel|
channel.on_data do |ch, data|
STDOUT.print data
end
channel.send_data( "1\n")
end
session.loop
end
Any ideas, anyone?
Thanks in advance
Can you verify that your
send_datacall is happening after you get the prompt from the remote server? Try constructing achannel.on_datablock around yoursend_datacall so that you can verify that you get the expected prompt from the server before you send a response.You might not want to be using
exechere. From the docs for Net::SSH::Connection::Channel:You are wanting to send a text string to reply to a prompt, not invoke a command. The docs show
execbeing used to send full CLI commands like “ls -l /home”.Instead,
send_datais probably what you want. The docs show it used to send arbitrary text such aschannel.send_data("the password\n"). Note, however, this sentence in the docs:You might want to take a look at
channel.request_pty. It appears to be designed for interaction with a console-based application.If you are trying to (in essence) script an SSH session that you would normally do manually, you may find it easier to use an
expect-like interface (for example, a gem like sshExpect might be worth a try).