python pexpect没有启动我的ssh隧道

问题描述 投票:0回答:1

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

(ip服务器替换为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吗?

thanx伙计们!

python ssh pexpect
1个回答
1
投票

你需要这样写:

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

否则,Python脚本将在发送密码后立即退出,而密码又会终止ssh命令。

© www.soinside.com 2019 - 2024. All rights reserved.