33import os
44from sys import platform as _platform
55
6+ def run_ifconifg (interface ):
7+ if _platform == "darwin" :
8+ return os .popen ('ifconfig ' + interface + ' | grep "inet " | cut -d" " -f2 | cut -d" " -f1' ).read ().strip ()
9+ else :
10+ return os .popen ('ifconfig ' + interface + ' | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1' ).read ().strip ()
11+
12+ def is_ip (input ):
13+ inpArr = input .split ("." )
14+ if len (inpArr ) != 4 :
15+ return False
16+ for number in inpArr :
17+ try :
18+ int (number )
19+ except ValueError :
20+ return False
21+
22+ return True
23+
624#Used to store, load and manage the configuration
725class ConfigHandler ():
826 CONFIGPATH = "config.conf"
@@ -14,16 +32,21 @@ class ConfigHandler():
1432
1533 log = None
1634
17- #dirty trick to get current ip on eth0/en0/wlan0 (TODO: make it work based on settings not by guesstimation)
35+
36+ #gets ip via the unixoid ifconfig command
1837 def get_host (self ):
19- if _platform == "linux" or _platform == "linux2" :
20- self .HOST = os .popen ('ifconfig eth0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1' ).read ().strip ()
21- if _platform == "linux3" :
22- self .HOST = os .popen ('ifconfig wlan0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1' ).read ().strip ()
23- if _platform == "darwin" :
24- self .HOST = os .popen ('ifconfig en0 | grep "inet " | cut -d" " -f2 | cut -d" " -f1' ).read ().strip ()
38+ interfaces = ['en0' , 'eth0' , 'wlan0' ]
39+
2540 if _platform == "win32" or _platform == "win64" :
2641 self .log .logAndPrintWarning ("Platform not supported, using 'localhost' for host" )
42+ else :
43+ for intf in interfaces :
44+ ip = run_ifconifg (intf )
45+ if is_ip (ip ):
46+ self .HOST = ip
47+ return
48+
49+
2750
2851 #loads the config file and prints the running config
2952 def __init__ (self , path , log ):
0 commit comments