版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/78954762
没有root权限,没有ssh密钥对,又想自动输入密码咋办?
#!/usr/bin/python
# simplest builtin python pseudo-tty for ssh password. meuh
# http://unix.stackexchange.com/a/276385/119298
import os
def run(cmd,*args):
pid, fd = os.forkpty()
if pid==0: # child
os.execlp(cmd,*args)
while True:
data = os.read(fd,1024)
print data
if "password:" in data: # ssh prompt
os.write(fd,"password\n")
elif data.endswith("$ ") or data.endswith("# "): # bash prompt for input
cmd = raw_input() + "\n"
os.write(fd, cmd)
run("ssh", "ssh", "-p", "端口号", "root@10.1.1.1")