首页 > 代码库 > paramiko操作详解(封装好的类,可以直接使用)
paramiko操作详解(封装好的类,可以直接使用)
#!/usr/bin/env python#encoding:utf8#author: djokerimport paramikoclass myParamiko: def __init__(self,hostip,username,password,port=22): self.hostip = hostip self.port = port self.username = username self.password = password self.obj = paramiko.SSHClient() self.obj.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.obj.connect(self.hostip,self.port,self.username,self.password) self.objsftp = self.obj.open_sftp() def run_cmd(self,cmd): stdin,stdout,stderr = self.obj.exec_command(cmd) return stdout.read() def run_cmdlist(self,cmdlist): self.resultList = [] for cmd in cmdlist: stdin,stdout,stderr = self.obj.exec_command(cmd) self.resultList.append(stdout.read()) return self.resultList def get(self,remotepath,localpath): self.objsftp.get(remotepath,localpath) def put(self,localpath,remotepath): self.objsftp.put(localpath,remotepath) def getTarPackage(self,path): list = self.objsftp.listdir(path) for packageName in list: stdin,stdout,stderr = self.obj.exec_command("cd " + path +";" + "tar -zvcf /tmp/" + packageName + ".tar.gz " + packageName) stdout.read() self.objsftp.get("/tmp/" + packageName + ".tar.gz","/tmp/" + packageName + ".tar.gz") self.objsftp.remove("/tmp/" + packageName + ".tar.gz") print "get package from " + packageName + " ok......" def close(self): self.objsftp.close() self.obj.close()if __name__ == ‘__main__‘: sshobj = myParamiko(‘10.10.8.21‘,‘root‘,‘xxxxxxxx‘,22) sshobj.close()
paramiko操作详解(封装好的类,可以直接使用)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。