putty连接linux上传python,通过PuTTY用于SSH的Python脚本

我可以在命令行中给出以下命令

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值