python paramiko详解_Python paramiko模块使用多个命令

我有一个创建连接的类.我可以在通道关闭之前连接并执行1命令.在另一个系统上,我可以执行多个命令,并且通道不会关闭.显然它是我尝试连接的系统的配置问题.

class connect:

newconnection = ''

def __init__(self,username,password):

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:

ssh.connect('somehost', username=username,password=password,port=2222,timeout=5)

except:

print "Count not connect"

sys.exit()

self.newconnection = ssh

def con(self):

return self.newconnection

然后我使用’ls’命令打印一些输出

sshconnection = connect('someuser','somepassword').con()

stdin, stdout, stderr = sshconnection.exec_command("ls -lsa")

print stdout.readlines()

print stdout

stdin, stdout, stderr = sshconnection.exec_command("ls -lsa")

print stdout.readlines()

print stdout

sshconnection.close()

sys.exit()

在第一个exec_command运行后,它将打印目录列表的预期输出.当我在第一个exec_command之后打印stdout时,它看起来像是关闭了通道

>>

就像我在另一个系统上说的那样,我能够继续运行命令并且连接不会关闭.有没有办法让我保持开放?或者更好的方式,我可以看到它关闭的原因?

编辑:所以看起来每个SSHClient.exec_command只能运行1个命令…所以我决定使用get_transport().open_session()然后运行一个命令.第一个总是有效的.第二个总是失败,脚本只是挂起

最佳答案 在exec_command执行后只有paramiko,关闭通道并且ssh返回auth提示符.

似乎只有paramiko,尝试织物或其他工具是不可能的.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值