detect ipv4 on osx

This commit is contained in:
j 2014-05-22 16:40:18 +02:00
parent dc2121293e
commit d2bec145fa

View file

@ -140,14 +140,28 @@ def get_interface():
def get_local_ipv4(): def get_local_ipv4():
ip = socket.gethostbyaddr(socket.getfqdn())[-1][0] ip = socket.gethostbyaddr(socket.getfqdn())[-1][0]
if ip == '127.0.0.1': if ip == '127.0.0.1':
cmd = ['ip', 'route', 'show'] if sys.platform == 'linux2':
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) cmd = ['ip', 'route', 'show']
stdout, stderr = p.communicate() p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
local = [l for l in stdout.split('\n') if 'default' in l] stdout, stderr = p.communicate()
if local: local = [l for l in stdout.split('\n') if 'default' in l]
dev = local[0].split(' ')[4] if local:
local_ip = [l for l in stdout.split('\n') if dev in l and not 'default' in l] dev = local[0].split(' ')[4]
return [p for p in local_ip[0].split(' ')[1:] if '.' in p][0] 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 return ip
def update_dict(root, data): def update_dict(root, data):