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

show [ip|ipv6] interfaces cmds #194

Merged
merged 19 commits into from
Dec 16, 2018
Merged
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
124 changes: 122 additions & 2 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import errno
import json
import netaddr
import netifaces
import os
import re
import subprocess
Expand Down Expand Up @@ -845,12 +847,93 @@ def mac(vlan, port, verbose):
#

# This group houses IP (i.e., IPv4) commands and subgroups
@cli.group()
@cli.group(cls=AliasedGroup, default_if_no_args=False)
def ip():
"""Show IP (IPv4) commands"""
pass


#
# get_if_admin_state
#
# Given an interface name, return its admin state reported by the kernel.
#
def get_if_admin_state(iface):
admin_file = "/sys/class/net/{0}/flags"

try:
state_file = open(admin_file.format(iface), "r")
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return "error"

content = state_file.readline().rstrip()
flags = int(content, 16)

if flags & 0x1:
return "up"
else:
return "down"


#
# get_if_oper_state
#
# Given an interface name, return its oper state reported by the kernel.
#
def get_if_oper_state(iface):
oper_file = "/sys/class/net/{0}/carrier"

try:
state_file = open(oper_file.format(iface), "r")
except IOError as e:
print "Error: unable to open file: %s" % str(e)
return "error"

oper_state = state_file.readline().rstrip()
if oper_state == "1":
return "up"
else:
return "down"


#
# 'show ip interfaces' command
#
# Display all interfaces with an IPv4 address and their admin/oper states.
# Addresses from all scopes are included. Interfaces with no addresses are
# excluded.
#
@ip.command()
def interfaces():
"""Show interfaces IPv4 address"""
header = ['Interface', 'IPv4 address/mask', 'Admin/Oper']
data = []

interfaces = natsorted(netifaces.interfaces())

for iface in interfaces:
ipaddresses = netifaces.ifaddresses(iface)

if netifaces.AF_INET in ipaddresses:
ifaddresses = []
for ipaddr in ipaddresses[netifaces.AF_INET]:
netmask = netaddr.IPAddress(ipaddr['netmask']).netmask_bits()
ifaddresses.append(["", str(ipaddr['addr']) + "/" + str(netmask)])

if len(ifaddresses) > 0:
admin = get_if_admin_state(iface)
if admin == "up":
oper = get_if_oper_state(iface)
else:
oper = "down"
data.append([iface, ifaddresses[0][1], admin + "/" + oper])
for ifaddr in ifaddresses[1:]:
data.append(["", ifaddr[1], ""])

print tabulate(data, header, tablefmt="simple", stralign='left', missingval="")


#
# 'route' subcommand ("show ip route")
#
Expand Down Expand Up @@ -883,12 +966,49 @@ def protocol(verbose):
#

# This group houses IPv6-related commands and subgroups
@cli.group()
@cli.group(cls=AliasedGroup, default_if_no_args=False)
def ipv6():
"""Show IPv6 commands"""
pass


#
# 'show ipv6 interfaces' command
#
# Display all interfaces with an IPv6 address and their admin/oper states.
# Addresses from all scopes are included. Interfaces with no addresses are
# excluded.
#
@ipv6.command()
def interfaces():
"""Show interfaces IPv6 address"""
header = ['Interface', 'IPv6 address/mask', 'Admin/Oper']
data = []

interfaces = natsorted(netifaces.interfaces())

for iface in interfaces:
ipaddresses = netifaces.ifaddresses(iface)

if netifaces.AF_INET6 in ipaddresses:
ifaddresses = []
for ipaddr in ipaddresses[netifaces.AF_INET6]:
netmask = ipaddr['netmask'].split('/', 1)[-1]
ifaddresses.append(["", str(ipaddr['addr']) + "/" + str(netmask)])

if len(ifaddresses) > 0:
admin = get_if_admin_state(iface)
if admin == "up":
oper = get_if_oper_state(iface)
else:
oper = "down"
data.append([iface, ifaddresses[0][1], admin + "/" + oper])
for ifaddr in ifaddresses[1:]:
data.append(["", ifaddr[1], ""])

print tabulate(data, header, tablefmt="simple", stralign='left', missingval="")


#
# 'route' subcommand ("show ipv6 route")
#
Expand Down