python 生成IP地址

生成主机地址:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 def getIP(count=1000, startIP='1.1.1.1'):
 5     a1 = int(startIP.split('.')[0])
 6     a2 = int(startIP.split('.')[1])
 7     a3 = int(startIP.split('.')[2])
 8     a4 = int(startIP.split('.')[3])
 9     file = open(str(count)+'_host_list.txt', 'w')
10     for a1 in range(a1, 256):
11         for a2 in range(a2, 256):
12             for a3 in range(a3, 256):
13                 for a4 in range(a4, 256):
14                     # ip = str(a1)+'.'+str(a2)+'.'+str(a3)+'.'+str(a4)
15                     ip = '%s.%s.%s.%s' % (a1, a2, a3, a4)
16                     # print(ip)
17                     if count > 1:
18                         file.write(ip + "\n")
19                         count -= 1
20                     elif count == 1:
21                         file.write(ip)
22                         count -= 1
23                     else:
24                         file.close()
25                         return
26                 a4 = 0
27             a3 = 0
28         a2 = 0
29 
30 if __name__ == '__main__':
31     getIP(100, '11.12.1.1')

 

生成子网:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 def getIP(count=1000, startIP='1.1.1.1'):
 5     a1 = int(startIP.split('.')[0])
 6     a2 = int(startIP.split('.')[1])
 7     a3 = int(startIP.split('.')[2])
 8     a4 = int(startIP.split('.')[3])
 9     file = open(str(count)+'_Network_list.txt', 'w')
10     for a1 in range(a1, 256):
11         for a2 in range(a2, 256):
12             for a3 in range(a3, 256):
13                 for a4 in range(24, 33):
14                     # ip = str(a1)+'.'+str(a2)+'.'+str(a3)+'.'+str(a4)
15                     ip = '%s.%s.%s.%s/%s' % (a1, a2, a3, 0,a4)
16                     # print(ip)
17                     if count > 1:
18                         file.write(ip + "\n")
19                         count -= 1
20                     elif count == 1:
21                         file.write(ip)
22                         count -= 1
23                     else:
24                         file.close()
25                         return
26                 a4 = 24
27             a3 = 0
28         a2 = 0
29 
30 if __name__ == '__main__':
31     getIP(20, '10.12.1.1')

 

 

生成范围:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def getIP(count=1000, startIP='1.1.1.1'):
    a1 = int(startIP.split('.')[0])
    a2 = int(startIP.split('.')[1])
    a3 = int(startIP.split('.')[2])
    a4 = int(startIP.split('.')[3])
    file = open(str(count)+'_Range_list.txt', 'w')
    for a1 in range(a1, 256):
        for a2 in range(a2, 256):
            for a3 in range(a3, 256):
                for a4 in range(a4, 256):
                    # ip = str(a1)+'.'+str(a2)+'.'+str(a3)+'.'+str(a4)
                    ip_start = '%s.%s.%s.%s' % (a1, a2, a3, a4)
                    ip_end = '%s.%s.%s.%s' % (a1, a2, a3, a4+10)
                    # print(ip)
                    if a4>245:
                        continue
                    ip = ip_start + '-' + ip_end
                    if count > 1:
                        file.write(ip + "\n")
                        count -= 1
                    elif count == 1:
                        file.write(ip)
                        count -= 1
                    else:
                        file.close()
                        return
                a4 = 0
            a3 = 0
        a2 = 0

if __name__ == '__main__':
    getIP(1025, '10.12.1.1')

  

posted @ 2021-05-19 20:51  dmwc  阅读(926)  评论(0编辑  收藏  举报