Skip to content

Commit

Permalink
[show]: Add 'show interfaces alias' command to display port name/alia…
Browse files Browse the repository at this point in the history
…s mapping (#107)
  • Loading branch information
jleveque authored and lguohan committed Sep 27, 2017
1 parent 0338eee commit 70422b5
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import click
import errno
import getpass
import json
import os
import subprocess
import sys
from click_default_group import DefaultGroup
from natsort import natsorted
from tabulate import tabulate

try:
# noinspection PyPep8Naming
Expand Down Expand Up @@ -144,6 +147,31 @@ def interfaces():
"""Show details of the network interfaces"""
pass

# 'alias' subcommand ("show interfaces alias")
@interfaces.command()
@click.argument('interfacename', required=False)
def alias(interfacename):
"""Show Interface Name/Alias Mapping"""
command = 'sonic-cfggen -d --var-json "PORT"'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

port_dict = json.loads(p.stdout.read())

header = ['Name', 'Alias']
body = []

if interfacename is not None:
if interfacename in port_dict:
body.append([interfacename, port_dict[interfacename]['alias']])
else:
click.echo("Invalid interface name, '{0}'".format(interfacename))
return
else:
for port_name in natsorted(port_dict.keys()):
body.append([port_name, port_dict[port_name]['alias']])

click.echo(tabulate(body, header))

# 'summary' subcommand ("show interfaces summary")
@interfaces.command()
@click.argument('interfacename', required=False)
Expand Down

0 comments on commit 70422b5

Please sign in to comment.