python paramiko链接linux执行命令操作

使用python的第三方库paramiko来登陆linux,来执行相关的操作。这样做的目的是把程序中要执行的命令交给linux操作,这样主程序就不被阻塞或等待可以继续执行下面的程序。

#coding:utf8
import os
import os.path
import paramiko 


'''
1,建库 建表
2,对这个用户赋予对所有库可mysqlimport的权限
GRANT FILE ON *.* TO 'idexadmin'@'%'
3,/opt/lnmp/mysql/bin/mysqlimport -uidexadmin -pidex000 -h180.169.19.149 -P3306 --columns='title'  Letv demo1000.txt

'''



PATH = os.path.dirname(__file__)


class sshClient():
    def __init__(self):
        self.client = None
    def connect(self,dbConf):
        try:
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(dbConf['host'],dbConf['port'],dbConf['username'],dbConf['passwd'],timeout=20)
        except Exception as e:
            return False,e
        self.client = ssh
        return True,'ok'
    
    def execute(self,*cmd):
        stdin, stdout, stderr = self.client.exec_command(*cmd)
        print stdout.readlines()
        print stderr.readlines()
        
    def  putFile(self,localPath,remotePath):
        #f = self.client.get_transport()
        #ftp =  paramiko.SFTPClient.from_transport(f)
        ftp = self.client.open_sftp()
        ftp.chmod(remotePath,777)
        if os.path.isdir(localPath):
            for f in os.listdir(localPath):
                remotefile = os.path.join(remotePath,f)
                localf = os.path.join(localPath,f)
                if os.path.isfile(localf):
                    ftp.put(localf,remotefile)
        else:
            files = os.path.split(localPath)[-1]
            remotefile = os.path.join(remotePath,fles)
            ftp.put(localPath,'%s/'%remotefile)
        
        
        
    def close(self):
        self.client.close()
        
        
if __name__ == "__main__":
    cmd = "%s -u %s -p%s -h %s -P %s -d  --columns='title' %s %s &>/dev/null &"%('/opt/lnmp/mysql/bin/mysqlimport','root','idex000','host','3306','Letv','demo1000000.txt')
    #note 这个是交给linux后台执行不等待输出。  "<span style="font-family: Arial, Helvetica, sans-serif;">&>/dev/null "  把标准输出和错误重定向到无底洞中,假如不加的 话就不能导入数据。我猜想是ssh模拟终端执行,而命令追加“&”就表明要后台执行;</span><span style="font-family: Arial, Helvetica, sans-serif;">exec_command(*cmd)执行后会马上断掉,执行后产生的输出找不到可输出的地方就会报错,最重导致倒入不成功。理解不对的地方,欢迎拍砖。</span>
    dbconf = dict(host = "host",
                  port = 22,
                  username = "root",
                  #passwd="XOAm2WcrZnpOCenADPcb",
                  passwd="sdf",
                  )
    
    
    ssh = sshClient()
    status,handl = ssh.connect(dbconf)
    if status:
       ssh.execute(cmd)#后台执行倒入数据
       ssh.putFile('/home/zhangcheng/bigDataEngine','/home/zhangcheng')#本地文件上传到远程
       ssh.execute('/opt/lnmp/mysql/bin/mysql -u root -pidex000 -h host -P 3306 -D Letv  -e "select * from demo1000')#命令行执行sql语句
       ssh.execute('/opt/lnmp/mysql/bin/mysql -u root -pidex000 -h host -P 3306 -D Letv  -e "insert into demo1000(title) values(\'ds\')"')#执行插入
       
        


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值