使用 ruby 和 Net::SSH 在第一个命令之前响应 SSH 提示
我正在尝试使用 Net::SSH 连接到紧接着的服务器 登录执行需要用户输入的脚本。用户必须输入“1”或“2”,然后终端将收到一些数据。
我的问题是,虽然我能够连接,但我无法找到将“1\n”发送到服务器并接收输出的方法。
以下代码在“INFO -- net.ssh.connection.session[80906b74]:channel_open_confirmation:0 0 0 32768”处停止。
使用channel.exec( "1\n" ) 而不是channel.send_data 不出所料也不起作用。
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
有什么想法吗?
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您能否在收到远程服务器的提示后验证您的
send_data
调用是否正在发生?尝试在send_data
调用周围构建一个channel.on_data
块,以便您可以在发送响应之前验证是否从服务器获得了预期的提示。您可能不想在此处使用
exec
。来自 Net::SSH::Connection::Channel 的文档:您想要发送文本字符串来回复提示,而不是调用命令。文档显示
exec
用于发送完整的 CLI 命令,例如“ls -l /home”。相反,
send_data
可能就是您想要的。文档显示它用于发送任意文本,例如channel.send_data("the password\n")
。但请注意文档中的这句话:您可能想看看
channel.request_pty
。它似乎是为与基于控制台的应用程序交互而设计的。如果您尝试(本质上)编写通常手动执行的 SSH 会话脚本,您可能会发现使用类似于
expect
的界面(例如,像 sshExpect 可能值得一试)。Can you verify that your
send_data
call is happening after you get the prompt from the remote server? Try constructing achannel.on_data
block around yoursend_data
call 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
exec
here. 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
exec
being used to send full CLI commands like "ls -l /home".Instead,
send_data
is 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).谢谢大家的指点。我已经能够解决这个问题了——除了使用channel.request_pty之外,还需要请求一个shell。以下最终按预期工作:
Thank you all for the pointers. I have been able to put my finger on the problem – besides using channel.request_pty it was also necessary to request a shell. The following finally works as expected:
我对 Net::SSH 库不是很熟悉,所以我本身无法提供帮助,但听起来您可以使用 Capistrano 实现您想要的目标。
例如,我有一个 capistrano 任务,它连接到远程服务器,运行一个需要输入的命令,然后继续。 Capistrano 负责远程 I/O。也许这对你来说是一个解决方案?
希望有帮助!
I'm not terribly familiar with the Net::SSH libs so I can't help with that per-se but it sounds like you could achieve what you want using Capistrano.
For example I have a capistrano task which connects to a remote server, runs a command which expects input and then continues. Capistrano takes care of the remote i/o. Maybe that could be a solution for you?
Hope it helps!
如果我在 shell 中执行
"1\n"
,我得到的答复是:bash: 1: command not find
如果我执行
echo "1"
code> 我得到:1
您确定要尝试执行您正在发送的文本吗?也许您正在寻找类似的东西:
If I execute
"1\n"
in a shell the reply I get is:bash: 1: command not found
If I execute
echo "1"
I get:1
Are you sure you want to try to execute the text you are sending? Maybe you were looking for something like:
我不熟悉该库,但在 SSH 上你可以打开多个通道。也许服务器只响应第一个默认通道,如果您打开另一个默认通道,您会得到一个新的 shell。
I'm not proficient with that lib, but on SSH you can open multiple channels. Maybe the server only responds to the first default channel and if you open another one you get a fresh shell.