sort-by-ip.py 618 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import ipaddress
  5. def main():
  6. with open(sys.argv[1], 'r') as infile:
  7. hydration = []
  8. for line in infile.readlines():
  9. line = line.strip()
  10. if line == '' or line[0] == '#':
  11. continue
  12. hydration.append(line.split())
  13. hydration.sort(key=lambda x: ipaddress.ip_address(x[0]))
  14. with open(sys.argv[1] + '.out', 'w') as outfile:
  15. for host_arr in hydration:
  16. outfile.write(' '.join(host_arr) + '\r\n')
  17. sys.exit(0)
  18. if __name__ == '__main__':
  19. main()