Python3 script for remote bulk modification of file contents

Source: Internet
Author: User
Tags stdin nameserver

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.