【发布时间】:2021-11-12 08:04:54
【问题描述】:
我一直在尝试使 ssh 自动进入我的服务器,但是找不到完全自动化该过程的方法。具体来说,绕过这个输入一直很困难:root@example 的密码:
我的代码:
import subprocess
import time
server_ip = 'server'
pwd = b'password'
p = subprocess.Popen(['ssh', 'root@{}'.format(server_ip)],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
if p == "root@example's password: ":
p.communicate(input= "{}".format(pwd))
else:
time.sleep(2)
if p == "root@example's password: ":
p.communicate(input= "{}".format(pwd))
else:
pass
返回什么:
Pseudo-terminal will not be allocated because stdin is not a terminal.
root@example's password:
user@computer ~ % Permission denied, please try again.
root@example's password:
Permission denied, please try again.
root@example's password:
root@example: Permission denied (publickey,password).
I know my code is very scuffed but it is the furthest I've got to getting in and submitting the password entry request.
感谢任何帮助!
【问题讨论】:
-
您可以使用
paramiko通过 ssh 连接使用 python 远程执行命令。会有帮助吗? -
也值得一试fabfile.org
标签: python ssh subprocess