【python自动化运维】批量安装zabbix agent

装完Zabbix Server后还有一大堆机器等着装zabbix agent怎么办?

saltstack不会!
ansible不会!!
shell不会!!!
python也不会!!!!

那还等什么,下载我做好的程序,配置你的账号密码,一键安装!!!

链接: https://pan.baidu.com/s/1RJe-su0XFPUPCeXyR2pi1g
提取码: w453





源码共享

ps:小白你别看了,学不会的!!

#!/usr/bin/python3
# author wugf

import configparser
import paramiko
import platform
import os

ini = "./install_zabbix_agent.ini"

# 通过ssh协议执行命令
def ssh(host, port, user, passwd, cmd):
    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("命令执行失败 ," + command)
            exit(1)
        
        print("----done: ",command)
    sshc.close()
        
conp = configparser.ConfigParser()
conp.read(ini, encoding = "utf-8")
sections = conp.sections()

rpm = conp["DEFAULT"]["rpm"]  # zabbix agent rpm文件地址
Server = conp["DEFAULT"]["Server"]  # zabbix server的地址
conf = conp["DEFAULT"]["conf"]  # zabbix_agentd.conf文件的地址

# 安装zabbix_agent命令
cmd = [ 
    "yum remove zabbix* -y",
    "rpm -ivh " + rpm,
    "sed -i '1,$s/^Server=.*/Server=" + Server + "/' " + conf,
    "更改zabbix_agent Hostname",
    "systemctl enable zabbix-agent",
    "systemctl start zabbix-agent"
    ]

for section in sections:
    Hostname = section  # zabbix_agent Hostname
    cmd[3] = "sed -i '1,$s/^Hostname=.*/Hostname=" + Hostname + "/' " + conf
    host = conp.get(section, "host")
    port = int(conp.get(section, "port"))
    user = conp.get(section, "user")
    passwd = conp.get(section, "passwd")
    
    ssh(host, port, user, passwd, cmd)

win = "Windows"
if win in platform.platform():
    os.system("pause")
    
    


源码共享

ps:小白你别看了,学不会的!!
在这里插入图片描述

#!/usr/bin/python3
# author wugf

import configparser
import paramiko
import platform
import os

ini = "./install_zabbix_agent.ini"

# 通过ssh协议执行命令
def ssh(host, port, user, passwd, cmd):
    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("命令执行失败 ," + command)
            exit(1)
        
        print("----done: ",command)
    sshc.close()
        
conp = configparser.ConfigParser()
conp.read(ini, encoding = "utf-8")
sections = conp.sections()

rpm = conp["DEFAULT"]["rpm"]  # zabbix agent rpm文件地址
Server = conp["DEFAULT"]["Server"]  # zabbix server的地址
conf = conp["DEFAULT"]["conf"]  # zabbix_agentd.conf文件的地址

# 安装zabbix_agent命令
cmd = [ 
    "yum remove zabbix* -y",
    "rpm -ivh " + rpm,
    "sed -i '1,$s/^Server=.*/Server=" + Server + "/' " + conf,
    "更改zabbix_agent Hostname",
    "systemctl enable zabbix-agent",
    "systemctl start zabbix-agent"
    ]

for section in sections:
    Hostname = section  # zabbix_agent Hostname
    cmd[3] = "sed -i '1,$s/^Hostname=.*/Hostname=" + Hostname + "/' " + conf
    host = conp.get(section, "host")
    port = int(conp.get(section, "port"))
    user = conp.get(section, "user")
    passwd = conp.get(section, "passwd")
    
    ssh(host, port, user, passwd, cmd)

win = "Windows"
if win in platform.platform():
    os.system("pause")
    
    

猜你喜欢

转载自blog.csdn.net/wuguifa/article/details/88972502