#!/bin/bash #Alternative api for resolving organisations and country codes #curl https://api.bgpview.io/asn/AS1299 | jq -a -M -r '.data | .description_short, .country_code' if [ -z "${1}" ] then exit fi myip="$(curl -sL https://emil.fi/api/my/ip)" myhost="$(host ${myip} | grep -oP '(?<=domain name pointer ).+(?=.$)' | head -n 1)" targetip="$(host ${1} | grep -oP '(?<= address ).+' | head -n 1)" if [ -z "${targetip}" ] then targetip="${1}" targethost="$(echo ${1} | grep -oP '(?<=domain name pointer ).+(?=.$)' | head -n 1)" else targethost="${1}" targetip="$(host ${1} | grep -oP '(?<= address ).+' | head -n 1)" fi if [ -z "${myhost}" ] then myhost="NXDOMAIN" fi if [ -z "${targethost}" ] then targethost="NXDOMAIN" fi if [ -z "${targetip}" ] then exit fi echo "$(date '+%F %T %z'): Path from ${myip} (${myhost}) to ${targetip} (${targethost})" IFS= traceroute -U -A "${1}" | ( asnumber="" country="" organisation="" timing="" asnumberp="" countryp="" organisationp="" timingp="" while IFS= read -r line do asnumber="$(echo ${line} | grep -oP '(?<=\[)AS\d+(?=\])' | head -n 1)" if [ "${asnumberp}" != "${asnumber}" ] && [ -n "${asnumberp}" ] && [ -n "${organisationp}" ] && [ -n "${countryp}" ] && [ -n "${timingp}" ] then echo "${asnumberp}, ${organisationp}, ${countryp} (${timingp} ms)" asnumberp="" countryp="" organisationp="" timingp="" fi if [ -z "${asnumber}" ] then continue fi timing="$(echo ${line} | grep -oP '\d+\.?\d*(?= ms)' | sort -n | head -n 1)" if [ "${asnumberp}" == "${asnumber}" ] then timing="$(echo ${timing} ${timingp} | tr ' ' '\n' | sort -n | head -n 1)" else query="$(whois -h whois.cymru.com -v ${asnumber} | tail -n 1 | rev | cut -d '|' -f 1 | rev | grep -oP '\S.*\S(?=\s*)')" organisation="$(echo ${query} | cut -d ',' -f 1 | grep -oP '\S.*\S(?=\s*)')" country="$(echo ${query} | rev | cut -d ',' -f 1 | rev | grep -oP '\S.*\S(?=\s*)')" fi asnumberp="${asnumber}" countryp="${country}" organisationp="${organisation}" timingp="${timing}" done if [ -n "${asnumber}" ] then echo "${asnumber}, ${organisation}, ${country} (${timing} ms)" fi )