Skip to content

Commit

Permalink
Try and catch the exception of ip.addr.
Browse files Browse the repository at this point in the history
  • Loading branch information
lwindg committed Jun 10, 2015
1 parent 5c4d176 commit a2dcb26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bundle.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "route",
"version": "0.9.4-1",
"version": "0.9.4-2",
"author": "Aeluin Chen",
"email": "[email protected]",
"description": "Handle the routing table",
Expand Down
20 changes: 13 additions & 7 deletions ip/addr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sh
import netifaces
import ipcalc
import copy
import logging

# https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net
Expand Down Expand Up @@ -67,7 +68,10 @@ def ifaddresses(iface):
full = netifaces.ifaddresses(iface)

info = {}
info["mac"] = full[netifaces.AF_LINK][0]['addr']
try:
info["mac"] = full[netifaces.AF_LINK][0]['addr']
except:
info["mac"] = ""

try:
info["link"] = open("/sys/class/net/%s/operstate" % iface).read()
Expand All @@ -80,13 +84,15 @@ def ifaddresses(iface):
info["link"] = 0

info["inet"] = []
if netifaces.AF_INET not in full:
return info

for ip in full[netifaces.AF_INET]:
item = {}
item["ip"] = ip['addr']
item["netmask"] = ip['netmask']
item["broadcast"] = ip["broadcast"]
net = ipcalc.Network("%s/%s" % (ip['addr'], ip['netmask']))
item["subnet"] = str(net.network())
item = copy.deepcopy(ip)
if "addr" in item:
item["ip"] = item.pop("addr")
net = ipcalc.Network("%s/%s" % (item["ip"], item["netmask"]))
item["subnet"] = str(net.network())
info["inet"].append(item)

return info
Expand Down

0 comments on commit a2dcb26

Please sign in to comment.