溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

paramiko 遠(yuǎn)程遠(yuǎn)程部署軟件腳本案例

發(fā)布時(shí)間:2020-07-26 17:01:20 來源:網(wǎng)絡(luò) 閱讀:224 作者:dengwang123 欄目:系統(tǒng)運(yùn)維


paramiko 遠(yuǎn)程遠(yuǎn)程部署軟件腳本案例


#?-*-?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


????#?創(chuàng)建一個(gè)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
????????'''?
????????在現(xiàn)有的ssh鏈接的基礎(chǔ)上獲取一個(gè)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?部署失敗?,請(qǐng)重試"?%?(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 遠(yuǎn)程執(zhí)行命令和上傳下載代碼案例

Phttps://www.cnblogs.com/haigege/p/5513759.html

phttps://blog.csdn.net/a382486075/article/details/78658165


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI