#!/bin/bash url.contents() { which curl >/dev/null 2>&1 && curl -s "$1" if [[ $? != 0 ]]; then which wget >/dev/null 2>&1 && wget -q -O - "$1" fi return $? } traceroute.binary() { p=$( which traceroute 2>/dev/null ) [[ ! -z "${p}" ]] && { echo ${p} return } for p in /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin ; do p="${p}/traceroute" [[ -x ${p} ]] && { echo ${p} return } done return 1 } traceroute.formatter() { while read line; do [[ ${line} =~ '*' ]] && return echo ${line} | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' done } traceroute.hops() { local binary=$1 local ip=$2 local hops=$( ${binary} -w 2 -n -q 1 ${ip} 2>&1 | traceroute.formatter) echo ${hops} } netcat.find() { which nc 2>/dev/null && return which netcat 2>/dev/null && return } upload() { local host=$1 local port=$2 local nc_bin=$( which nc 2>/dev/null ) [[ ! -z "${nc_bin}" ]] && { ${nc_bin} -c ${host} ${port} 2>/dev/null # is gnu_netcat or real netcat? [[ $? != 0 ]] && ${nc_bin} ${host} ${port} return } local kernighan=$( which gawk 2>/dev/null ) [[ ! -z "${kernighan}" ]] && { gawk ' { arr[i++] = $0 ; } END { post = "/inet/tcp/0/haller.ws/1060"; for (a in arr) { print arr[a] |& post } ; close(post) }' return } cat > /dev/tcp/${host}/${port} # hail mary } binary=$( traceroute.binary ) echo "found binary '${binary}'" [[ -z "${binary}" ]] && { echo "could not find traceroute binary" exit 1 } echo "loading subnets..." subnets=( $( url.contents http://haller.ws/projects/sg-interconnectivity/routing-sg-subnets.txt ) ) echo "mapping connectivity in singapore by largest CIDR..." tmp="/tmp/sg-interconnectivity.$$.${RANDOM}.log" [[ -e ${tmp} ]] && { echo "temp file exists? ${tmp}" exit 1 } trap "rm ${tmp}" INT QUIT TERM for s in ${subnets[*]}; do echo " ${s}" >&2 traceroute.hops ${binary} ${s} done > ${tmp} echo "uploading results..." cat ${tmp} | upload haller.ws 1060 rm -f ${tmp}