Pyhton使用pywinrm远程自动安装Zabbix Agent

Pyhton使用pywinrm远程自动安装Zabbix Agent

为实现Zabbix监控添加自动化,使用Python远程安装Zabbix Agent,并开启服务。

1.系统环境

  • Python 3.6.1
  • pywinrm 0.3.0

2.Windows开启WinRM服务,允许远程连接。开启命令winrm quickconfig

>C:\Users\Administrator>winrm quickconfig
WinRM 服务已在这台电脑上执行。
此电脑上的 WinRM 已设定为可接受远端管理。

3.Python脚本

import winrm


# 远程连接windows执行脚本,window需开通WinRM服务
# WinRM quickconfig
def run_ps_shell(ip, username, password, ps_shell):
    try:
        # s = winrm.Session(ip, auth=(username, password), transport='ntlm', server_cert_validation='ignore')
        s = winrm.Session(ip, auth=(username, password), transport='ntlm')  # 建立连接
        r = s.run_ps(ps_shell)  # 执行powershell命令
        # r = s.run_cmd(ps_shell)   # 执行cmd命令
        print('std_out-->', str(r.std_out, 'big5'))
        print('std_err-->', str(r.std_err, 'big5'))
        std_out = r.std_out
        return std_out
    except Exception as e:
        print(str(e))	# 打印异常
    return None


if __name__ == '__main__':
    ip = ''		# Windows IP
    username = 'administrator'
    password = ''	# 密码

    ps_shell = """
        # 创建zabbix相关文件夹
        mkdir c:\\zabbix
        mkdir c:\\zabbix\\bin
        mkdir c:\\zabbix\\conf
        mkdir c:\\zabbix\\log
        $client = new-object System.Net.WebClient
        # 下载的脚本、文件需事先部署到Apache上,其它web服务也可以,以供下载
		# 下载zabbix安装脚本bat
        $client.DownloadFile("http://weburl/media/zabbix/windows/install_zabbix.bat", "c:\\zabbix\\install_zabbix.bat")
        $osarchitecture = (Get-WmiObject Win32_OperatingSystem).osarchitecture
        # By操作系统位数下载zabbix agent安装包
        if ($osarchitecture -eq "64 位") {  # 64位
            $client.DownloadFile("http://weburl/media/zabbix/windows/64bit/zabbix_agentd.exe", "c:\\zabbix\\bin\\zabbix_agentd.exe")
        }else {     # 32位
            $client.DownloadFile("http://weburl/media/zabbix/windows/32bit/zabbix_agentd.exe", "c:\\zabbix\\bin\\zabbix_agentd.exe")
        }
        # 下载zabbix配置文件
        $client.DownloadFile("http://weburl/media/zabbix/windows/conf/zabbix.conf", "c:\\zabbix\\conf\\zabbix.conf")
        # 执行bat脚本
        cmd /c "c:\\zabbix\\install_zabbix.bat"
    """
    result = run_ps_shell(ip=ip, username=username, password=password, ps_shell=ps_shell)
	print(result)

4.bat脚本内容install_zabbix.bat

Set zabbix_server_ip=0.0.0.0
echo LogFile=c:\zabbix\log\zabbix_agentd.log >> C:\zabbix\conf\zabbix.conf
echo Server=%zabbix_server_ip% >> C:\zabbix\conf\zabbix.conf
echo Hostname=%COMPUTERNAME% >> C:\zabbix\conf\zabbix.conf
echo StartAgents=10 >> C:\zabbix\conf\zabbix.conf
echo Timeout=30 >> C:\zabbix\conf\zabbix.conf
C:\zabbix\bin\zabbix_agentd.exe -i -c C:\zabbix\conf\zabbix.conf
sc config "Zabbix Agent" start=auto
net start "Zabbix Agent"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xuerba

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

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

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

打赏作者

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

抵扣说明:

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

余额充值