First, Description:
1, using the Python Paramiko module, invoke remote shell command to modify the corresponding file.
2, there is a private profile, lists the server list.
3. Python loops through the server IP of the configuration file to connect to it and executes the corresponding command.
4, the main is a regular, matching Zabbix agent in the IP settings.
[Email protected] ~]# sed-i ' s/^server=[0-9]*\. [0-9]*\. [0-9]*\. [0-9]*/server=33.66.88.99/g ' zabbix_agentd.conf
The contents of the script are as follows:
#! /usr/bin/env python#-*-coding:utf-8-*-# __author__ = "Life" # Email: [email protected]# date:2017/12/28import par Amikolist_file_content = []def read_file (): With open (' ip-list ', ' RU ', encoding= ' Utf-8 ') as F1:for i in F1.readli NES (): List_file_content.append (I.strip ()) print (list_file_content) def ssh_conn (hosts): For host in hosts : Print (host) ssh = Paramiko. Sshclient () # Create SSH object # allows connections to host Ssh.set_missing_host_key_policy (Paramiko) that are not in the Know_hosts file. Autoaddpolicy ()) # Connection Server Ssh.connect (Hostname=host, port=22, username= ' root ', password= ' 1 ') # Execute append file inside Tolerance Command # stdin, stdout, stderr = Ssh.exec_command ("Echo ' nameserver 172.16.50.11 ' >>/tmp/1.txt") # stdin , Stdout,stderr = Ssh.exec_command ("Echo ' nameserver 172.18.50.11\n ' >>/tmp/1.txt") # Modify the Zabbix agent content stdin, stdout, stderr = Ssh.exec_command ("Sed-i" s/^server=[0-9]*\.[ 0-9]*\. [0-9]*\. [0-9]*/server=33.66.88.99/g '/etc/zabbix/zabbix_agentd.conf ") if __name__ = = ' __main__ ': Read_file () ssh_conn (list_file_content)
The results are as follows:
Python3 script for remote bulk modification of file contents