繁体   English   中英

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

[英]How to interact with ssh using subprocess module

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

我正在使用Windows 7上的Python 2.7.6

这是我的代码:

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

产出:

None

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

下面是一个使用SSH代码的示例,它可以在证书部分处理yes / no的promt,也可以在要求输入密码时使用。

#!/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'\n')
                break
            elif b'are you sure you want to continue connecting' in lower:
                # Adding key to known_hosts
                write(child_fd, b'yes\n')
            elif b'company privacy warning' in lower:
                pass # This is an understood message
            else:
                print('Error:',output)

        waitpid(pid, 0)

你不能立即读取stdin的原因(并纠正我,如果我错了)是因为SSH作为一个子进程在你需要读取/附加的不同进程ID下运行。

由于您使用的是Windows,因此pty将无法使用。 有两种解决方案,将更好地工作,这就是Pexpect的 ,并有人指出,基于密钥的认证。

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

而且你已经完成了。 如果没有,请选择pexpect。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM