我可以在命令行中给出以下命令
C:\>cd "C:\Program Files\ExtraPuTTY\Bin"
C:\Program Files\ExtraPuTTY\Bin>putty.exe -ssh root@172.20.0.102 22
这可以帮助我通过PuTTY打开SSH会话.
而我无法在Python脚本中复制它们.
cwd="C://Program Files//ExtraPuTTY//Bin"
COMMAND="ls"
ssh = Popen(['putty.exe -ssh','%s'%HOST, COMMAND,cwd],shell=True,stdout=f,stderr=f)
我看到的错误是
“putty.exe -ssh”‘ is not recognized as an internal or external command,operable program or batch file
解决方法:
然后,此python代码段应该可以工作:
import subprocess
cmd='plink -ssh {}@{} -pw {}'.format(user,server,password)
sp = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
sp.stdin.write(stdin)
sp.stdin.close()
stdout= sp.stdout.read()
stderr=sp.stderr.read()
sp.wait()
stdin是用户在终端中键入的命令,stdout和stderr是服务器输出.
在user =“ root”,server =“ 172.20.0.102 22”和ssh连接的密码中填写凭据
标签:python,ssh,popen,putty
来源: https://codeday.me/bug/20191009/1882136.html