summaryrefslogtreecommitdiff
path: root/cloudinit/netinfo.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/netinfo.py')
-rw-r--r--cloudinit/netinfo.py44
1 files changed, 38 insertions, 6 deletions
diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py
index feba5a62..ac3c011f 100644
--- a/cloudinit/netinfo.py
+++ b/cloudinit/netinfo.py
@@ -21,6 +21,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import cloudinit.util as util
+import re
from prettytable import PrettyTable
@@ -40,27 +41,40 @@ def netdev_info(empty=""):
toks = line.lower().strip().split()
if toks[0] == "up":
devs[curdev]['up'] = True
+ # If the output of ifconfig doesn't contain the required info in the
+ # obvious place, use a regex filter to be sure.
+ elif len(toks) > 1:
+ if re.search(r"flags=\d+<up,", toks[1]):
+ devs[curdev]['up'] = True
fieldpost = ""
if toks[0] == "inet6":
fieldpost = "6"
for i in range(len(toks)):
- if toks[i] == "hwaddr":
+ if toks[i] == "hwaddr" or toks[i] == "ether":
try:
devs[curdev]["hwaddr"] = toks[i + 1]
except IndexError:
pass
- for field in ("addr", "bcast", "mask"):
+
+ # Couple the different items we're interested in with the correct
+ # field since FreeBSD/CentOS/Fedora differ in the output.
+ ifconfigfields = {
+ "addr:": "addr", "inet": "addr",
+ "bcast:": "bcast", "broadcast": "bcast",
+ "mask:": "mask", "netmask": "mask"
+ }
+ for origfield, field in ifconfigfields.items():
target = "%s%s" % (field, fieldpost)
if devs[curdev].get(target, ""):
continue
- if toks[i] == "%s:" % field:
+ if toks[i] == "%s" % origfield:
try:
devs[curdev][target] = toks[i + 1]
except IndexError:
pass
- elif toks[i].startswith("%s:" % field):
+ elif toks[i].startswith("%s" % origfield):
devs[curdev][target] = toks[i][len(field) + 1:]
if empty != "":
@@ -73,15 +87,32 @@ def netdev_info(empty=""):
def route_info():
- (route_out, _err) = util.subp(["route", "-n"])
+ (route_out, _err) = util.subp(["netstat", "-rn"])
routes = []
entries = route_out.splitlines()[1:]
for line in entries:
if not line:
continue
toks = line.split()
- if len(toks) < 8 or toks[0] == "Kernel" or toks[0] == "Destination":
+
+ # FreeBSD shows 6 items in the routing table:
+ # Destination Gateway Flags Refs Use Netif Expire
+ # default 10.65.0.1 UGS 0 34920 vtnet0
+ #
+ # Linux netstat shows 2 more:
+ # Destination Gateway Genmask Flags MSS Window irtt Iface
+ # 0.0.0.0 10.65.0.1 0.0.0.0 UG 0 0 0 eth0
+ if (len(toks) < 6 or toks[0] == "Kernel" or
+ toks[0] == "Destination" or toks[0] == "Internet" or
+ toks[0] == "Internet6" or toks[0] == "Routing"):
continue
+
+ if len(toks) < 8:
+ toks.append("-")
+ toks.append("-")
+ toks[7] = toks[5]
+ toks[5] = "-"
+
entry = {
'destination': toks[0],
'gateway': toks[1],
@@ -92,6 +123,7 @@ def route_info():
'use': toks[6],
'iface': toks[7],
}
+
routes.append(entry)
return routes