Advertisement
Guest User

r.py

a guest
Aug 25th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. #!/usr/bin/python
  2. #This simple script reads a list of IP ranges in CIDR notation
  3. # and prints the individual IP addresses.
  4. #
  5. #Posted this script because I got tired of re-writing the same script
  6. # everytime I needed this. If you write security tools, please support
  7. # CIDR notation.
  8. #
  9. #Usage: ./cidrToIps.py -i <inputfile>
  10. #
  11. #$ cat cidr-list.txt
  12. #192.168.0.1/29
  13. #192.168.1.1/30
  14. #$ ./cidrToIps.py -i cidr-list.txt
  15. #Reading IP ranges in CIDR notation from file: cidr-list.txt
  16. #192.168.0.0
  17. #192.168.0.1
  18. #192.168.0.2
  19. #192.168.0.3
  20. #192.168.0.4
  21. #192.168.0.5
  22. #192.168.0.6
  23. #192.168.0.7
  24. #192.168.1.0
  25. #192.168.1.1
  26. #192.168.1.2
  27. #192.168.1.3
  28. #
  29. #calderon@websec.mx
  30. #
  31.  
  32. from netaddr import IPNetwork
  33. import sys
  34. import getopt
  35.  
  36. def main(argv):
  37.   inputfile = ''
  38.   try:
  39.     opts, args = getopt.getopt(argv,"i:",["ifile="])
  40.   except getopt.GetoptError:
  41.     print 'cidrToIps.py -i <inputfile>'
  42.     sys.exit(2)
  43.   for opt, arg in opts:
  44.     if opt == '-h':
  45.       print 'cidrToIps.py -i <inputfile>'
  46.       sys.exit()
  47.     elif opt in ("-i", "--ifile"):
  48.       inputfile = arg
  49.  
  50.   print 'Reading IP ranges in CIDR notation from file:', inputfile
  51.   fo = open(inputfile, "r+")
  52.   for line in fo:
  53.     for ip in IPNetwork(line):
  54.       f = open("output.txt" , "w")
  55.       f.write('%s' % ip)
  56. if __name__ == "__main__":
  57.   main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement