-
Notifications
You must be signed in to change notification settings - Fork 77
Implement -br flag for brief interface output. #69
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
Open
xraystyle
wants to merge
5
commits into
brona:master
Choose a base branch
from
xraystyle:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
82330c7
Implement -br flag for brief interface output.
xraystyle 4ffa4a9
remove unnecessary else.
xraystyle 29f07e2
Fix merge conflicts while preserving functionality.
xraystyle 92fdfac
Make requested fixes, update changelog in README.
xraystyle bde20e2
Merge branch 'master' into master
xraystyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,4 @@ Kenneth Endfinger | |
| lexhuismans | ||
| Ettore Simone | ||
| luoling8192 | ||
| xraystyle | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,7 +90,7 @@ def parse_ifconfig(res, af, address): | |
| return links | ||
|
|
||
|
|
||
| def link_addr_show(argv, af, json_print, pretty_json, color, address): | ||
| def link_addr_show(argv, af, json_print, pretty_json, color, address, brief): | ||
| if len(argv) > 0 and argv[0] == "dev": | ||
| argv.pop(0) | ||
| if len(argv) > 0: | ||
|
|
@@ -114,35 +114,62 @@ def link_addr_show(argv, af, json_print, pretty_json, color, address): | |
| return json_dump(links, pretty_json) | ||
|
|
||
| for l in links: | ||
| print("%d: %s: <%s> mtu %d status %s" % ( | ||
| l["ifindex"], | ||
| colorize_ifname(color, l["ifname"]), | ||
| ",".join(l["flags"]), | ||
| l["mtu"], | ||
| colorize_op_state(color, l["operstate"]) | ||
| )) | ||
| print( | ||
| " link/" + l["link_type"] + | ||
| ((" " + colorize_mac(color, l["address"])) if "address" in l else "") + | ||
| ((" brd " + colorize_mac(color, l["broadcast"])) if "broadcast" in l else "") | ||
| ) | ||
| for a in l.get("addr_info", []): | ||
| if brief: | ||
| # Brief format: interface_name STATUS ip_addresses... | ||
| # Interface name (left-padded to align) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use https://docs.python.org/3/library/stdtypes.html#str.rjust Btw isn't this actually right padded? |
||
| ifname_colored = colorize_ifname(color, l["ifname"]) | ||
| ifname_padding = max(0, 16 - len(l["ifname"])) | ||
| line = ifname_colored + " " * ifname_padding + " " | ||
|
|
||
| # Status | ||
| status_colored = colorize_op_state(color, l["operstate"]) | ||
| status_padding = max(0, 8 - len(l["operstate"])) | ||
| line += status_colored + " " * status_padding | ||
|
|
||
| # Add addresses | ||
| addrs = [] | ||
| for a in l.get("addr_info", []): | ||
| addr_str = "%s/%d" % (colorize_inet(color, a["family"], a["local"]), a["prefixlen"]) | ||
| addrs.append(addr_str) | ||
|
|
||
| # For link output, show MAC address | ||
| if not address and "address" in l: | ||
| addrs.insert(0, colorize_mac(color, l["address"])) | ||
|
|
||
| # Add minimum 7 spaces before first address, then single space between addresses | ||
| if addrs: | ||
| line += " " + " ".join(addrs) | ||
| print(line) | ||
| else: | ||
| print("%d: %s: <%s> mtu %d status %s" % ( | ||
| l["ifindex"], | ||
| colorize_ifname(color, l["ifname"]), | ||
| ",".join(l["flags"]), | ||
| l["mtu"], | ||
| colorize_op_state(color, l["operstate"]) | ||
| )) | ||
| print( | ||
| " %s %s" % (a["family"], colorize_inet(color, a["family"], a["local"])) + | ||
| ((" peer %s" % colorize_inet(color, a["family"], a["address"])) if "address" in a else "") + | ||
| "/%d" % (a["prefixlen"]) + | ||
| ((" brd " + colorize_inet(color, a["family"], a["broadcast"])) if "broadcast" in a else "") | ||
| " link/" + l["link_type"] + | ||
| ((" " + colorize_mac(color, l["address"])) if "address" in l else "") + | ||
| ((" brd " + colorize_mac(color, l["broadcast"])) if "broadcast" in l else "") | ||
| ) | ||
| for a in l.get("addr_info", []): | ||
| print( | ||
| " %s %s" % (a["family"], colorize_inet(color, a["family"], a["local"])) + | ||
| ((" peer %s" % colorize_inet(color, a["family"], a["address"])) if "address" in a else "") + | ||
| "/%d" % (a["prefixlen"]) + | ||
| ((" brd " + colorize_inet(color, a["family"], a["broadcast"])) if "broadcast" in a else "") | ||
| ) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| # Help | ||
| def do_help(argv=None, af=None, json_print=None, pretty_json=None, color=None): | ||
| def do_help(argv=None, af=None, json_print=None, pretty_json=None, color=None, brief=None): | ||
| perror("Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }") | ||
| perror("where OBJECT := { link | addr | route | neigh }") | ||
| perror(" OPTIONS := { -V[ersion] | -j[son] | -p[retty] | -c[olor] |") | ||
| perror(" -4 | -6 }") | ||
| perror(" -br[ief] | -4 | -6 }") | ||
| perror(HELP_ADDENDUM) | ||
| exit(255) | ||
|
|
||
|
|
@@ -184,7 +211,7 @@ def do_help_neigh(): | |
|
|
||
| # Route Module | ||
| @help_msg(do_help_route) | ||
| def do_route(argv, af, json_print, pretty_json, color): | ||
| def do_route(argv, af, json_print, pretty_json, color, brief): | ||
| if not argv or ( | ||
| any_startswith(["show", "lst", "list"], argv[0]) and len(argv) == 1 | ||
| ): | ||
|
|
@@ -409,13 +436,13 @@ def do_route_get(argv, af, json_print, pretty_json, color): | |
|
|
||
| # Addr Module | ||
| @help_msg(do_help_addr) | ||
| def do_addr(argv, af, json_print, pretty_json, color): | ||
| def do_addr(argv, af, json_print, pretty_json, color, brief): | ||
| if not argv: | ||
| argv.append("show") | ||
|
|
||
| if any_startswith(["show", "lst", "list"], argv[0]): | ||
| argv.pop(0) | ||
| return do_addr_show(argv, af, json_print, pretty_json, color) | ||
| return do_addr_show(argv, af, json_print, pretty_json, color, brief) | ||
| elif "add".startswith(argv[0]) and len(argv) >= 3: | ||
| argv.pop(0) | ||
| return do_addr_add(argv, af) | ||
|
|
@@ -427,8 +454,8 @@ def do_addr(argv, af, json_print, pretty_json, color): | |
| return True | ||
|
|
||
|
|
||
| def do_addr_show(argv, af, json_print, pretty_json, color): | ||
| return link_addr_show(argv, af, json_print, pretty_json, color, True) | ||
| def do_addr_show(argv, af, json_print, pretty_json, color, brief): | ||
| return link_addr_show(argv, af, json_print, pretty_json, color, True, brief) | ||
|
|
||
|
|
||
| def do_addr_add(argv, af): | ||
|
|
@@ -481,13 +508,13 @@ def do_addr_del(argv, af): | |
|
|
||
| # Link module | ||
| @help_msg(do_help_link) | ||
| def do_link(argv, af, json_print, pretty_json, color): | ||
| def do_link(argv, af, json_print, pretty_json, color, brief): | ||
| if not argv: | ||
| argv.append("show") | ||
|
|
||
| if any_startswith(["show", "lst", "list"], argv[0]): | ||
| argv.pop(0) | ||
| return do_link_show(argv, af, json_print, pretty_json, color) | ||
| return do_link_show(argv, af, json_print, pretty_json, color, brief) | ||
| elif "set".startswith(argv[0]): | ||
| argv.pop(0) | ||
| return do_link_set(argv, af) | ||
|
|
@@ -496,8 +523,8 @@ def do_link(argv, af, json_print, pretty_json, color): | |
| return True | ||
|
|
||
|
|
||
| def do_link_show(argv, af, json_print, pretty_json, color): | ||
| return link_addr_show(argv, af, json_print, pretty_json, color, False) | ||
| def do_link_show(argv, af, json_print, pretty_json, color, brief): | ||
| return link_addr_show(argv, af, json_print, pretty_json, color, False, brief) | ||
|
|
||
|
|
||
| def do_link_set(argv, af): | ||
|
|
@@ -550,7 +577,7 @@ def do_link_set(argv, af): | |
|
|
||
| # Neigh module | ||
| @help_msg(do_help_neigh) | ||
| def do_neigh(argv, af, json_print, pretty_json, color): | ||
| def do_neigh(argv, af, json_print, pretty_json, color, brief): | ||
| if not argv: | ||
| argv.append("show") | ||
|
|
||
|
|
@@ -694,6 +721,7 @@ def main(argv): | |
| af = -1 # default / both | ||
| json_print = False | ||
| pretty_json = False | ||
| brief = False | ||
| color_mode = "never" | ||
|
|
||
| while argv and argv[0].startswith("-"): | ||
|
|
@@ -706,6 +734,9 @@ def main(argv): | |
| elif argv[0] == "-4": | ||
| af = 4 | ||
| argv.pop(0) | ||
| elif "-brief".startswith(argv[0]): | ||
| brief = True | ||
| argv.pop(0) | ||
| elif "-color".startswith(argv[0].split("=")[0]): | ||
| # 'always' is default if -color is set without any value | ||
| color_mode = argv[0].split("=")[1] if "=" in argv[0] else "always" | ||
|
|
@@ -738,7 +769,7 @@ def main(argv): | |
| argv.pop(0) | ||
| # Functions return true or terminate with exit(255) | ||
| # See help_msg and do_help* | ||
| return cmd_func(argv, af, json_print, pretty_json, color_scheme) | ||
| return cmd_func(argv, af, json_print, pretty_json, color_scheme, brief) | ||
|
|
||
| perror('Object "{}" is unknown, try "ip help".'.format(argv[0])) | ||
| exit(1) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please merge it with the HEAD section below.