From a1e87c49203633c059cb3aa8ca26fec2c7650292 Mon Sep 17 00:00:00 2001 From: Tamas Pal Date: Tue, 8 Jul 2014 11:01:27 +0200 Subject: [PATCH] base: ips uses ifconfig or ip according to $OSTYPE, supports IPv6 ifconfig not available to users on Linux and it has a different output, using ip instead. Also, added support to show IPv6 addresses too. --- plugins/available/base.plugin.bash | 43 ++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/plugins/available/base.plugin.bash b/plugins/available/base.plugin.bash index 4f09c515..c8ccf574 100644 --- a/plugins/available/base.plugin.bash +++ b/plugins/available/base.plugin.bash @@ -3,11 +3,50 @@ about-plugin 'miscellaneous tools' function ips () { - about 'display all ip addresses for this host' + about 'display all IPv4/6 addresses for this host' group 'base' - ifconfig | grep "inet " | awk '{ print $2 }' + + mode=4 + if [ -n "$1" ]; then + case "$1" in + -4) ;; + -6) mode=6 ;; + -a) mode=both ;; + -h) echo "Usage ips [-4|-6|-a]" + echo " -4 list only IPv4 addresses" + echo " -6 list only IPv6 addresses" + echo " -a list both IPv4 and IPv6 addresses" + return + ;; + *) echo "Illegal option $1" + echo "Only -4, -6 and -a options are allowed" + return 1 + ;; + esac + fi + + token="inet" + case $mode in + 4) ;; + 6) token="${token}6" ;; + both) token="${token}6?" ;; + esac + case "$OSTYPE" in + darwin*) + ifconfig | grep -E "${token} " | awk '{ print $2 }' + ;; + + # ifconfig is not available for users on modern linux distributions, + # also it uses a slightly different format. + # Use ip instead. + linux*) + ip ad | grep -E "${token} " | awk '{ sub(/\/.*$/,"",$2); print $2}' + ;; + esac } +alias ips6='ips -6' + function down4me () { about 'checks whether a website is down for you, or everybody'