In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Paramiko remote remote deployment Software script case
#-*-coding: utf-8-*-import paramikoimport sys, osimport datetimeimport subprocessimport timeimport reimport hashlibreload (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 # create a ssh link 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''get a ftp link based on the existing ssh link: transport = self.client_ssh.get_transport () self.sftpcliet = paramiko.SFTPClient.from_transport (transport) method 2: 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 Universe g 'filebeat623/filebeat.yml "% (ip) self.sendcmd (" nohup filebeat623/filebeat-e-c filebeat623/filebeat.yml > info.log 2 > & 1 & ") print" s filebeat deployment complete "% (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 # traverses the ip list def bianli (self Ip_list): for ip in ip_list: self.create_ssh (ip) if self.check_result () > = 1: print "% s filebeat has been deployed No need to redeploy "% (ip) continue pass self.tranfile () self.execute (ip) if self.check_result () < 1: print"% s filebeat deployment failed Please retry "% (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 remote command execution and upload and download code cases
P https://www.cnblogs.com/haigege/p/5513759.html
P https://blog.csdn.net/a382486075/article/details/78658165
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.