|
3 | 3 | import click
|
4 | 4 | import errno
|
5 | 5 | import getpass
|
| 6 | +import json |
6 | 7 | import os
|
7 | 8 | import subprocess
|
8 | 9 | import sys
|
9 | 10 | from click_default_group import DefaultGroup
|
| 11 | +from natsort import natsorted |
| 12 | +from tabulate import tabulate |
10 | 13 |
|
11 | 14 | try:
|
12 | 15 | # noinspection PyPep8Naming
|
@@ -144,6 +147,31 @@ def interfaces():
|
144 | 147 | """Show details of the network interfaces"""
|
145 | 148 | pass
|
146 | 149 |
|
| 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 | + |
147 | 175 | # 'summary' subcommand ("show interfaces summary")
|
148 | 176 | @interfaces.command()
|
149 | 177 | @click.argument('interfacename', required=False)
|
|
0 commit comments