diff --git a/oml/localnodes.py b/oml/localnodes.py index 8e0763c..2f89009 100644 --- a/oml/localnodes.py +++ b/oml/localnodes.py @@ -6,8 +6,6 @@ import json import logging import socket import struct -import subprocess -import sys import thread from threading import Thread diff --git a/oml/utils.py b/oml/utils.py index 71658c5..4789933 100644 --- a/oml/utils.py +++ b/oml/utils.py @@ -138,30 +138,30 @@ def get_interface(): return 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'] + if sys.platform == 'darwin': + 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() - local = [l for l in stdout.split('\n') if 'default' in l] - if local: - 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] + ips = [l for l in stdout.split('\n') if 'inet ' in l] + if ips: + ip = ips[0].strip().split(' ')[1] + else: + cmd = ['ip', 'route', 'show'] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE) + stdout, stderr = p.communicate() + local = [l for l in stdout.split('\n') if 'default' in l] + if local: + dev = local[0].split(' ')[4] + local_ip = [l for l in stdout.split('\n') + if dev in l and not 'default' in l] + ip = [p for p in local_ip[0].split(' ')[1:] if '.' in p][0] return ip def update_dict(root, data):