From bbfd03b6ccc215318f212e93c4f5b4aabcaf2b24 Mon Sep 17 00:00:00 2001 From: Steven Pack Date: Sat, 29 Aug 2020 20:03:00 -0700 Subject: [PATCH] delete unused script --- scripts/asn | 312 ---------------------------------------------------- 1 file changed, 312 deletions(-) delete mode 100755 scripts/asn diff --git a/scripts/asn b/scripts/asn deleted file mode 100755 index cba349a..0000000 --- a/scripts/asn +++ /dev/null @@ -1,312 +0,0 @@ -#!/usr/bin/env bash - -################################################################################################################ -# ----------------------------------------------------------------------------------- -# ASN/IPv4/IPv6/Prefix/AS Path lookup tool. Uses Team Cymru's whois service for data. -# ----------------------------------------------------------------------------------- -# example usage: -# asn -- to lookup matching ASN data. Supports "as123" and "123" formats (case insensitive) -# asn -- to lookup matching route(4/6) and ASN data -# asn -- to lookup matching ASN data -# asn -- to lookup matching IP(v4/v6), route and ASN data (supports multiple IPs - e.g. DNS RR) -# -# Author: Adriano Provvisiero - BV Networks 2017-2020 -# -################################################################################################################ - -WhoisASN(){ - found_asname=$(whois -h whois.cymru.com " -f -w -c -p as$1" | sed -e 's/\ *|\ */|/g' | awk -F '[|]' '{print $3}') - [[ "$found_asname" == "NO_NAME" ]] && found_asname="" -} - -WhoisIP(){ - printf "${white}%${longest}s -> ${yellow}(route: %20s) ${white}-> ${red}[AS%s] ${green}%s\n" "$1" "$found_route" "$found_asn" "$found_asname" -} - -LookupASNAndRouteFromIP(){ - found_route="" - found_asn="" - found_asname="" - output=$(whois -h whois.cymru.com " -f -p $1" | sed -e 's/\ *|\ */|/g') - found_asn=$(echo "$output" | awk -F'[|]' '{print $1}') - found_asname=$(echo "$output" | awk -F'[|]' '{print $4}') - found_route=$(echo "$output" | awk -F'[|]' '{print $3}') - [[ "$found_asname" == "NA" ]] && found_asname="" - [[ "$found_route" == "NA" ]] && found_route="" -} - -ResolveHostnameToIPList(){ - ip=$(host "$1" 2>/dev/null | grep -Eo "$ipv4v6regex") - echo -e "$ip\n" -} - -PrintUsage(){ - programname=$(basename "$0") - echo -e "\nASN Lookup Tool\ - \n\n\nUsage:\n\t${white}$programname [-n] TARGET${default}\n\nOptions:\n\ - \n ${white}-n, --notrace\n\t${default}Disable tracing the AS path to the TARGET\n\nSupported targets:\n\ - \n ${white}\n\t(to lookup matching ASN data. Supports \"as123\" and \"123\" formats - case insensitive)\ - \n ${white}\n\t(to lookup matching route and ASN data)\ - \n ${white}\n\t(to lookup matching ASN data)\ - \n ${white}\n\t(to lookup matching IP, route and ASN data. Supports multiple IPs - e.g. DNS RR)\ - \n\ - \nNote: AS path tracing will be performed only for single IPs/DNS lookup results. - " -} - -TraceASPath(){ - host_to_trace="$1" - mtr_rounds=3 - collecting_message="\n${lightgreybg}${black}Collecting mtr output (press CTRL-C to cancel)...${default}" - echo -en "$collecting_message" - # cache the previously looked-up AS to save one lookup (target/last hop) - saved_asn=$found_asn - saved_asname=$found_asname - # last_resolved_ip will save us one dns resolution (mtr returns the same last hop twice) - last_resolved_ip="" - privateregex='/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/' # cheers https://stackoverflow.com/a/11327345/5377165 - # start the mtr trace in a bash coprocess to parse its output asynchronously - coproc mtr -l -n -c"$mtr_rounds" "$host_to_trace" - declare -a hostline_array - declare -a dnsline_array - declare -a pingline_array - declare -a aspath_array - while true; do - # read mtr output from the bash coprocess pipe, - # parsing raw mtr results beginning with : - # - h ("hostline", or IPs) - # - p ("pingline", or latencies) - # see https://github.com/traviscross/mtr/blob/master/FORMATS - if read -r -u "${COPROC[0]}" raw_mtr_line 2>/dev/null; then - mtr_type=$(echo "$raw_mtr_line" | cut -d ' ' -f 1) - mtr_hopnum=$(echo "$raw_mtr_line" | cut -d ' ' -f 2) - mtr_data=$(echo "$raw_mtr_line" | cut -d ' ' -f 3) - case "$mtr_type" in - "h") - # this is a hostline ($mtr_data is an IP address) - hostline_array["$mtr_hopnum"]="$mtr_data" - # do a reverse DNS lookup for the IP instead of relying on - # mtr's dnsline results since it proved unreliable in dnsmasq scenarios. - # See https://github.com/nitefood/asn/pull/3 - if [ "$mtr_data" != "$last_resolved_ip" ]; then - last_resolved_ip="$mtr_data" - if hostname=$(host "$mtr_data"); then - dnsline_array["$mtr_hopnum"]=$(echo "$hostname" |\ - awk 'NR==1{sub(/\.\r?$/, "", $NF); print $NF}') # get first line and remove trailing dot and CR (Cygwin) from hostname - fi - fi - ;; - "p") - # this is a pingline ($mtr_data is a latency value in microseconds) - cur_latency_sum=${pingline_array["$mtr_hopnum"]} - pingline_array["$mtr_hopnum"]=$(echo "$cur_latency_sum $mtr_data" |\ - awk '{ sum = $1 + $2; printf ("%.1f\n", sum) }') # we store the total sum of reply times in the array, and will average it at display time - ;; - esac - else - break - fi - done - # mtr finished, parse and format results - trace_output=$(printf "${lightgreybg}${black}%4s %-72s%18s %s ${default}" "Hop" "IP Address" "Ping avg" "AS Information") - cur_hop_count=1 - last_hop=false - for mtr_hopnum in "${!hostline_array[@]}"; do - hop_ip=${hostline_array[$mtr_hopnum]} - # AS DATA lookup - # check if IP is in private addressing space - if [[ $hop_ip =~ $privateregex ]]; then - asn_data="${white}(Private network)${default}" - else - # not a private address, try saving a lookup - # (if $hop_ip = our target, ie we're at the last hop) - if [ "$hop_ip" = "$host_to_trace" ]; then - found_asn=$saved_asn - found_asname=$saved_asname - # avoid adding the same AS multiple times in a row in the summary path - aspath_entry=$(printf "AS%s [%s]${default}" "$found_asn" "$(echo "$found_asname" | cut -d ',' -f 1)" ) - if [[ ${#aspath_array[@]} -eq 0 ]] || [[ "${aspath_array[-1]}" != "$aspath_entry" ]]; then - aspath_array+=("$aspath_entry") - fi - asn_data="${red}[AS$found_asn] ${green}$found_asname${default}" - last_hop=true - else - # not the last hop. Lookup hop data - LookupASNAndRouteFromIP "$hop_ip" - if [ -z "$found_asname" ] && [ -z "$found_route" ]; then - # no data found - asn_data="${yellow}(No data)${default}" - else - # lookup success - asn_data="${red}[AS$found_asn] ${green}$found_asname${default}" - # avoid adding the same AS multiple times in a row in the summary path - aspath_entry=$(printf "AS%s [%s]${default}" "$found_asn" "$(echo "$found_asname" | cut -d ',' -f 1)" ) - if [[ ${#aspath_array[@]} -eq 0 ]] || [[ "${aspath_array[-1]}" != "$aspath_entry" ]]; then - aspath_array+=("$aspath_entry") - fi - fi - fi - fi - - # DNS data (only used if a hostname was resolved) - if [ -n "${dnsline_array[$mtr_hopnum]}" ]; then - saveip=$hop_ip - hop_ip="${dnsline_array[$mtr_hopnum]} ($saveip)" - fi - - # PING data - # account for missing (no reply) hops - while [ "$mtr_hopnum" -ge "$((cur_hop_count))" ]; do - trace_output+=$(printf "\n%3s. %-90s %10s %s" "$cur_hop_count" "${white}???${default}" "*" "${white}(No reply)${default}") - ((cur_hop_count++)) - done - # data in the array item is the total sum of mtr latencies collected for this hop in microseconds. - # convert it to milliseconds, and round it to 1 digit precision (mtr's output style) - ping_total_usec=${pingline_array[$mtr_hopnum]} - if [ -n "$ping_total_usec" ]; then - ping_average_msec=$(echo "$ping_total_usec $mtr_rounds" | awk '{ avg = $1 / $2; printf( "%.1f\n", avg/1000) }') - ping_data="$ping_average_msec ms" - fi - trace_output+=$(printf "\n%3s. %-90s %10s %s" "$cur_hop_count" "${white}$hop_ip${default}" "${ping_data}" "$asn_data") - [[ "$last_hop" = true ]] && break - (( cur_hop_count++ )) - done - printf "\r%${#collecting_message}s\r" " " # to delete the previous "collecting" text - echo -en "${green}===== ${white}AS path to $userinput ${green}=====${default}\n\n (localhost)" - for as in "${aspath_array[@]}"; do - echo -en " -> ${yellow}${as}${default}" - done - echo -e " -> (target)\n\n${green}===== ${white}Detailed trace ${green}=====${default}\n\n${trace_output}\n" -} - -PrintErrorAndExit(){ - echo -e "\n\e[30m\e[101m${1}\e[39m\e[49m\n" - tput sgr0 - exit 1 -} - -Ctrl_C() { - PrintErrorAndExit "Interrupted" -} - -IFS=$'\n\t' -green="\e[32m" -yellow="\e[33m" -white="\e[97m" -blue="\e[94m" -red="\e[31m" -black="\e[30m" -lightgreybg="\e[47m" -default="\e[0m" - -trap Ctrl_C INT - -ipv4v6regex='[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|'\ -'([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|'\ -'([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|'\ -':((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|'\ -'(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|'\ -'1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))' # cheers https://stackoverflow.com/a/17871737 - -# Check cmdline parameters -if [[ $# -lt 1 ]]; then - PrintUsage - exit 1 -fi -case "$1" in - "-n"|"--notrace") - if [[ $# -lt 2 ]]; then - PrintUsage - echo -e "Error: missing ${red}TARGET${default}\n" - exit 1 - fi - TRACEASNPATH=false - userinput="$2" - ;; - *) - if [ "${1:0:1}" = "-" ]; then - PrintUsage - echo -e "Error: unknown option ${red}$1${default}\n" - exit 1 - else - TRACEASNPATH=true - userinput="$1" - fi - ;; -esac - -# Check prerequisites -# TODO: consider switching to DNS lookups (ie dig instead of whois) to improve speed -if [ $TRACEASNPATH = true ] && [ -z "$(command -v mtr)" ]; then - echo -e "\n${yellow}WARNING: ${white}program '${red}mtr${white}' not found on this system.\nPlease install it (for example with ${green}sudo apt install mtr-tiny${white}" \ - "\non Debian-based linux distributions) to enable AS path tracing" - TRACEASNPATH=false -fi -if [ -z "$(command -v whois)" ]; then - echo -e "\n${red}ERROR: ${white}program '${red}whois${white}' not found on this system.\nPlease install it (for example with ${green}sudo apt install whois${white} on Debian-based linux distributions)\n" - exit 1 -fi -if [ -z "$(command -v host)" ]; then - echo -e "\n${red}ERROR: ${white}program '${red}host${white}' not found on this system.\nPlease install it (for example with ${green}sudo apt install bind9-host${white} on Debian-based linux distributions)\n" - exit 1 -fi - -input=$(echo "$userinput" | sed -e 's/\/.*//g' | grep -Eo "$ipv4v6regex") -if [ -z "$input" ]; then - # Input is not an IP Address. Check if it is a number (ASN) - asn=$(echo "$userinput" | sed -e 's/[a|A][s|S]//g' | grep -E "^[0-9]*$") - if [ -z "$asn" ]; then - # Input is not an ASN either. Consider it a hostname and try to resolve it. - echo -e -n "\n${blue}Resolving \"$userinput\"... " - ip=$(ResolveHostnameToIPList "$userinput") - if [ -z "$ip" ]; then - PrintErrorAndExit "Error: unable to resolve hostname" - fi - numips=$(echo "$ip" | wc -l) - [[ $numips = 1 ]] && s="" || s="es" - echo -e "${blue}$numips IP address$s found:" - # grab the longest IP to properly size output padding - longest=0 - for singleip in $ip; do - [[ ${#singleip} -gt $longest ]] && longest=${#singleip} - done - (( longest++ )) - # output actual results - for singleip in $ip; do - LookupASNAndRouteFromIP "$singleip" - WhoisIP "$singleip" - done - # Check if AS path tracing is requested - if [ $TRACEASNPATH = true ] && [[ $numips = 1 ]]; then - TraceASPath "$ip" - fi - tput sgr0 - echo "" - exit 0 - else - # Input is an ASN - WhoisASN "$asn" - if [ -z "$found_asname" ]; then - PrintErrorAndExit "Error: no data found for AS${asn}" - fi - printf "\n${red}[AS%s] ${green}%s\n${white}\n" "$userinput" "$found_asname" - tput sgr0 - exit 0 - fi -else - # Input is an IP address - LookupASNAndRouteFromIP "$input" - if [ -z "$found_asname" ] && [ -z "$found_route" ]; then - PrintErrorAndExit "Error: no data found for $input" - fi - echo -e "\n${green}1 IP address found:\e[39m" - (( longest=${#input}+1 )) - WhoisIP "$input" - # Check if AS path tracing is requested - if [ $TRACEASNPATH = true ]; then - TraceASPath "$input" - fi - tput sgr0 - echo "" - exit 0 -fi