简体   繁体   中英

Complete IPv4 Address Space Output

I am trying to run a .py file that will output a .txt file that contains all of the IP addresses from 0.0.0.0 to 255.255.255.255, but it is throwing an error in the Sublime Text Console. This is my .py file:

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()

This is my console output:

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

I have modified by some logging print to debug the flow of the code in the following way:-

   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()

Please find the following output(I have only print 10 ip addresses):-

[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

I am using python version 2.7.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM