Last active
September 26, 2023 06:16
-
-
Save keyganker/419508d4ef7751b26e3dc54807d568d9 to your computer and use it in GitHub Desktop.
根据两个ip,掩码判断两个地址是否联通 #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
ip_src = sys.argv[1] | |
mask_src = sys.argv[2] | |
ip_dst = sys.argv[3] | |
mask_dst = sys.argv[4] | |
ip_src_arr = ip_src.split(".") | |
mask_src_arr = mask_src.split(".") | |
if len(ip_src_arr) != 4: | |
print "src ip format error"; | |
exit() | |
if len(mask_src_arr) != 4: | |
print "src mask format error"; | |
exit() | |
ip_dst_arr = ip_dst.split(".") | |
mask_dst_arr = mask_dst.split(".") | |
if len(ip_dst_arr) != 4: | |
print "dst ip format error"; | |
exit() | |
if len(mask_dst_arr) != 4: | |
print "dst mask format error"; | |
exit() | |
net_src_arr = [] | |
for index,item in enumerate(ip_src_arr): | |
net_src_arr.append(int(item) & int(mask_src_arr[index])) | |
net_dst_arr = [] | |
for index,item in enumerate(ip_dst_arr): | |
net_dst_arr.append(int(item) & int(mask_dst_arr[index])) | |
print 'Src net addr is: ' + '.'.join(str(x) for x in net_src_arr) | |
print 'Dst net addr is: ' + '.'.join(str(x) for x in net_dst_arr) | |
if not cmp(net_src_arr, net_dst_arr): | |
print "Connected!" | |
else: | |
print "Disconnected!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment