my paramiko class

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

paramiko读音?

我用`pip install paramiko`安装了paramiko,但无法导入paramiko [重复]

python第三方模块之paramiko模块

paramiko模块,线程,进程

paramiko

python paramiko 模块