如何使用pexpect在后台运行东西?

2024-04-26 01:05:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在脚本中使用'Pexpect'库,并希望在此之后执行一些操作。我的工作如下:

def child():
        pexpect.spawn("ssh tranzmeo@192.168.21.110")
        child.expect(".*password.*")
        child.sendline('tranzmeo1@#')
        print(child.before)
        child.interact()
        return

这是我从另一个文件(我称之为training)调用的函数。当我运行training时,会调用child(),但它会立即停止程序并进入tranzmeo控制台,这是我不想要的。我需要ssh进入它,在tranzmeo的后台做一些操作。如何使用pexpect实现这一点?你知道吗

我的operation如下(仅供参考):

def scp(tensorflow_file_location_temp ,tensorflow_file_loc)
        child = pexpect.spawn("scp -r " + tensorflow_file_location_temp + "models@192.168.21.117:" + tensorflow_file_location )
        child.expect(".*password.*")
        child.sendline('Tranzmeo1@#')
        child.interact()

Tags: childtensorflowdeftraininglocationpasswordsshfile
1条回答
网友
1楼 · 发布于 2024-04-26 01:05:41

.interact()gives control of the child process to the interactive user (the human at the keyboard). Keystrokes are sent to the child process, and the stdout and stderr output of the child process is printed.

如果你不想那样,就别说了!您想继续使用从pexpect.spawn("ssh tranzmeo@192.168.21.110")返回的对象(您发布的代码没有将其分配给child,我假设这是一个复制粘贴错误):

child.sendline("scp -r " + tensorflow_file_location_temp + "models@192.168.21.117:" + tensorflow_file_location)

但是您应该设置ssh密钥,这样您就可以在没有密码和simply running ^{}的情况下做您想做的事情。你知道吗

相关问题 更多 >