Python自动安装zabbix-agent2实例
如需安装其他应用,只要把CMD中名利进行相应的更换,就可以实现。
可配合python打开excel,生成列表一起使用,批量安装应用。
https://blog.csdn.net/yayun616/article/details/124476881


import paramiko



cmd = [
    "yum remove zabbix* -y",
    "rpm -ivh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm",
    "yum install -y zabbix-agent2",
    "sed -i 's/Server=127.0.0.1/Server=zabbix.xxxxx.com,zabbix1.xxxxx.com,zabbix2.xxxxxx.com/g' /etc/zabbix/zabbix_agent2.conf",
    "systemctl enable zabbix-agent2",
    "systemctl start zabbix-agent2"
]

def ssh(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("命令执行失败 ," + command)
            break

        print("----done: ", command)
    sshc.close()

if __name__ == '__main__':
    host = ''
    user = ''
    passwd = ''
    ssh(host,user,passwd,cmd)

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐