我试图使用Net :: SSH远程执行命令。我在Windows上执行脚本是成功的,但是在我的Unix环境中,我能够连接,但即使是最简单的命令(例如ls,echo)也无法执行。
连接成功:
ssh = Net::SSH.start('myhost.network.etc', 'myuser', password: 'mypwd', verbose: :debug); nil
D, [2016-09-02T15:16:58.042000 #12124740] DEBUG -- net.ssh.transport.session[8ce]: establishing connection to myhost.network.etc:22
D, [2016-09-02T15:16:58.343000 #12124740] DEBUG -- net.ssh.transport.session[8ce]: connection established
I, [2016-09-02T15:16:58.345000 #12124740] INFO -- net.ssh.transport.server_version[8d0]: negotiating protocol version
D, [2016-09-02T15:16:58.414000 #12124740] DEBUG -- net.ssh.transport.server_version[8d0]: remote is `SSH-2.0-OpenSSH_6.0'
D, [2016-09-02T15:16:58.416000 #12124740] DEBUG -- net.ssh.transport.server_version[8d0]: local is `SSH-2.0-Ruby/Net::SSH_2.9.2 java'
D, [2016-09-02T15:16:58.434000 #12124740] DEBUG -- tcpsocket[8d2]: read 976 bytes
D, [2016-09-02T15:16:58.438000 #12124740] DEBUG -- tcpsocket[8d2]: received packet nr 0 type 20 len 972
I, [2016-09-02T15:16:58.441000 #12124740] INFO -- net.ssh.transport.algorithms[8d4]: got KEXINIT from server
I, [2016-09-02T15:16:58.445000 #12124740] INFO -- net.ssh.transport.algorithms[8d4]: sending KEXINIT
D, [2016-09-02T15:16:58.452000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 0 type 20 len 1564
D, [2016-09-02T15:16:58.453000 #12124740] DEBUG -- tcpsocket[8d2]: sent 1568 bytes
I, [2016-09-02T15:16:58.455000 #12124740] INFO -- net.ssh.transport.algorithms[8d4]: negotiating algorithms
D, [2016-09-02T15:16:58.463000 #12124740] DEBUG -- net.ssh.transport.algorithms[8d4]: negotiated:
* kex: diffie-hellman-group-exchange-sha1
* host_key: ssh-rsa
* encryption_server: aes128-cbc
* encryption_client: aes128-cbc
* hmac_client: hmac-sha1
* hmac_server: hmac-sha1
* compression_client: none
* compression_server: none
* language_client:
* language_server:
D, [2016-09-02T15:16:58.464000 #12124740] DEBUG -- net.ssh.transport.algorithms[8d4]: exchanging keys
D, [2016-09-02T15:16:58.469000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 1 type 34 len 20
D, [2016-09-02T15:16:58.471000 #12124740] DEBUG -- tcpsocket[8d2]: sent 24 bytes
D, [2016-09-02T15:16:58.484000 #12124740] DEBUG -- tcpsocket[8d2]: read 152 bytes
D, [2016-09-02T15:16:58.488000 #12124740] DEBUG -- tcpsocket[8d2]: received packet nr 1 type 31 len 148
D, [2016-09-02T15:16:58.519000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 2 type 32 len 140
D, [2016-09-02T15:16:58.522000 #12124740] DEBUG -- tcpsocket[8d2]: sent 144 bytes
D, [2016-09-02T15:16:58.547000 #12124740] DEBUG -- tcpsocket[8d2]: read 720 bytes
D, [2016-09-02T15:16:58.551000 #12124740] DEBUG -- tcpsocket[8d2]: received packet nr 2 type 33 len 700
D, [2016-09-02T15:16:58.588000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 3 type 21 len 20
D, [2016-09-02T15:16:58.591000 #12124740] DEBUG -- tcpsocket[8d2]: sent 24 bytes
D, [2016-09-02T15:16:58.594000 #12124740] DEBUG -- tcpsocket[8d2]: received packet nr 3 type 21 len 12
D, [2016-09-02T15:16:58.604000 #12124740] DEBUG -- net.ssh.authentication.session[8d6]: beginning authentication of `myuser'
D, [2016-09-02T15:16:58.723000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 4 type 5 len 28
D, [2016-09-02T15:16:58.725000 #12124740] DEBUG -- tcpsocket[8d2]: sent 52 bytes
D, [2016-09-02T15:16:58.727000 #12124740] DEBUG -- tcpsocket[8d2]: read 52 bytes
D, [2016-09-02T15:16:58.730000 #12124740] DEBUG -- tcpsocket[8d2]: received packet nr 4 type 6 len 28
D, [2016-09-02T15:16:58.784000 #12124740] DEBUG -- net.ssh.authentication.session[8d6]: trying none
D, [2016-09-02T15:16:58.788000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 5 type 50 len 44
D, [2016-09-02T15:16:58.797000 #12124740] DEBUG -- tcpsocket[8d2]: sent 68 bytes
D, [2016-09-02T15:16:58.801000 #12124740] DEBUG -- tcpsocket[8d2]: read 84 bytes
D, [2016-09-02T15:16:58.809000 #12124740] DEBUG -- tcpsocket[8d2]: received packet nr 5 type 51 len 60
D, [2016-09-02T15:16:58.811000 #12124740] DEBUG -- net.ssh.authentication.session[8d6]: allowed methods: publickey,password,keyboard-interactive
D, [2016-09-02T15:16:58.813000 #12124740] DEBUG -- net.ssh.authentication.methods.none[8d8]: none failed
D, [2016-09-02T15:16:58.817000 #12124740] DEBUG -- net.ssh.authentication.session[8d6]: trying publickey
D, [2016-09-02T15:16:58.824000 #12124740] DEBUG -- net.ssh.authentication.agent[8da]: connecting to ssh-agent
D, [2016-09-02T15:16:58.835000 #12124740] DEBUG -- net.ssh.authentication.methods.publickey[8dc]: trying publickey (ae:c3:2c:c6:af:87:aa:cf:07:44:4d:11:34:47:ce:9f)
D, [2016-09-02T15:16:58.842000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 6 type 50 len 348
D, [2016-09-02T15:16:58.844000 #12124740] DEBUG -- tcpsocket[8d2]: sent 372 bytes
D, [2016-09-02T15:16:58.854000 #12124740] DEBUG -- tcpsocket[8d2]: read 84 bytes
D, [2016-09-02T15:16:58.860000 #12124740] DEBUG -- tcpsocket[8d2]: received packet nr 6 type 51 len 60
D, [2016-09-02T15:16:58.864000 #12124740] DEBUG -- net.ssh.authentication.session[8d6]: allowed methods: publickey,password,keyboard-interactive
D, [2016-09-02T15:16:58.866000 #12124740] DEBUG -- net.ssh.authentication.session[8d6]: trying password
D, [2016-09-02T15:16:58.871000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 7 type 50 len 60
D, [2016-09-02T15:16:58.874000 #12124740] DEBUG -- tcpsocket[8d2]: sent 84 bytes
D, [2016-09-02T15:16:58.878000 #12124740] DEBUG -- tcpsocket[8d2]: read 36 bytes
D, [2016-09-02T15:16:58.894000 #12124740] DEBUG -- tcpsocket[8d2]: received packet nr 7 type 52 len 12
D, [2016-09-02T15:16:58.896000 #12124740] DEBUG -- net.ssh.authentication.methods.password[8de]: password succeeded
但是当我尝试执行任何命令时,net-ssh会话不会返回任何内容并无限期挂起:
ssh.exec! 'ls'
D, [2016-09-02T15:41:35.371000 #12124740] DEBUG -- tcpsocket[8d2]: queueing packet nr 8 type 90 len 44
如果我再打开第二个shell并终止脚本打开的ssh进程,我会得到以下回溯,但除了确认会话仍处于活动状态并等待回复之外,找不到任何有用的回复:
D, [2016-09-02T15:46:31.237000 #12124740] DEBUG -- tcpsocket[8d2]: read 0 bytes
IOError: closed stream
from org/jruby/RubyIO.java:1301:in `syswrite'
from org/jruby/ext/socket/RubyBasicSocket.java:134:in `send'
from /home/devinst/.gem/gems/net-ssh-2.9.2/lib/net/ssh/buffered_io.rb:99:in `send_pending'
from /home/devinst/.gem/gems/net-ssh-2.9.2/lib/net/ssh/connection/session.rb:245:in `postprocess'
from org/jruby/RubyArray.java:1613:in `each'
from /home/devinst/.gem/gems/net-ssh-2.9.2/lib/net/ssh/connection/session.rb:244:in `postprocess'
from /home/devinst/.gem/gems/net-ssh-2.9.2/lib/net/ssh/connection/session.rb:212:in `process'
from /home/devinst/.gem/gems/net-ssh-2.9.2/lib/net/ssh/connection/session.rb:170:in `loop'
from org/jruby/RubyKernel.java:1519:in `loop'
from /home/devinst/.gem/gems/net-ssh-2.9.2/lib/net/ssh/connection/session.rb:170:in `loop'
from /home/devinst/.gem/gems/net-ssh-2.9.2/lib/net/ssh/connection/channel.rb:269:in `wait'
from /home/devinst/.gem/gems/net-ssh-2.9.2/lib/net/ssh/connection/session.rb:365:in `exec!'
from (irb):19:in `evaluate'
from org/jruby/RubyKernel.java:1119:in `eval'
from org/jruby/RubyKernel.java:1519:in `loop'
from org/jruby/RubyKernel.java:1282:in `catch'
from org/jruby/RubyKernel.java:1282:in `catch'
from /home/devinst/jruby-1.7.9/bin/jirb:13:in `(root)'
只是为了确保没有阻止本地和远程服务器之间的SSH会话,我尝试使用Unix上的ssh
命令连接到远程服务器,它运行正常。
我不知道还有什么可以尝试。