使用 Pexpect 停止通过 SSH 返回
我正在尝试使用 pexpect ssh 进入计算机,但我不想返回到原来的计算机。我的代码是:
#!/usr/bin/python2.6
import pexpect, os
def ssh():
# Logs into computer through SSH
ssh_newkey = 'Are you sure you want to continue connecting'
# my ssh command line
p=pexpect.spawn('ssh [email protected]')
i=p.expect([ssh_newkey,'password:',pexpect.EOF])
p.sendline("password")
i=p.expect('-bash-3.2')
print os.getcwd()
ssh()
这允许我通过 ssh 进入计算机,但是当我运行 os.getcwd() 时,pexpect 将我返回到原始计算机。你看,我想 ssh 到另一台计算机并使用他们的环境,而不是使用 pexpect 拖动我的环境。任何人都可以建议如何使其工作或替代方法。
谢谢
I am trying to use pexpect to ssh into a computer but I do not want to return back to the original computer. The code I have is:
#!/usr/bin/python2.6
import pexpect, os
def ssh():
# Logs into computer through SSH
ssh_newkey = 'Are you sure you want to continue connecting'
# my ssh command line
p=pexpect.spawn('ssh [email protected]')
i=p.expect([ssh_newkey,'password:',pexpect.EOF])
p.sendline("password")
i=p.expect('-bash-3.2')
print os.getcwd()
ssh()
This allows me to ssh into the computer but when I run the os.getcwd()
the pexpect has returned me to the original computer. You see I want to ssh into another computer and use their environment not drag my environment using pexpect. Can anyone suggest how to get this working or an alternative way.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
启动 ssh 的进程永远不会离开它运行的计算机。当您通过 ssh 连接到另一台计算机时,您会在那里启动一个新进程。该过程是一个完全独立的事物,一个单独的程序来运行。如果您想在远程计算机上执行任何操作,则必须通过连接发送要执行的命令,或者复制要运行的程序并远程执行它。
The process that launches
ssh
is never going to leave the computer it runs on. When you ssh into another computer, you start a new process there. That process is an entirely separate thing, a separate program to run. If you want to do anything on the remote machine, you have to either send the commands to execute over the connection, or copy over the program you want to run and execute it remotely.您到另一台机器的实例是 ppsendline 您在另一台机器上想要的内容并 p.expect 结果。在所概述的情况下
尝试一下。还可以尝试使用 pxssh 模块来将 ssh 与 python 结合使用。该模块使用 pexpect 并具有其中的所有方法来准确执行您想要的操作
your instance to the other machine is p. p.sendline what you want on the other machine and p.expect the result. in the case outlined
Try that. Also try module pxssh to use ssh with python. This module uses pexpect and has all of the methods in it to do exactly what you want here