diff --git a/pym/utils/ip.py b/pym/utils/ip.py index 0f6b190..b302c4d 100644 --- a/pym/utils/ip.py +++ b/pym/utils/ip.py @@ -15,7 +15,8 @@ # limitations under the License. import math -from cl_utils import process, checkUtils, readFile, listDirectory +from cl_utils import process, checkUtils, readFile, listDirectory, \ + readLinesFile, getRunCommands import sys import re from os import path @@ -151,15 +152,25 @@ def receiveIpAndMask(interface="eth0"): def isDhcpIp(interface="eth0"): """Get ip by dhcp or static""" # dhclient - fdhcpLeases = "/var/lib/dhcp/dhclient.leases" - if interface in readFile(fdhcpLeases): + if filter(lambda x:interface in x and ("dhcpcd" in x or "dhclient" in x), + getRunCommands()): return True - # dhcpcd - fdhcpInfo = "/var/lib/dhcpcd/dhcpcd-%s.info"%interface - fdhcpLease = "/var/lib/dhcpcd/dhcpcd-%s.lease"%interface - if path.exists(fdhcpInfo) or path.exists(fdhcpLease): - return True - return False + else: + return False + +def getRouteTable(onlyIface=[]): + """Get route table, exclude specifed iface""" + ipProg = checkUtils('/sbin/ip') + routes = process(ipProg,"route") + if onlyIface: + filterRe = re.compile("|".join(map(lambda x:r"dev %s"%x,onlyIface))) + routes = filter(filterRe.search,routes) + for line in routes: + network,op,line = line.partition(" ") + routeParams = map(lambda x:x.strip(),line.split()) + # (network,{'via':value,'dev':value}) + if network: + yield (network,dict(zip(routeParams[0::2],routeParams[1::2]))) def getInterfaces(): """Get available interfaces"""