IP Parsing Function
This section describes the IP parsing functions, including their syntax, parameters, and usage examples.
Function |
Description |
---|---|
Parses the country, province/state, and city information based on the IP address. |
|
Checks whether the IP address belongs to the CIDR address block. |
|
Checks whether the IP address is IPv4 or IPv6. |
|
Checks whether the IP address is a private address or a public address. |
|
Translates a single IP address into a CIDR address block. |
|
Outputs the input CIDR address block in prefixlen or netmask. |
|
Checks whether two network segments overlap. |
|
Converts an IP address in string to a long integer. |
|
Converts a long integer to an IP address in string. |
geo_parse
Use the geo_parse function to parse the country, province/state, and city information based on the IP address.
- Function format
geo_parse(ip, keep_fields=None, ip_sep=None)
- Parameter description
Parameter
Type
Mandatory
Description
ip
String
Yes
IP address, which is used to parse the country, province, and city information. If there are multiple IP addresses, you can use the ip_sep parameter to specify separators.
keep_fields
Tuple
No
Key information contained in the returned result. The supported keys are as follows:
- city: city name.
- province: province name.
- country: country name.
- isp: name of the network carrier.
- lat: latitude of the location where the IP address is located.
- lon: longitude of the location where the IP address is located.
For example, keep_fields=("city","country") indicates that only the city and country information is output.
In addition, keep_fields supports field renaming. For example, (("city","cty"),("country","state")) will output the fields as cty and state.
Note: The keep_fields parameter is of the tuple type. If there is only one parameter, add a comma (,), for example, ("city",) or (("city","cty"),).
ip_sep
String
No
IP address separator, which is used to separate a character string containing multiple IP addresses into multiple IP addresses. The parsing result is returned in JSON format. The default value is None, indicating that no separation is performed.
- Returned result
Data in dictionary format.
- Function example
- Query a single IP address.
- Test data
{ "ip": "192.168.0.1" }
- Processing rule
e_set("geo_parse", geo_parse("192.168.0.1"))
- Processing result (only available for public IP addresses)
ip: 192.168.0.1 geo_parse: {"city": "", "province": "", "country": "", "isp": "", "lat": "", "lon": ""}
- Test data
- Query multiple IP addresses.
- Test data
{ "ip": "192.168.0.1" }
- Processing rule
e_set("geo_parse", geo_parse("192.168.0.1,192.168.0.2", ip_sep=","))
- Processing result (only available for public IP addresses)
geo_parse: {"192.168.0.1": {"province": "", "country": "", "lat": "", "lon": ""}, "192.168.0.2": {"city": "", "province": "", "country": "", "isp": "", "lat": "", "lon": ""}} ip: 192.168.0.1
- Test data
- Query a single IP address.
ip_cidrmatch
Use the ip_cidrmatch function to check whether the IP address belongs to the CIDR address block.
- Function format
ip_cidrmatch(cidr_subnet, ip, default="")
- Parameter description
Parameter
Type
Mandatory
Description
cidr_subnet
String
Yes
CIDR address block.
ip
String
Yes
IP address.
default
String
No
This value is returned if the IP address does not match the CIDR address block.
- Returned result
If the IP address belongs to the CIDR block, true is returned. Otherwise, false is returned.
- Function example
- The IPv4 address matches the CIDR address block.
- Test data
{ "subnet": "192.168.1.0/24" }
- Processing rule
e_set("result",ip_cidrmatch(v("subnet"),"192.168.1.11"))
- Processing result
subnet: 192.168.1.0/24 result: true
- Test data
- The IP address does not match the CIDR address block.
- Test data
{ "subnet": "192.168.1.0/24" }
- Processing rule
e_set("result",ip_cidrmatch(v("subnet"),"192.168.100.10", default="error"))
- Processing result
subnet: 192.168.1.0/24 result: error
- Test data
- The IPv4 address matches the CIDR address block.
ip_version
Use the ip_version function to check whether the IP address is IPv4 or IPv6.
- Function format
ip_version(ip, default="")
- Parameter description
Parameter
Type
Mandatory
Description
ip
String
Yes
Entered IP address.
default
String
No
This value is returned when the IP address version cannot be determined.
- Returned result
IPv4 or IPv6
- Function example
- IPv4 address.
- Test data
{ "ip": "10.21.115.10" }
- Processing rule
e_set("version",ip_version(v("ip")))
- Processing result
ip: 10.21.115.10 version: IPv4
- Test data
- IPv6 address.
- Test data
{ "ip": "2001:0db8:85a3:0000:0000:8a2e:0370:7334" }
- Processing rule
e_set("version",ip_version(v("ip")))
- Processing result
ip: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 version: IPv6
- Test data
- IPv4 address.
ip_type
Use the ip_type function to check whether the IP address is a private or public address.
- Function format
ip_type(ip, default="")
- Parameter description
Parameter
Type
Mandatory
Description
ip
String
Yes
IP address.
default
String
No
This value is returned when the IP address type cannot be determined.
- Returned result
IP address type, which can be private, reserved, loopback, public, or allocated ripe ncc.
- Function example
- Private IP address.
- Test data
{ "ip": "10.1.2.3" }
- Processing rule
e_set("result",ip_type(v("ip")))
- Processing result
ip: 10.1.2.3 result: private
- Test data
- IPv6 address.
- Test data
{ "ip": "127.0.0.1" }
- Processing rule
e_set("result",ip_type(v("ip")))
- Processing result
ip: 127.0.0.1 result: loopback
- Test data
- Private IP address.
ip_makenet
Use the ip_makenet function to translate a single IP address into a CIDR address block.
- Function format
ip_makenet(ip, subnet_mask=None, default="")
- Parameter description
Parameter
Type
Mandatory
Description
ip
String
Yes
IP address.
subnet_mask
String
Yes
Subnet mask. If the IP address is an IP address range, the subnet mask can be left blank.
default
String
No
This value is returned when the IP address cannot be converted into a CIDR address block.
- Returned result
CIDR address block.
- Function example
- Convert an IP address range to a CIDR address block.
- Test data
{ "ip": "192.168.10.0-192.168.10.255" }
- Processing rule
e_set("result",ip_makenet(v("ip")))
- Processing result
ip: 192.168.10.0-192.168.10.255 result: 192.168.10.0/24
- Test data
- Convert an IP address to a CIDR address block.
- Test data
{ "ip": "192.168.10.0" }
- Processing rule
e_set("result",ip_makenet(v("ip"), "255.255.255.0"))
- Processing result
ip: 192.168.10.0 result: 192.168.10.0/24
- Test data
- Convert an IP address range to a CIDR address block.
ip_to_format
Use the ip_to_format function to output the input CIDR address block in prefixlen or netmask.
- Function format
ip_to_format(cidr_subnet, want_prefix_len=0, default="")
- Parameter description
Parameter
Type
Mandatory
Description
cidr_subnet
String
Yes
CIDR block. For example, 192.168.10.0/24.
want_prefix_len
Int
No
Return format. The default value is 0.
- 0: no format
- 1: prefix
- 2: netmask
- 3: IP address range
default
String
No
This value is returned when the input CIDR address block cannot be output in the format.
- Returned result
IP address in a specific format.
- Function example
- Output the IP address in the IP address range format.
- Test data
{ "ip": "192.168.11.0/24" }
- Processing rule
e_set("result",ip_to_format(v("ip"),3))
- Processing result
ip: 192.168.11.0/24 result: 192.168.11.0-192.168.11.255
- Test data
- Output the IP address in netmask format.
- Test data
{ "ip": "192.168.11.0/24" }
- Processing rule
e_set("result",ip_to_format(v("ip"),2))
- Processing result
ip: 192.168.11.0/24 result: 192.168.11.0/255.255.255.0
- Test data
- Output the IP address in prefix format.
- Test data
{ "ip": "192.168.11.0/24" }
- Processing rule
e_set("result",ip_to_format(v("ip"),1))
- Processing result
ip: 192.168.11.0/24 result: 192.168.11.0/24
- Test data
- Output the IP address in the IP address range format.
ip_overlaps
Use the ip_overlaps function to check whether two CIDR blocks overlap.
- Function format
ip_overlaps(cidr_subnet, cidr_subnet2, default="")
- Parameter description
Parameter
Type
Mandatory
Description
cidr_subnet
String
Yes
CIDR address block 1.
cidr_subnet2
String
Yes
CIDR address block 2.
default
String
No
This value is returned when whether two CIDR address blocks overlap cannot be determined.
- Returned result
0: The two CIDR address blocks do not overlap.
1: The two CIDR address blocks overlap at the end position.
-1: The two CIDR address blocks overlap at the start position.
- Function example
- Example 1: Two CIDR blocks do not overlap.
- Test data
{ "a": "192.168.0.0/24", "b": "192.168.1.0/24" }
- Processing rule
e_set("result",ip_overlaps(v("a"),v("b")))
- Processing result
a: 192.168.0.0/24 b: 192.168.1.0/24 result: 0
- Test data
- Example 2: Two CIDR addresses overlap at the start position.
- Test data
{ "a": "192.168.1.0/24", "b": "192.168.0.0/23" }
- Processing rule
e_set("result",ip_overlaps(v("a"),v("b")))
- Processing result
a: 192.168.1.0/24 b: 192.168.0.0/23 result: 1
- Test data
- Example 3: Two CIDR addresses overlap at the end position.
- Test data
{ "a": "192.168.0.0/23", "b": "192.168.1.0/24" }
- Processing rule
e_set("result",ip_overlaps(v("a"),v("b")))
- Processing result
a: 192.168.0.0/23 b: 192.168.1.0/24 result: 1
- Test data
- Example 1: Two CIDR blocks do not overlap.
ip2long
Use the ip2long function to convert an IP address in string to a long integer.
- Function format
ip2long(value,default=0)
- Parameter description
Parameter
Type
Mandatory
Description
value
String
Yes
Value to be converted.
default
String
No
Value converted from an invalid IP address.
- Returned result
Long integer data after conversion.
- Function example
- Normal parsing.
- Test data
{ "ip": "116.209.192.0" }
- Processing rule
e_set("result",ip2long(v("ip")))
- Processing result
result: 1959903232 ip: 116.209.192.0
- Test data
- Invalid parsing.
- Test data
{ "ip": "116.209.abc.xxx" }
- Processing rule
e_set("result",ip2long(v("ip"), "error"))
- Processing result
result: error ip: 116.209.abc.xxx
- Test data
- Normal parsing.
long2ip
Use the long2ip function to convert a long integer to an IP address in string.
- Function format
long2ip(value,default="")
- Parameter description
Parameter
Type
Mandatory
Description
value
String
Yes
Value to be converted.
default
String
No
Converts invalid long integer data to an empty string.
- Returned result
IP address converted from a long integer.
- Function example
- Correct conversion.
- Test data
{ "data": "1959903232" }
- Processing rule
e_set("ip",long2ip(v("data")))
- Processing result
data: 1959903232 ip: 116.209.192.0
- Test data
- Custom error handling after conversion failure.
- Test data
{ "data": "4294967296" }
- Processing rule
e_set("ip",long2ip(v("data"), default="error"))
- Processing result
data: 4294967296 ip: error
- Test data
- Correct conversion.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot