detect ipv4 on osx
This commit is contained in:
parent
dc2121293e
commit
d2bec145fa
1 changed files with 22 additions and 8 deletions
14
oml/utils.py
14
oml/utils.py
|
@ -140,6 +140,7 @@ def get_interface():
|
|||
def get_local_ipv4():
|
||||
ip = socket.gethostbyaddr(socket.getfqdn())[-1][0]
|
||||
if ip == '127.0.0.1':
|
||||
if sys.platform == 'linux2':
|
||||
cmd = ['ip', 'route', 'show']
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate()
|
||||
|
@ -148,6 +149,19 @@ def get_local_ipv4():
|
|||
dev = local[0].split(' ')[4]
|
||||
local_ip = [l for l in stdout.split('\n') if dev in l and not 'default' in l]
|
||||
return [p for p in local_ip[0].split(' ')[1:] if '.' in p][0]
|
||||
else:
|
||||
cmd = ['/sbin/route', '-n', 'get', 'default']
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate()
|
||||
interface = [[p.strip() for p in s.split(':', 1)] for s in stdout.strip().split('\n') if 'interface' in s]
|
||||
if interface:
|
||||
interface = interface[0][1]
|
||||
cmd = ['ifconfig', interface]
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate()
|
||||
ips = [l for l in stdout.split('\n') if 'inet ' in l]
|
||||
if ips:
|
||||
ip = ips[0].strip().split(' ')[1]
|
||||
return ip
|
||||
|
||||
def update_dict(root, data):
|
||||
|
|
Loading…
Reference in a new issue