如何使用子流程模块与SSH进行交互 [英] How to interact with ssh using subprocess module
问题描述
我正在尝试使用子进程生成一个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'\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下运行.
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进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!