使用paramiko 进行多线程本地代码远端打包,及本地打总包

本文介绍了如何利用Python的paramiko库进行多线程操作,实现本地代码的远端批量打包,并详细讲解了如何在本地进行打包结果的整合,为自动化部署提供高效解决方案。
摘要由CSDN通过智能技术生成
# -*- coding: utf-8 -*-
#! /usr/bin/python
import paramiko
import os
import time
import threading
import commands
import sys

master_running = True
agent_running = True


class SSHConnection(object):
    def __init__(self, host_dict):
        self.host = host_dict['host']
        self.port = host_dict['port']
        self.username = host_dict['username']
        self.passwd = host_dict['passwd']
        self.__k = None
 
    def connect(self):
        transport = paramiko.Transport((self.host, self.port))
        transport.connect(username=self.username,password=self.passwd)
        self.__transport = transport
 
    def close(self):
        self.__transport.close()
 
    def run_cmd(self, command):
        ssh = paramiko.SSHClient()
        ssh._transport = self.__transport
        stdin, stdout, stderr = ssh.exec_command(command)
        # 获取命令结果
        res = to_str(stdout.read())
        # 获取错误信息
        error = to_str(stderr.read())
        # 如果有错误信息,返回error
        # 否则返回res
        if error.strip():
            return False, error
        else:
            return True, res
 
    def upload(self, local_path, target_path, exclude_args):
        def put_file(lo_path, ta_path):
            if not os.path.exists(lo_path):
                print "%s is not exist" % lo_path
                exit()
            if os.path.isdir(lo_path):
                self.run_cmd('mkdir %s' % ta_path)
                file_list = os.listdir(lo_path)
                for file in file_list:
                    if file in exclude_args:
                        continue
                    new_lo = lo_path + &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值