捕捉 pexpect 中的死亡过程

发布于 2024-10-31 04:18:42 字数 164 浏览 0 评论 0原文

我正在写一些 pexpect 的东西,基本上是通过 telnet 发送命令。

但是,我的 telnet 会话可能会终止(由于网络问题、电缆被拉断等)。

如何初始化 telnet 会话,以便在它终止时我可以捕获它并告诉它重新连接,然后继续执行它所在的代码。

这可能吗?

I'm writing some pexpect stuff that's basically sending commands over telnet.

But, it's possible that my telnet session could die (due to networking problems, a cable getting pulled, whatnot).

How do I initialize a telnet session such that, if it dies, I can catch it and tell it to reconnect and then continue execution of the code where it was at.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坚持沉默 2024-11-07 04:18:43

我这样做了,它起作用了:

def telnet_connect():
    print "Trying to connect via telnet..."
    telnet_connecting = pexpect.spawn('telnet localhost 10023', timeout=2)
    while 1:
        try:
            telnet_connecting.expect('login: ')
            break
        except:
            telnet_connecting = telnet_connect()
            break
    return telnet_connecting

递归 FTW?

I did this, and it worked:

def telnet_connect():
    print "Trying to connect via telnet..."
    telnet_connecting = pexpect.spawn('telnet localhost 10023', timeout=2)
    while 1:
        try:
            telnet_connecting.expect('login: ')
            break
        except:
            telnet_connecting = telnet_connect()
            break
    return telnet_connecting

Recursion FTW?

拥有 2024-11-07 04:18:42

恕我直言,您通常最好使用当前维护的库,例如 exscript< /a> 或 telnetlib,但是 < 中的高效咒语代码>pexpect是:

import pexpect as px

cmds = ['cmd1', 'cmd2', 'cmd3']
retcode = -1
while (retcode<10):
    if (retcode<2):
        child = px.spawn('telnet %s %s' % (ip_addr,port))
    lregex = '(sername:)'            # Insert regex for login prompt here
    pregex = '(prompt1>)|(prompt2$)' # Insert your prompt regex here
    # retcode = 0 for px.TIMEOUT, 1 for px.EOF, 2 for lregex match...
    retcode = child.expect([px.TIMEOUT, px.EOF, lregex, pregex],timeout = 10)
    if (retcode==2):
        do_login(child)  # Build a do_login() method to send user / passwd
    elif (2<retcode<10) and (len(cmds)>0):
        cmd = cmds.pop(0)
        child.sendline(cmd)
    else:
        retcode = 10

IMHO, you're normally better-off with a currently-maintained library like exscript or telnetlib, but the efficient incantation in pexpect is:

import pexpect as px

cmds = ['cmd1', 'cmd2', 'cmd3']
retcode = -1
while (retcode<10):
    if (retcode<2):
        child = px.spawn('telnet %s %s' % (ip_addr,port))
    lregex = '(sername:)'            # Insert regex for login prompt here
    pregex = '(prompt1>)|(prompt2$)' # Insert your prompt regex here
    # retcode = 0 for px.TIMEOUT, 1 for px.EOF, 2 for lregex match...
    retcode = child.expect([px.TIMEOUT, px.EOF, lregex, pregex],timeout = 10)
    if (retcode==2):
        do_login(child)  # Build a do_login() method to send user / passwd
    elif (2<retcode<10) and (len(cmds)>0):
        cmd = cmds.pop(0)
        child.sendline(cmd)
    else:
        retcode = 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文