Skip to content

Commit 70422b5

Browse files
jlevequelguohan
authored andcommitted
[show]: Add 'show interfaces alias' command to display port name/alias mapping (sonic-net#107)
1 parent 0338eee commit 70422b5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

show/main.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import click
44
import errno
55
import getpass
6+
import json
67
import os
78
import subprocess
89
import sys
910
from click_default_group import DefaultGroup
11+
from natsort import natsorted
12+
from tabulate import tabulate
1013

1114
try:
1215
# noinspection PyPep8Naming
@@ -144,6 +147,31 @@ def interfaces():
144147
"""Show details of the network interfaces"""
145148
pass
146149

150+
# 'alias' subcommand ("show interfaces alias")
151+
@interfaces.command()
152+
@click.argument('interfacename', required=False)
153+
def alias(interfacename):
154+
"""Show Interface Name/Alias Mapping"""
155+
command = 'sonic-cfggen -d --var-json "PORT"'
156+
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
157+
158+
port_dict = json.loads(p.stdout.read())
159+
160+
header = ['Name', 'Alias']
161+
body = []
162+
163+
if interfacename is not None:
164+
if interfacename in port_dict:
165+
body.append([interfacename, port_dict[interfacename]['alias']])
166+
else:
167+
click.echo("Invalid interface name, '{0}'".format(interfacename))
168+
return
169+
else:
170+
for port_name in natsorted(port_dict.keys()):
171+
body.append([port_name, port_dict[port_name]['alias']])
172+
173+
click.echo(tabulate(body, header))
174+
147175
# 'summary' subcommand ("show interfaces summary")
148176
@interfaces.command()
149177
@click.argument('interfacename', required=False)

0 commit comments

Comments
 (0)