在使用python的ssh登录到UNIX框时,我如何使用子过程发送密码



我正在尝试使用Windows Machine的python的子过程模块连接到UNIX框。请注意:严格想使用子过程模块(PLZ不要建议Pexpect,paramiko,我无法从我无法从中运行它们据我所知,连接到Unix的窗口也有环境restricitons)这是我尝试过的代码,我总是会通过密码提示获得交互式键盘身份验证消息:我尝试将密码发送到该提示,但无法执行。

import subprocess
cmd='plink -ssh username@hostname -pw password'
sp=subprocess.Popen(cmd,stdin=subprocess.pipe,stdout=subprocess.pipe,shell=False)
sp.stdin.write('password n')
sp.stdin.flush()
error,out=sp.communicate
print error
print out

在命令中使用-pw不起作用(一个时间是-pw选项或stdin.write),也尝试使用stdin.write.write。我的执行环境也有一些局限性,也需要pexpect的术语

中不存在

这在我的Windows框上可以很好地连接到FreeBSD:

>>> import subprocess
>>> cmd = r'c:pathtoplink -ssh user@machine -pw pass'
>>> p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
             stderr=subprocess.PIPE)
>>> p.stdin.write(b'ls -alnexitn')
12
>>> p.stdin.flush()
>>> err = p.stderr.read()
>>> out = p.stdout.read()
>>> print(err)
b'Using username "user".rn'
>>> print(out.decode())
Last login: Mon Nov 20 17:16:18 2017 from xxx
... remaining of login message
$ ls -al
total 240
... listing of home directory
$ exit

userpassmachine应设置为您自己的Linux或Unix Box ...

最新更新