【问题标题】:python pexpect does not launch my ssh tunnelpython pexpect 没有启动我的 ssh 隧道
【发布时间】:2019-02-21 14:20:32
【问题描述】:

我有这个脚本(称为隧道):

(ip server 替换为 ssh_server_ip)

ssh -ND 5545 -p 443 -vvv user@ssh_server_ip

我尝试通过 python pexpect 运行它。

import pexpect 
import time

p = pexpect.run('./tunnel')
#does not work with spawn
#p = pexpect.spawn('./tunnel') 
p.expect('password: ')
time.sleep(2)
p.sendline('pswd')

当我运行脚本时,与服务器建立了连接,但持续时间不长(几秒钟)

ssh        8872    root    3u  IPv4 510801      0t0  TCP 192.168.1.36:46328->ssh_server_ip:https (ESTABLISHED)

如果 ssh 隧道完全建立,我应该有以下内容:

ssh        8130    root    3u  IPv4 503864      0t0  TCP 192.168.1.36:46326->ssh_server_ip:https (ESTABLISHED)
ssh        8130    root    4u  IPv6 505215      0t0  TCP [::1]:5545 (LISTEN)
ssh        8130    root    5u  IPv4 505216      0t0  TCP 127.0.0.1:5545 (LISTEN)

我应该使用 bash expect 代替 python pexpect 吗?

谢谢各位!

【问题讨论】:

  • 当然输出没有错误。

标签: python ssh pexpect


【解决方案1】:

你需要这样写:

p.sendline('password')
p.expect(pexpect.EOF)

否则 Python 脚本将在发送密码后立即退出,这反过来会杀死 ssh 命令。

【讨论】:

  • 好的,现在又出现了一个问题;看起来服务器需要被炸毁(我需要一遍又一遍地尝试连接)才能接受连接。在我的 python 脚本中,如果服务器不接受连接,则会出现错误。所以我需要在没有连接的情况下循环脚本,但我不知道在这种情况下使用什么条件。
  • 听起来像是一个不同的问题。请提出一个新问题。
猜你喜欢
  • 2017-07-15
  • 1970-01-01
  • 2013-08-12
  • 2019-07-15
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 2021-05-23
  • 1970-01-01
相关资源
最近更新 更多