python修改zabbix中的ip

 ansible web -m raw -a "rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm"python

ansible web -m raw -a "yum install zabbix-agent  zabbix-sender zabbix-get"web

#!/usr/bin/env python
#coding:utf-8
import re  
import socket
import fcntl
import structsocket

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])tcp

file_name='/etc/zabbix/zabbix_agentd.conf'
server="192.168.19.137"
fp=open(file_name,'r')  
alllines=fp.readlines()  
fp.close()函數

  
fp=open(file_name,'w') 
for eachline in alllines:
    cs_ip=re.sub("Zabbix server",get_ip_address("eth0"),eachline)  
    fp.writelines(cs_ip)
for eachline in alllines:
    ss_ip=re.sub("Server",server,eachline)  
    fp.writelines(ss_ip)
fp.close()rest

#firewall
cmd1="iptables -A INPUT -s 192.168.19.137/32 -p tcp -m tcp --dport 10050:10051 -j ACCEPT"
cmd2="iptables -A INPUT -s 192.168.19.137/32 -m udp -p udp --dport 10050:10051 -j ACCEPT"
cmd3="/etc/init.d/iptables save"
cmd4="/etc/init.d/iptables restart"
commands.getoutput(cmd1)
commands.getoutput(cmd2)
commands.getoutput(cmd3)
commands.getoutput(cmd4)
 server

-A INPUT -s 192.168.19.137/32 -p tcp -m tcp --dport 10050:10051 -j ACCEPT
-A INPUT -s 192.168.19.137/32 -m udp -p udp --dport 10050:10051 -j ACCEPTip

 

 

#!/usr/bin/env python
#coding:utf-8
import re  
import socket
import fcntl
import struct
def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])
cs_ip=get_ip_address('eth0')
print cs_ip
server="192.168.19.137"utf-8

file_name='/etc/zabbix/zabbix_agentd.conf'  
fp=open(file_name,'r')  
alllines=fp.readlines()  
fp.close()  
fp=open(file_name,'w')  
for eachline in alllines:  
    a=re.sub('Zabbix server',get_ip_address('eth0'),eachline)  
    fp.writelines(a) 
for eachline in alllines:  
    b=re.sub('127.0.0.1',server,eachline)  
    fp.writelines(b)  
fp.close()get

 

函數的形式

def modifyip(tfile,sstr,rstr):

    try:

        lines=open(tfile,'r').readlines()

        flen=len(lines)-1

        for i in range(flen):

            if sstr in lines[i]:

                lines[i]=lines[i].replace(sstr,rstr)

        open(tfile,'w').writelines(lines)

        

    except Exception,e:

        print e

modifyip('a.txt','a','A')把文件中的全部小a換成A

直接修改某一行

#!/usr/bin/python

import sys,os

f=open('hi.txt','r+')
flist=f.readlines()
flist[4]='hi\n'
f=open('hi.txt','w+')
f.writelines(flist)

終極版

#!usr/bin/env python
#coding:utf-8
import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(s.fileno(),0x8915,struct.pack('256s', ifname[:15]))[20:24])
cs_ip=get_ip_address('eth0')
print cs_ip

def modifyip(tfile,sstr,rstr):     try:         lines=open(tfile,'r').readlines()         flen=len(lines)-1         for i in range(flen):             if sstr in lines[i]:                 lines[i]=lines[i].replace(sstr,rstr)         open(tfile,'w').writelines(lines)     except Exception,e:         print e modifyip('zab.txt','172.16.43.40','127.0.0.1') modifyip('zab.txt','Zabbix server',cs_ip)  

相關文章
相關標籤/搜索