完整的IPv4地址空间输出

时间:2019-05-27 05:23:21

标签: python ipv4

我正在尝试运行一个.py文件,该文件将输出一个.txt文件,其中包含从0.0.0.0到255.255.255.255的所有IP地址,但是在Sublime Text Console中抛出错误。这是我的.py文件:

def ipRange(start_ip, end_ip):
   start = list(map(int, start_ip.split(".")))
   end = list(map(int, end_ip.split(".")))
   temp = start
   ip_range = []

   ip_range.append(start_ip)
   while temp != end:
      start[3] += 1
      for i in (3, 2, 1):
         if temp[i] == 256:
            temp[i] = 0
            temp[i-1] += 1
      ip_range.append(".".join(map(str, temp)))    

   return ip_range

output = []   
# sample usage 
ip_range = ipRange("0.0.0.0", "255.255.255.255")
for ip in ip_range:
    output.append(ip)
fo = open("output.txt", "rw+")
line = fo.writelines(output)
fo.close()

这是我的控制台输出:

tput: No value for $TERM and no -T specified
]0;LeoLaneseltdKernel Information: 
Darwin 18.5.0 x86_64

1 个答案:

答案 0 :(得分:0)

我已经通过一些日志记录打印进行了修改,以通过以下方式调试代码流:-

   def ipRange(start_ip, end_ip):
       start = list(map(int, start_ip.split(".")))
       end = list(map(int, end_ip.split(".")))
       print('start', start)
       print('end', end)
       temp = start
       ip_range = []

       ip_range.append(start_ip)
       print('IP ADDRESS IS', ip_range)
       while temp != end:
          print('while ')
          start[3] += 1
          for i in (3, 2, 1):
             if temp[i] == 256:
                temp[i] = 0
                temp[i-1] += 1
      ip_range.append(".".join(map(str, temp)))    
      print('IP ADDRESS IS', ip_range)
      return ip_range


print("Executing the python script")
output = []   
# sample usage 
ip_range = ipRange("0.0.0.0", "0.0.0.10")
for ip in ip_range:
    print(ip)
    output.append(ip)
fo = open("output.txt", "w+")
line = fo.writelines(output)
fo.close()

请找到以下输出(我只打印10个ip地址):-

[nikhilesh@pgadmin ~]$ python 2.py
starting the python
('start', [0, 0, 0, 0])
('end', [0, 0, 0, 10])
('IP ADDRESS IS', ['0.0.0.0'])
while 
while 
while 
while 
while 
while 
while 
while 
while 
while 
('IP ADDRESS IS', ['0.0.0.0', '0.0.0.1', '0.0.0.2', '0.0.0.3', '0.0.0.4', '0.0.0.5', '0.0.0.6', '0.0.0.7', '0.0.0.8', '0.0.0.9', '0.0.0.10'])
0.0.0.0
0.0.0.1
0.0.0.2
0.0.0.3
0.0.0.4
0.0.0.5
0.0.0.6
0.0.0.7
0.0.0.8
0.0.0.9
0.0.0.10
[nikhilesh@pgadmin ~]$ cat output.txt   

0.0.0.00.0.0.10.0.0.20.0.0.30.0.0.40.0.0.50.0.0.60.0.0.70.0.0.80.0.0.90.0.0.10

我正在使用python 2.7.5版本