From bbc2a15795bfbe11febe8c00d2a592ba5cf8daf5 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Mon, 28 Dec 2020 16:13:03 +0100 Subject: xml: completion-help: add helper for all local assigned IP addresses This replaces the Python script by a bash variant which is much faster as the Python interpreter does not need to be launched on invocation. --- src/completion/list_local.py | 24 ------------------------ src/completion/list_local_ips.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 24 deletions(-) delete mode 100755 src/completion/list_local.py create mode 100755 src/completion/list_local_ips.sh (limited to 'src') diff --git a/src/completion/list_local.py b/src/completion/list_local.py deleted file mode 100755 index 40cc95f1e..000000000 --- a/src/completion/list_local.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env python3 - -import json -import argparse - -from vyos.util import cmd - -# [{"ifindex":1,"ifname":"lo","flags":["LOOPBACK","UP","LOWER_UP"],"mtu":65536,"qdisc":"noqueue","operstate":"UNKNOWN","group":"default","txqlen":1000,"link_type":"loopback","address":"00:00:00:00:00:00","broadcast":"00:00:00:00:00:00","addr_info":[{"family":"inet","local":"127.0.0.1","prefixlen":8,"scope":"host","label":"lo","valid_life_time":4294967295,"preferred_life_time":4294967295},{"family":"inet6","local":"::1","prefixlen":128,"scope":"host","valid_life_time":4294967295,"preferred_life_time":4294967295}]},{"ifindex":2,"ifname":"eth0","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"pfifo_fast","operstate":"UP","group":"default","txqlen":1000,"link_type":"ether","address":"08:00:27:fa:12:53","broadcast":"ff:ff:ff:ff:ff:ff","addr_info":[{"family":"inet","local":"10.0.2.15","prefixlen":24,"broadcast":"10.0.2.255","scope":"global","label":"eth0","valid_life_time":4294967295,"preferred_life_time":4294967295},{"family":"inet6","local":"fe80::a00:27ff:fefa:1253","prefixlen":64,"scope":"link","valid_life_time":4294967295,"preferred_life_time":4294967295}]},{"ifindex":3,"ifname":"eth1","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"pfifo_fast","operstate":"UP","group":"default","txqlen":1000,"link_type":"ether","address":"08:00:27:0d:25:dc","broadcast":"ff:ff:ff:ff:ff:ff","addr_info":[{"family":"inet6","local":"fe80::a00:27ff:fe0d:25dc","prefixlen":64,"scope":"link","valid_life_time":4294967295,"preferred_life_time":4294967295}]},{"ifindex":4,"ifname":"eth2","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"pfifo_fast","operstate":"UP","group":"default","txqlen":1000,"link_type":"ether","address":"08:00:27:68:d0:b1","broadcast":"ff:ff:ff:ff:ff:ff","addr_info":[{"family":"inet6","local":"fe80::a00:27ff:fe68:d0b1","prefixlen":64,"scope":"link","valid_life_time":4294967295,"preferred_life_time":4294967295}]},{"ifindex":5,"ifname":"eth3","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"pfifo_fast","operstate":"UP","group":"default","txqlen":1000,"link_type":"ether","address":"08:00:27:f0:17:c5","broadcast":"ff:ff:ff:ff:ff:ff","addr_info":[{"family":"inet6","local":"fe80::a00:27ff:fef0:17c5","prefixlen":64,"scope":"link","valid_life_time":4294967295,"preferred_life_time":4294967295}]}] - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - group = parser.add_mutually_exclusive_group() - - out = cmd('ip -j address show') - data = json.loads(out) - - - interfaces = [] - for interface in data: - if not 'addr_info' in interface: - continue - interfaces.extend(interface['addr_info']) - - print(' '.join([interface['local'] for interface in interfaces if 'local' in interface])) diff --git a/src/completion/list_local_ips.sh b/src/completion/list_local_ips.sh new file mode 100755 index 000000000..a506ce16e --- /dev/null +++ b/src/completion/list_local_ips.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +ipv4=0 +ipv6=0 + +while [[ "$#" -gt 0 ]]; do + case $1 in + -4|--ipv4) ipv4=1 ;; + -6|--ipv6) ipv6=1 ;; + -b|--both) ipv4=1; ipv6=1 ;; + *) echo "Unknown parameter passed: $1" ;; + esac + shift +done + +if [ $ipv4 -eq 1 ] && [ $ipv6 -eq 1 ]; then + ip a | grep inet | awk '{print $2}' | sed -e /^fe80::/d | awk -F/ '{print $1}' +elif [ $ipv4 -eq 1 ] ; then + ip a | grep 'inet ' | awk '{print $2}' | awk -F/ '{print $1}' +elif [ $ipv6 -eq 1 ] ; then + ip a | grep 'inet6 ' | awk '{print $2}' | sed -e /^fe80::/d | awk -F/ '{print $1}' +else + echo "Usage:" + echo "-4|--ipv4 list only IPv4 addresses" + echo "-6|--ipv6 list only IPv6 addresses" + echo "--both list both IP4 and IPv6 addresses" + echo "" + exit 1 +fi -- cgit v1.2.3