【超简单】Python自动安装zabbix-agent2实例

该文章介绍了如何使用Python的paramiko库通过SSH连接远程主机,执行一系列命令来自动化安装Zabbix-agent2。过程包括卸载旧版本,安装新版本,配置服务器地址,并启动服务。这种方法可以扩展到其他应用的批量安装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python自动安装zabbix-agent2实例
如需安装其他应用,只要把CMD中名利进行相应的更换,就可以实现。
可配合python打开excel,生成列表一起使用,批量安装应用。
https://blog.youkuaiyun.com/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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信飞翔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值