pepepect在脚本结束前超时

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

我正在使用pexpect使用ssh连接到远程服务器。

以下代码有效,但我必须使用time.sleep进行延迟。

特别是当我发送命令以在远程服务器上运行脚本时。

该脚本最多需要运行一分钟,如果我不使用60秒的延迟,那么该脚本将过早结束。

当我使用sftp下载文件时,出现相同的问题。如果文件很大,则它会部分下载。

是否有一种不使用延迟的控制方式?

#!/usr/bin/python3
import pexpect
import time
from subprocess import call

siteip = "131.235.111.111"
ssh_new_conn = 'Are you sure you want to continue connecting'
password = 'xxxxx'

child = pexpect.spawn('ssh admin@' + siteip)
time.sleep(1)
child.expect('admin@.* password:')
child.sendline('xxxxx')
time.sleep(2)
child.expect('admin@.*')
print('ssh to abcd - takes 60 seconds')
child.sendline('backuplog\r')
time.sleep(50)
child.sendline('pwd')
pexpect
1个回答
0
投票

许多pexpect函数带有一个可选的timeout=关键字,而您在spawn()中指定的关键字则设置了默认值。例如

child.expect('admin@',timeout=70)

您可以使用值None永不超时。

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