Browse Source

fixed ip regex for linux

New Future 7 years ago
parent
commit
650ac94291
1 changed files with 3 additions and 5 deletions
  1. 3 5
      util/ip.py

+ 3 - 5
util/ip.py

@@ -70,17 +70,15 @@ def ip_regex_match(parrent_regex, match_regex):
 
     for s in os.popen(cmd).readlines():
         addr = ip_pattern.search(s)
-        if addr:
-            print addr.group(1)
         if addr and matcher.match(addr.group(1)):
             return addr.group(1)
 
 
 def regex_v4(reg): # ipv4 正则提取
     if os.name == 'nt':  # Windows: IPv4 xxx: 192.168.1.2
-        regex_str = r'IPv4 .*: ((\d{1,3}\.){3}\d{1,3})\W'
+        regex_str = r'IPv4 .*: ((?:\d{1,3}\.){3}\d{1,3})\W'
     else:
-        regex_str = r'inet [\w\:]*((\d{1,3}\.){3}\d{1,3})\s'
+        regex_str = r'inet (?:addr\:)?((?:\d{1,3}\.){3}\d{1,3})\s'
     return ip_regex_match(regex_str, reg)
 
 
@@ -88,5 +86,5 @@ def regex_v6(reg): # ipv6 正则提取
     if os.name == 'nt':  # Windows: IPv4 xxx: ::1
         regex_str = r'IPv6 .*: ([\:\dabcdef]*)?\W'
     else:
-        regex_str = r'inet6 [\w\:]*([\:\dabcdef]*)?\s'
+        regex_str = r'inet6 (?:addr\:)?([\:\dabcdef]*)?\s'
     return ip_regex_match(regex_str, reg)