python 判断内网IP方法及实例应用

一、初衷:

  一般在CMDB里会存储一台服务器的内网IP、管理IP、电信IP、联通IP,我们在使用的时候只需要拿到其中一个外网IP地址即可。那么我们就需要判断内网IP、管理IP并剔除掉,获取第一个外网IP。

  例如三线机房服务器:10.20.0.111(内网IP),221.222.222.33, 8.8.8.8, 1114.114.144.114, 10.20.1.100(管理IP)

 

 

二、原理代码:

内网IP可分为三类:

  • A类地址:10.0.0.0--10.255.255.255
  • B类地址:172.16.0.0--172.31.255.255
  • C类地址:192.168.0.0--192.168.255.255
局域网在选取使用私有地址时,一般会按照实际需要容纳的主机数来选择私有地址段。常见的局域网由于容量小,一般选择C类的192.168.0.0作为地址段使用,一些大型企业就需要使用B类甚至A类地址段作为内部网络的地址段。
 
实现代码:
  
 1 def ip_into_int(ip):
 2     # 先把 192.168.1.13 变成16进制的 c0.a8.01.0d ,再去了“.”后转成10进制的 3232235789 即可。
 3     # (((((192 * 256) + 168) * 256) + 1) * 256) + 13
 4     return reduce(lambda x,y:(x<<8)+y,map(int,ip.split('.')))
 5 
 6 def is_internal_ip(ip):
 7     ip = ip_into_int(ip)
 8     net_a = ip_into_int('10.255.255.255') >> 24
 9     net_b = ip_into_int('172.31.255.255') >> 20
10     net_c = ip_into_int('192.168.255.255') >> 16
11     return ip >> 24 == net_a or ip >>20 == net_b or ip >> 16 == net_c
12 
13 if __name__ == '__main__':
14     ip = '192.168.0.1'
15     print ip, is_internal_ip(ip)
16     ip = '10.2.0.1'
17     print ip, is_internal_ip(ip)
18     ip = '172.16.1.1'
19     print ip, is_internal_ip(ip)

运行结果:

[root@ ubuntu]$ python p.py
192.168.0.1 True
10.2.0.1 True
172.16.1.1 True
其中map和reduce函数的用法介绍:
>>> map(int, '12.34'.split('.'))
[12, 34]
>>> reduce(lambda x,y:(x<<8)+y, [12, 34])
3106
# 左移8位,相当于乘以256
>>> 12 * 256 + 34
3106

 

三、实例

 

 1 #!/usr/bin/python
 2 #-*-coding:utf8-*-
 3 
 4 vid = []
 5 real_host = {}
 6 
 7 #判断内网IP
 8 def ip_into_int(ip):
 9         return reduce(lambda x,y:(x<<8)+y,map(int,ip.split('.')))
10 
11 def is_internal_ip(ip):
12         ip = ip_into_int(ip)
13         net_a = ip_into_int('10.255.255.255') >> 24
14         net_b = ip_into_int('172.31.255.255') >> 20
15         net_c = ip_into_int('192.168.255.255') >> 16
16         return ip >> 24 == net_a or ip >>20 == net_b or ip >> 16 == net_c
17 
18 ef get_ips(vid):
19 
20         t = len(vid)
21         for i in range(t):
22                 host_ip = []
23                 ips_url = 'http://www.google.com/IP.do?versionId=%d ' % vid[i]
24                 ips =urllib2.urlopen(ips_url)
25                 ips_json = json.loads(ips.read())
26 
27                 t2 = len(ips_json['object'])
28                 for k in range(t2):
29                         flag = 0
30                         ip_list = ips_json['object'][k].split(',')
31 
32                         t3 =len(ip_list)
33                         for m in range(t3):
34                                 if flag ==0 and is_internal_ip(ip_list[m]) == False:
35                                         host_ip.append(ip_list[m])
36                                         flag = 1
37 
38                 real_host[vid[i]] = host_ip
39 
40         ips.close()
41         return real_host

 

 

 其中ip_list = [10.20.0.111, 221.222.222.33, 8.8.8.8, 1114.114.144.114, 10.20.1.100],通过循环依次取IP地址,判断后存第一个IP到host_ip[]。并设置flag =1 说明已取到此机器的外网IP,可以去下一个机器的了。

 

posted @ 2015-11-13 17:08  Devops达人  阅读(6102)  评论(0编辑  收藏  举报