一、说明:

1、利用Python的paramiko模块,调用远程的shell命令去修改相应文件。

2、有一个专用配置文件,列出服务器清单。

3、Python循环读取配置文件的服务器IP去连接它,并执行相应的命令。

4、主要是有一个正则,匹配Zabbix agent中的IP设置。

[root@mysql-m ~]# sed -i 's/^Server=[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/Server=33.66.88.99/g' zabbix_agentd.conf

脚本的内容如下:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "life"
# Email: batistuta1977@163.com
# Date: 2017/12/28
import paramiko
list_file_content = []
def read_file():
with open('ip-list','rU',encoding='utf-8') as f1:
for i in f1.readlines():
list_file_content.append(i.strip()) print(list_file_content)
def ssh_conn(hosts):
for host in hosts:
print(host)
ssh = paramiko.SSHClient() # 创建ssh对象
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname=host, port=22, username='root', password='1') # 执行追加文件内容命令
# 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") # 修改zabbix agent内容
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)

结果如下:

  

Python3 写的远程批量修改文件内容的脚本的更多相关文章

  1. linux --批量修改文件内容

    由于目前测试的BIOS有一个option 发生了改变,因此我们需要在之前写好的脚本上进行修改,将旧的option 改为新的选项,因此在此处用到了批量修改文件中的内容: 1. perl 命令替换: pe ...

  2. python批量修改文件内容及文件编码方式的处理

    最近公司在做tfs迁移,后面要用新的ip地址去访问tfs 拉取代码  ,所以原来发布脚本中.bat类型的脚本中的的ip地址需要更换 简单说下我们发布脚本层级目录 :每个服务站点下都会有一个发布脚本 . ...

  3. sed命令——批量修改文件内容

    批量替换单个文件内容 命令格式:sed -i 's/旧内容/新内容/g' 文件路径 sed -i 's/oldString/newString/g' file 例如:我想替换cwx.txt文件中的 j ...

  4. 使用sed,grep 批量修改文件内容

    使用sed命令可以进行字符串的批量替换操作,以节省大量的时间及人力: 使用的格式如下: sed -i "s/oldstring/newstring/g" `grep oldstri ...

  5. linux VI模式下批量修改文件内容

    1.         :s/vivian/sky/ 替换当前行第一个 vivian 为 sky            :s/vivian/sky/g 替换当前行所有 vivian 为 sky 2.   ...

  6. 用C#Winform写个简单的批量清空文件内容和删除文件的小工具

    用C#Winform写个简单的批量清空文件内容和删除文件的小工具 本文介绍这个简单得不能再简单的小项目.做这个项目,有以下目的. 1 当然是做个能用的工具 2 学习使用Github 关于用VS2013 ...

  7. 用SSH指令批量修改文件夹 文件权限和拥有者

    在linux系统下或登录ssh可以批量修改文件权限 wwwroot目录下的所有目录的权限递归设置为755 cd wwwrootfind -type d -exec chmod 755 {} \;或者是 ...

  8. [转帖]Linux下批量替换文件内容方法

    Linux下批量替换文件内容方法 https://www.cnblogs.com/fjping0606/p/4428850.html 刚才用到的命令 原作者写的挺好的记录一下 以后 用. 1:查找fi ...

  9. Web 在线文件管理器学习笔记与总结(5)修改文件内容

    ① 读出要修改的文件的内容 ② 进行修改 ③ 将修改后的内容写进文件 index.php: <?php require 'dir.func.php'; require 'file.func.ph ...

随机推荐

  1. mysql中not in子查询不能为空

    转载自:https://blog.csdn.net/headingalong/article/details/77744755 错误sql delete from company_info where ...

  2. hdu3853之概率dp入门

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xingyeyongheng/article/details/25205693 LOOPS Time ...

  3. Bootstrap.之模态框 显示在遮罩层后面

    Bootstrap.之模态框 显示在遮罩层后面 问题描述: 在使用bootstrap模态框,弹出的窗口在遮罩层后面,见图: 解决方案: 保证模态框的代码,所在的上一级(父元素)是body标签,即可.例 ...

  4. spring整合http

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接出处:https://blog.csdn.net/qq_3076499,否则保留追究法律责任的权利. 如果 ...

  5. bzoj 3231 [Sdoi2008]递归数列——矩阵乘法

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3231 矩阵乘法裸题. 1018是10^18.别忘了开long long. #include& ...

  6. textarea高度自动增高

    <!--随着textarea 输入内容 自动增加高度--> <script type="text/javascript"> $(".input_t ...

  7. zabbix自定义监控redis

    zabbix监控redis脚本 #!/bin/bash #此脚本用来获取redis-cli info信息 redis_cli="/usr/local/redis/bin/redis-cli& ...

  8. ubuntu 已安装 post-installation 脚本 返回错误状态 1

    1.$ sudo mv /var/lib/dpkg/info /var/lib/dpkg/info_old //现将info文件夹更名 2.$ sudo mkdir /var/lib/dpkg/inf ...

  9. Python之路,Day3- Python基础(转载Alex)

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  10. GYM100741 A Queries

    A. Queries time limit per test 0.25 s memory limit per test 64 MB input standard input output standa ...