import paramiko
from multiprocessing import Pool
#只需要修改列表中的每个命令即可
cmd_change = [
"sed -i 's/^Server=127.0.0.1/Server=zabbix.xxxxx.com,zabbix1.xxxxx.com,zabbix2.xxxxx.com/g' /etc/zabbix/zabbix_agent.conf",
"sed -i 's/^ServerActive=127.0.0.1/ServerActive=zabbix.xxxxx.com,zabbix1.xxxxx.com,zabbix2.xxxxx.com/g' /etc/zabbix/zabbix_agent.conf",
"systemctl restart zabbix-agent",
]
# [[ip,账号,密码],[ip,账号,密码]]
zhanghao = [['10.1x.2xx.5x', 'root', 'xxxxxxx'], ['10.1x.2xx.5x', 'root', 'xxxxxxx'],]
def ssh_login_linux(host, user, passwd, cmd, port=22):
sshc = paramiko.SSHClient()
sshc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
sshc.connect(hostname=host, port=port, username=user, password=passwd)
except BaseException:
print("ssh账号密码认证错误", host, port, user, passwd)
print("\n----remote execute command")
for command in cmd:
stdin, stdout, stderr = sshc.exec_command(command)
stdout.channel
return_code = stdout.channel.recv_exit_status()
if return_code != 0:
print(host,"命令执行失败 :" + command)
break
print(host,"----done: ", command)
sshc.close()
if __name__ == '__main__':
#5个进程同时运行
pool = Pool(5)
for i in zhanghao:
pool.apply_async(func=ssh_login_linux, args=(i[0],i[1],i[2],cmd_change))
pool.close()
pool.join()
Python通过多进程,批量修改linux中文件配置。只需要修改命令内容即可
于 2023-04-06 14:43:05 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)