From b1c37ed9fe0e2cfac2ac46d757c6037c1c5436c2 Mon Sep 17 00:00:00 2001 From: John Southworth Date: Tue, 16 Aug 2011 18:26:24 -0500 Subject: Create new ping command with the following semantics 1. ping will use * ping for a v4 address * ping6 for a v6 address * ping or ping6 for a hostname which ever resolves prefering ipv4 2. ping ipv4 will ping 3. ping ipv6 will ping6 4. ping option * allows for options to pass down to ping * this is a stop gap while we add the most common options * to the CLI (cherry picked from commit 5145fd68da8f1c7f8289b99f58cb6eb96389fcef) --- scripts/vyatta-ping | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 scripts/vyatta-ping (limited to 'scripts') diff --git a/scripts/vyatta-ping b/scripts/vyatta-ping new file mode 100644 index 0000000..d454c19 --- /dev/null +++ b/scripts/vyatta-ping @@ -0,0 +1,28 @@ +#!/bin/bash +ADDR=$1 +# Regular expressions for matching an ipv4 and ipv6 address +# simple ipv4 matcher +ip4regex="^(25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$" +# based on IPv6 regex from here: http://forums.dartware.com/viewtopic.php?t=452 +ip6regex="^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:)))(%.+)?\s*$" + +# Main logic +if [[ "$ADDR" =~ $ip4regex ]]; then + /bin/ping ${@:2} $ADDR +elif [[ "$ADDR" =~ $ip6regex ]]; then + /bin/ping6 ${@:2} $ADDR +else + echo "Resolving Address: $ADDR" + if host $ADDR | awk {' print $4 '} \ + | grep -m1 -E "$ip4regex">/dev/null; then + # resolve address and check if it is ipv4 or other + /bin/ping ${@:2} $ADDR + elif host $ADDR | awk {' print $4 '} \ + | grep -m1 -E "$ip6regex">/dev/null; then + # if ipv6 resolution then ping6 + /bin/ping6 ${@:2} $ADDR + else + echo -e "\n Unknown address: [$ADDR]\n" + fi +fi + -- cgit v1.2.3