使用 Pexpect 停止通过 SSH 返回

发布于 2024-08-30 22:12:43 字数 707 浏览 7 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(2

玻璃人 2024-09-06 22:12:43

启动 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.

她比我温柔 2024-09-06 22:12:43

您到另一台机器的实例是 ppsendline 您在另一台机器上想要的内容并 p.expect 结果。在所概述的情况下

p.sendline("pwd && hostname")
p.expect("-bash-3.2") # although its better to set the prompt yourself so that this can be ported to any machine
response = p.before
print "received response [[" + response + "]]"

尝试一下。还可以尝试使用 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

p.sendline("pwd && hostname")
p.expect("-bash-3.2") # although its better to set the prompt yourself so that this can be ported to any machine
response = p.before
print "received response [[" + response + "]]"

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文