您好,登錄后才能下訂單哦!
這篇文章運用了實例代碼展示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.189.217","10.110.193.106"] class pub_filebeat(): def __init__(self): self.username = "redis" self.passwd = "123456" self.client_ssh = None self.sftpcliet = None self.port = 22 self.chan = None self.result = None # 創建一個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) self.chan = self.client_ssh.invoke_shell() self.flag=None ''' 在現有的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.sendcmd(" sudo su - ") if self.flag == 1: self.sendcmd("redis@yto369") self.sendcmd(" cd /home/redis ") self.sendcmd(" tar -zxvf filebeat623.tar.gz ") self.sendcmd(" sed -i 's/hostip: 10.1.241.99/hostip: %s/g' filebeat623/filebeat.yml ; " % (ip)) self.sendcmd(" nohup filebeat623/filebeat -e -c filebeat623/filebeat.yml > info.log 2>&1 & ") print "%s filebeat 部署完成" % (ip) except Exception,e: print e.message 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() def sendcmd(self,cmd): cmd = cmd + '\r' p = re.compile(r'\[root@.+\s.*\]') self.result = "" self.chan.send(cmd) while True: time.sleep(0.5) ret = self.chan.recv(65535).decode("utf-8") self.result = self.result + ret if 'password' in self.result.split(" "): self.flag = 1 break if p.search(ret): break # 遍歷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實現遠程配置軟件腳本就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。