I thought about the logic that divides / 22 or / 23 into / 24 units by specifying the IP range in Python.

When I listened to the seminar "Fully Automatic Zabbix" at OSC2015 Tokyo / Spring and registered NetArc Discovery in a wide range, it took time to search, so I heard that it is better to register separately ... But, "It's troublesome to calculate the range and divide it" ... So, after studying Python, I thought about the logic to divide / 22 or / 23 into 24 units by specifying the IP range.

It is the logic to divide the IP. Since this is a logic test, variable names are appropriate. (excuse) It's messy-A (^_^;

import sys

io = sys.argv[1]

tmp = io.split("/")

ip     = tmp[0]
prefix = tmp[1]

tmp = ip.split(".")
o1 = tmp[0]
o2 = tmp[1]
o3 = tmp[2]
o4 = tmp[3]

sum = (int(o1) << 24 | int(o2) << 16 | int(o3) << 8 | int(o4) ) & 0xFFFFFF00
sum = sum >> (32-int(prefix))
sum = sum << (32-int(prefix))

for i in range(0, 2 << (24 - int(prefix) - 1)):
    a = sum | i<<8
    o1 = (a & 0xFF000000) >> 24
    o2 = (a & 0x00FF0000) >> 16
    o3 = (a & 0x0000FF00) >> 8
    o4 = 0

    a = str(o1) + "." + str(o2) + "." + str(o3) + "." + str(o4) + "/24"

    print a

Execution result adc61fa5e5778d323d28c90526a81b6e.png

Python on Windows ... A (^_^;

After that, you can register the divided IP using Zabbix API ...

Running the Zabbix API in Python is easy with the article below. [Try the ZABBIX API with Pyhton](http://www.zumwalt.info/blog/2012/11/pyhton%E3%81%A7zabbix-api%E3%82%92%E8%A7%A6%E3 % 81% A3% E3% 81% A6% E3% 81% BF% E3% 82% 8B /)

As mentioned in the article, I also used pycurl at first, but switched to urllib2. A (^_^;

When using Zabbix API in Python, it is convenient to create xxx.json for request. If you read the file with json.load, replace auth of params and various parameters and throw it, you do not have to write all the requests in the program as an excuse, which is convenient.

Recommended Posts

I thought about the logic that divides / 22 or / 23 into / 24 units by specifying the IP range in Python.
Macro (VBA) that converts the contents selected in the range in Excel into the list initialization code in Python.
About netaddr that operates IP address, CIDR, etc. in Python
About "for _ in range ():" in python
If I thought I didn't see the pyc file recently, it was quarantined in pycache by python3.
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
Movement that changes direction in the coordinate system I tried Python 3
I learned about processes in Python
I wrote the queue in Python
I wrote the stack in Python
How to sort by specifying a column in the Python Numpy array.
OR the List in Python (zip function)
Read the file line by line in Python
Read the file line by line in Python
Talking about the features that pandas and I were in charge of in the project
Divides the character string by the specified number of characters. In Ruby and Python.
Note that I understand the least squares algorithm. And I wrote it in Python.
I made a tool in Python that right-clicks an Excel file and divides it into files for each sheet.