首页 > 代码库 > my paramiko class
my paramiko class
#!/usr/bin/evn python # coding:utf-8 import paramiko,os class ParamikoTools(object): def __init__(self,ip, port, user, password, key, timeout): self.host = ip self.port = port self.user = user self.password = password self .key = key self.timeout = timeout def sshs(self,cmd): try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=self.host,port=self.port,username=self.user,password=self.password,timeout=self.timeout) stdin, stdout, stderr = ssh.exec_command(cmd) return stdout.read() ssh.close() except Exception,e: print "%s exec command except:%s"%(self.host,e) def sshkey(self,key,cmd): try: k = paramiko.RSAKey.from_private_key_file(key) c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(hostname=self.host, username=self.user, pkey=k,timeout=self.timeout) stdin, stdout, stderr = c.exec_command(cmd) return stdout.read() c.close() except Exception,e: print "%s exec command except:%s" &(self.host,e) def getRemoteFiles(self, localfile, remotefile): try: # ssh = paramiko.SSHClient() # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # ssh.connect(hostname=self.host, port=self.port, username=self.user, password=self.password, # timeout=self.timeout) transport = paramiko.Transport((self.host,self.port)) transport.connect(username=self.user,password=self.password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.get(remotefile, localfile) print "Transport file fished !" transport.close() except Exception,e: print "Get file from %s to local %s,%s"%(remotefile, localfile, e) def putLocalToRemote(self, localfile, remotefile): try: transport = paramiko.Transport((self.host,self.port)) transport.connect(username=self.user,password=self.password) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(localfile, remotefile) print "Transport file fished !" except Exception,e: print "Put local file to remote host execpt, localfile:%s-- remotedir:%s--except:%s" %(localfile, remotefile, e)
本文出自 “-=湖边竹=-” 博客,请务必保留此出处http://bronte.blog.51cto.com/2418552/1864420
my paramiko class
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。