Python paramiko模块使用多个命令

Python paramiko模块使用多个命令,第1张

概述我有一个创建连接的类.我可以在通道关闭之前连接并执行1命令.在另一个系统上,我可以执行多个命令,并且通道不会关闭.显然它是我尝试连接的系统的配置问题. class connect: newconnection = '' def __init__(self,username,password): ssh = paramiko.SSHClient() 我有一个创建连接的类.我可以在通道关闭之前连接并执行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,stderr = sshconnection.exec_command("ls -lsa")print stdout.readlines() print stdout sshconnection.close()sys.exit()

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

<paramiko.Channelfile from <paramiko.Channel 1 (closed) -> <paramiko.Transport at 0x2400f10L (cipher aes128-ctr,128 bits) (active; 0 open channel(s))>>>

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

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

解决方法 在exec_command执行后只有paramiko,关闭通道并且ssh返回auth提示符.

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

** fabric did not work out too.

总结

以上是内存溢出为你收集整理的Python paramiko模块使用多个命令全部内容,希望文章能够帮你解决Python paramiko模块使用多个命令所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1192959.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存