paramiko 远程远程部署软件脚本案例


paramiko 远程远程部署软件脚本案例


# -*- coding: utf-8 -*-

import paramiko
import sys, os
import datetime
import subprocess
import time
import re
import hashlib

reload(sys)
sys.setdefaultencoding("utf-8")
os.environ["PATH"] = "/usr/local/jdk18/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
os.environ["LANG"] = "en_US.UTF-8"

ip_list = ["10.110.220.29","10.110.220.28","10.110.220.30"]




class pub_filebeat():

    def __init__(self):
        self.username = "redis"
        self.passwd = "123456"
        self.client_ssh = None
        self.sftpcliet = None
        self.port = 22


    # 创建一个ssh 链接
    def create_ssh(self,ip):
        self.client_ssh = paramiko.SSHClient()
        self.client_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.client_ssh.connect(ip, self.port, self.username, self.passwd, timeout=5)
        ''' 
        在现有的ssh链接的基础上获取一个ftp链接
        方式一   :
        transport = self.client_ssh.get_transport()
        self.sftpcliet = paramiko.SFTPClient.from_transport(transport)
        
        方式二 :
        self.sftpcliet = self.client_ssh.open_sftp()
        '''
        #
        # transport = self.client_ssh.get_transport()
        # self.sftpcliet = paramiko.SFTPClient.from_transport(transport)
        self.sftpcliet = self.client_ssh.open_sftp()

    def execute(self,ip):
        try:
            self.client_ssh.exec_command("cd /home/redis ")
            self.client_ssh.exec_command(" sudo tar  -zxvf filebeat623.tar.gz",)
            self.client_ssh.exec_command(" sudo  chown -R  root:root filebeat623")
            self.client_ssh.exec_command(" sudo sed -i 's/hostip: 10.1.241.99/hostip: %s/g' filebeat623/filebeat.yml ; " % (ip))
            time.sleep(10)
            std_in,std_out,std_err = self.client_ssh.exec_command("  sudo nohup filebeat623/filebeat -e  -c filebeat623/filebeat.yml > info.log 2>&1 & ")
            print "%s  filebeat 部署完成" % (ip)
        except Exception,e:
            print e.message
            # self.client_ssh.close()

    def tranfile(self):
        try:
            self.sftpcliet.put("F:\dwchaoyue\ELK\/filebeat623.tar.gz", '/home/redis/filebeat623.tar.gz')
        except Exception,e:
            print e.message
            # self.sftpcliet.close()


    # 遍历ip 列表
    def bianli(self,ip_list):
        for ip in ip_list:
            self.create_ssh(ip)
            if self.check_result() > 1 :
                print "%s filebeat 已部署,无需重新部署" % (ip)
                continue
                pass
            self.tranfile()
            self.execute(ip)
            if self.check_result() < 1:
                print "%s filebeat 部署失败 ,请重试" % (ip)
            self.client_ssh.close()
            self.sftpcliet.close()


    def check_result(self):
        std_in,std_out,std_err = self.client_ssh.exec_command("ps  -ef  | grep  -i filebeat | grep  -v  'grep' | wc  -l")
        return int(std_out.read())




if __name__ == "__main__":
    try:
        pb = pub_filebeat()
        pb.bianli(ip_list)
    except Exception, e:
        print e
		
		
		
		
		
		
		

paramiko 远程执行命令和上传下载代码案例
phttps://blog.csdn.net/a382486075/article/details/78658165


		
		


上一篇:ERROR: transport error 202: bind failed: Address already in use ERROR: JDWP Transport dt_socket fail


下一篇:python实现上传文件到linux指定目录