Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try and catch the exception of ip.addr. #15

Merged
merged 1 commit into from
Jun 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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