<legend id='LnM6Q'><style id='LnM6Q'><dir id='LnM6Q'><q id='LnM6Q'></q></dir></style></legend>
  1. <i id='LnM6Q'><tr id='LnM6Q'><dt id='LnM6Q'><q id='LnM6Q'><span id='LnM6Q'><b id='LnM6Q'><form id='LnM6Q'><ins id='LnM6Q'></ins><ul id='LnM6Q'></ul><sub id='LnM6Q'></sub></form><legend id='LnM6Q'></legend><bdo id='LnM6Q'><pre id='LnM6Q'><center id='LnM6Q'></center></pre></bdo></b><th id='LnM6Q'></th></span></q></dt></tr></i><div id='LnM6Q'><tfoot id='LnM6Q'></tfoot><dl id='LnM6Q'><fieldset id='LnM6Q'></fieldset></dl></div>
    <tfoot id='LnM6Q'></tfoot>
    • <bdo id='LnM6Q'></bdo><ul id='LnM6Q'></ul>

    <small id='LnM6Q'></small><noframes id='LnM6Q'>

    1. 如何使用子进程模块与 ssh 交互

      时间:2023-07-21
      1. <legend id='HI682'><style id='HI682'><dir id='HI682'><q id='HI682'></q></dir></style></legend>

          <tbody id='HI682'></tbody>

            • <bdo id='HI682'></bdo><ul id='HI682'></ul>

              1. <small id='HI682'></small><noframes id='HI682'>

              2. <tfoot id='HI682'></tfoot>
                <i id='HI682'><tr id='HI682'><dt id='HI682'><q id='HI682'><span id='HI682'><b id='HI682'><form id='HI682'><ins id='HI682'></ins><ul id='HI682'></ul><sub id='HI682'></sub></form><legend id='HI682'></legend><bdo id='HI682'><pre id='HI682'><center id='HI682'></center></pre></bdo></b><th id='HI682'></th></span></q></dt></tr></i><div id='HI682'><tfoot id='HI682'></tfoot><dl id='HI682'><fieldset id='HI682'></fieldset></dl></div>

                本文介绍了如何使用子进程模块与 ssh 交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用子进程生成一个 ssh 子进程.

                I'm trying to spawn an ssh child process using subprocess.

                我正在 Windows 7 上开发 Python 2.7.6

                I'm working on Python 2.7.6 on Windows 7

                这是我的代码:

                from subprocess import *
                r=Popen("ssh sshserver@localhost", stdout=PIPE)
                stdout, stderr=r.communicate()
                print(stdout)
                print(stderr)
                

                输出:

                None
                

                stdout 应包含:sshserver@localhost 的密码:

                stdout should contain: sshserver@localhost's password:

                推荐答案

                这是一个工作 SSH 代码的示例,它处理证书部分是/否的提示以及在要求输入密码时.

                Here's an example of working SSH code that handles the promt for yes/no on the certificate part and also when asked for a password.

                #!/usr/bin/python
                
                import pty, sys
                from subprocess import Popen, PIPE, STDOUT
                from time import sleep
                from os import fork, waitpid, execv, read, write
                
                class ssh():
                    def __init__(self, host, execute='echo "done" > /root/testing.txt', askpass=False, user='root', password=b'SuperSecurePassword'):
                        self.exec = execute
                        self.host = host
                        self.user = user
                        self.password = password
                        self.askpass = askpass
                        self.run()
                
                    def run(self):
                        command = [
                                '/usr/bin/ssh',
                                self.user+'@'+self.host,
                                '-o', 'NumberOfPasswordPrompts=1',
                                self.exec,
                        ]
                
                        # PID = 0 for child, and the PID of the child for the parent    
                        pid, child_fd = pty.fork()
                
                        if not pid: # Child process
                            # Replace child process with our SSH process
                            execv(command[0], command)
                
                        ## if we havn't setup pub-key authentication
                        ## we can loop for a password promt and "insert" the password.
                        while self.askpass:
                            try:
                                output = read(child_fd, 1024).strip()
                            except:
                                break
                            lower = output.lower()
                            # Write the password
                            if b'password:' in lower:
                                write(child_fd, self.password + b'
                ')
                                break
                            elif b'are you sure you want to continue connecting' in lower:
                                # Adding key to known_hosts
                                write(child_fd, b'yes
                ')
                            elif b'company privacy warning' in lower:
                                pass # This is an understood message
                            else:
                                print('Error:',output)
                
                        waitpid(pid, 0)
                

                您无法立即读取 stdin 的原因(如果我在这里错了,请纠正我)是因为 SSH 在您需要的不同进程 ID 下作为子进程运行阅读/附加到.

                The reason (and correct me if i'm wrong here) for you not being able to read the stdin straight away is because SSH runs as a subprocess under a different process ID which you need to read/attach to.

                由于您使用的是 windows,所以 pty 将不起作用.有两种解决方案会更好,那就是 pexpect 和有人指出的基于密钥的身份验证.

                Since you're using windows, pty will not work. there's two solutions that would work better and that's pexpect and as someone pointed out key-based authentication.

                为了实现基于密钥的身份验证,您只需执行以下操作:在您的客户端上,运行:ssh-keygen将您的 id_rsa.pub 内容(一行)复制到服务器上的 /home/user/.ssh/authorized_keys 中.

                In order to achieve a key-based authentication you only need to do the following: On your client, run: ssh-keygen Copy your id_rsa.pub content (one line) into /home/user/.ssh/authorized_keys on the server.

                你就完成了.如果没有,请使用 pexpect.

                And you're done. If not, go with pexpect.

                import pexpect
                child = pexpect.spawn('ssh user@host.com')
                child.expect('Password:')
                child.sendline('SuperSecretPassword')
                

                这篇关于如何使用子进程模块与 ssh 交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Windows 中使用子进程 下一篇:如何在 Python 2.7 中线程化多个子进程实例?

                相关文章

                <small id='aQnbW'></small><noframes id='aQnbW'>

                  <i id='aQnbW'><tr id='aQnbW'><dt id='aQnbW'><q id='aQnbW'><span id='aQnbW'><b id='aQnbW'><form id='aQnbW'><ins id='aQnbW'></ins><ul id='aQnbW'></ul><sub id='aQnbW'></sub></form><legend id='aQnbW'></legend><bdo id='aQnbW'><pre id='aQnbW'><center id='aQnbW'></center></pre></bdo></b><th id='aQnbW'></th></span></q></dt></tr></i><div id='aQnbW'><tfoot id='aQnbW'></tfoot><dl id='aQnbW'><fieldset id='aQnbW'></fieldset></dl></div>

                    <bdo id='aQnbW'></bdo><ul id='aQnbW'></ul>
                1. <legend id='aQnbW'><style id='aQnbW'><dir id='aQnbW'><q id='aQnbW'></q></dir></style></legend>

                  <tfoot id='aQnbW'></tfoot>