Browse Source

Add new script to sort hosts file by ip

lennylxx 7 years ago
parent
commit
e63e6d9403
1 changed files with 18 additions and 0 deletions
  1. 18 0
      tools/sort-by-ip.py

+ 18 - 0
tools/sort-by-ip.py

@@ -0,0 +1,18 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+import sys
+import ipaddress
+
+def main():
+    with open(sys.argv[1], 'r') as infile:
+        hosts = infile.readlines()
+        hosts.sort(key=lambda x: ipaddress.ip_address(x.split()[0]))
+
+        with open(sys.argv[1] + '.out', 'w') as outfile:
+            outfile.writelines(hosts)
+
+    sys.exit(0)
+
+if __name__ == '__main__':
+    main()