Skip to content

Instantly share code, notes, and snippets.

@mad
Created January 17, 2012 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mad/1627346 to your computer and use it in GitHub Desktop.
Save mad/1627346 to your computer and use it in GitHub Desktop.
Monitor router open connections in command-line (D-Link DI-604)
#! /bin/bash
LOGIN="admin"
PWD="password"
# now only for d-link DI-604
URL="http://192.168.0.1/cgi-bin/natp"
BASIC_AUTH=$(echo -n ${LOGIN}:${PWD} | base64)
page=0
line_number=1
echo -e "Num\tProto\t Local Address\t\t Foreign Address\t NAT port\t TTL"
while [ true ]; do
# XXX: param and url must be set together
content=$(GET -H "Authorization: Basic ${BASIC_AUTH}" "${URL}?rc=_nstat&pn=$page&rd=nstat")
# XXX: really?
$(echo $content | grep -E -q 'UDP|TCP|ICMP')
if [ $? = 0 ]; then
# XXX: skip IP:53 (DNS req)
# Remove html stuff, and add line number
echo $content \
| sed -e 's/<\/TR>/<\/TR>\n/g' \
| sed -ne 's/<TR>//p' \
| sed -ne 's/.*<TD>\(.*\)<\/TD> <TD>\(.*\)<\/TD> <TD>\(.*\)<\/TD> <TD>\(.*\)<\/TD> <TD>\(.*\)<\/TD>.*/\2\t \1\t \3\t \4\t\t \5/p' \
| nl -n ln -v ${line_number}
page=$(($page + 20))
line_number=$((${line_number} + 19))
else
echo "END"
exit
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment