<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Werx Limited &#187; bash</title> <atom:link href="http://werxltd.com/wp/category/software-development/bash/feed/" rel="self" type="application/rss+xml" /><link>http://werxltd.com/wp</link> <description>We make IT work.</description> <lastBuildDate>Mon, 23 Jan 2012 23:03:59 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Simple init.d script template</title><link>http://werxltd.com/wp/2012/01/05/simple-init-d-script-template/</link> <comments>http://werxltd.com/wp/2012/01/05/simple-init-d-script-template/#comments</comments> <pubDate>Thu, 05 Jan 2012 19:25:24 +0000</pubDate> <dc:creator>wes</dc:creator> <category><![CDATA[bash]]></category> <category><![CDATA[hosting]]></category> <category><![CDATA[administration]]></category> <category><![CDATA[daemon]]></category> <category><![CDATA[init.d]]></category> <category><![CDATA[script]]></category> <category><![CDATA[startup]]></category> <category><![CDATA[system]]></category><guid
isPermaLink="false">http://werxltd.com/wp/?p=1077</guid> <description><![CDATA[Recently I found the need to create an init.d script and since I had a hard time finding an example elsewhere1, here&#8217;s the overly simple script I came up with to get the job done: #!/bin/bash # myapp daemon # chkconfig: 345 20 80 # description: myapp daemon # processname: myapp DAEMON_PATH="/home/wes/Development/projects/myapp" DAEMON=myapp DAEMONOPTS="-my opts" [...]]]></description> <content:encoded><![CDATA[<p>Recently I found the need to create an init.d script and since I had a hard time finding an example elsewhere<sup><a
href="http://werxltd.com/wp/2012/01/05/simple-init-d-script-template/#footnote_0_1077" id="identifier_0_1077" class="footnote-link footnote-identifier-link" title="That said, if you know of such an example I&amp;#8217;d love to hear from you.">1</a></sup>, here&#8217;s the overly simple script I came up with to get the job done:</p><pre class="brush:bash">
#!/bin/bash
# myapp daemon
# chkconfig: 345 20 80
# description: myapp daemon
# processname: myapp

DAEMON_PATH="/home/wes/Development/projects/myapp"

DAEMON=myapp
DAEMONOPTS="-my opts"

NAME=myapp
DESC="My daemon description"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
	printf "%-50s" "Starting $NAME..."
	cd $DAEMON_PATH
	PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&#038;1 &#038; echo $!`
	#echo "Saving PID" $PID " to " $PIDFILE
        if [ -z $PID ]; then
            printf "%s\n" "Fail"
        else
            echo $PID > $PIDFILE
            printf "%s\n" "Ok"
        fi
;;
status)
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z `ps axf | grep 5003 | grep -v grep` ]; then
                printf "%s\n" "Process dead but pidfile exists"
            else
                echo "Running"
            fi
        else
            printf "%s\n" "Service not running"
        fi
;;
stop)
        printf "%-50s" "Stopping $NAME"
            PID=`cat $PIDFILE`
            cd $DAEMON_PATH
        if [ -f $PIDFILE ]; then
            kill -HUP $PID
            printf "%s\n" "Ok"
            rm -rf $PIDFILE
        else
            printf "%s\n" "pidfile not found"
        fi
;;

restart)
  	$0 stop
  	$0 start
;;

*)
        echo "Usage: $0 {status|start|stop|restart}"
        exit 1
esac
</pre><p>This script will work in /etc/init.d on Xubuntu 11.10 (so most Debian-based systems) and CentOS 5.5 and you can control it via chkconfig.</p><div
class="betterrelated none"><p>No related content found.</p></div><ol
class="footnotes"><li
id="footnote_0_1077" class="footnote">That said, if you know of such an example I&#8217;d love to hear from you.</li></ol><p><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;counturl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;count=none&amp;text=Simple%20init.d%20script%20template" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;counturl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;count=none&amp;text=Simple%20init.d%20script%20template" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><!--<![endif]--><a
class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;linkname=Simple%20init.d%20script%20template" title="LinkedIn" rel="nofollow" target="_blank"><img
src="http://werxltd.com/wp/wp-content/plugins/add-to-any/icons/linkedin.png?9d7bd4" width="16" height="16" alt="LinkedIn"/></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2012%2F01%2F05%2Fsimple-init-d-script-template%2F&amp;title=Simple%20init.d%20script%20template" id="wpa2a_2">Share/Save</a></p>]]></content:encoded> <wfw:commentRss>http://werxltd.com/wp/2012/01/05/simple-init-d-script-template/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>A few helpful bash command-line one-liners</title><link>http://werxltd.com/wp/2010/03/30/a-few-helpful-bash-command-line-one-liners/</link> <comments>http://werxltd.com/wp/2010/03/30/a-few-helpful-bash-command-line-one-liners/#comments</comments> <pubDate>Tue, 30 Mar 2010 12:00:47 +0000</pubDate> <dc:creator>wes</dc:creator> <category><![CDATA[bash]]></category> <category><![CDATA[it industry]]></category> <category><![CDATA[software development]]></category> <category><![CDATA[console]]></category> <category><![CDATA[linux]]></category> <category><![CDATA[shell]]></category> <category><![CDATA[svn]]></category><guid
isPermaLink="false">http://werxltd.com/wp/?p=545</guid> <description><![CDATA[[HT Peter, commandlinefu] Query SVN log history and filter by username svn log &#124; sed -n '/username/,/-----$/ p' Run the last command as root sudo !! Save a file you edited in vim without the needed permissions :w !sudo tee % Why is this command so awesome? Peter described it quite well: This happens to [...]]]></description> <content:encoded><![CDATA[<p>[HT <a
href="http://www.catonmat.net/blog/top-ten-one-liners-from-commandlinefu-explained/">Peter</a>, <a
href="http://www.commandlinefu.com/commands/browse/sort-by-votes">commandlinefu</a>]</p><p>Query SVN log history and filter by username</p><pre class="brush:bash">svn log | sed -n '/username/,/-----$/ p'</pre><p>Run the last command as root</p><pre class="brush:bash">sudo !!</pre><p>Save a file you edited in vim without the needed permissions</p><pre class="brush:bash">:w !sudo tee %</pre><p>Why is this command so awesome? Peter described it quite well:</p><blockquote><p>This happens to me way too often. I open a system config file in vim and edit it just to find out that I don’t have permissions to save it. This one-liner saves the day. Instead of writing the while to a temporary file :w /tmp/foobar and then moving the temporary file to the right destination mv /tmp/foobar /etc/service.conf, you now just type the one-liner above in vim and it will save the file.</p></blockquote><p>Change to the previous working directory</p><pre class="brush:bash">cd -</pre><p>Run the previous shell command but replace string “foo” with “bar”</p><pre class="brush:bash">^foo^bar^</pre><p>Find the last command that begins with “whatever,” but avoid running it</p><pre class="brush:bash">!whatever:p</pre><p>Copy your public-key to remote-machine for public-key authentication</p><pre class="brush:bash">ssh-copy-id remote-machine</pre><p>Capture video of a linux desktop</p><pre class="brush:bash">ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg</pre><div
class="betterrelated"><p><strong>Related content:</strong></p><ol><li> <a
href="http://werxltd.com/wp/2009/12/01/hacking-your-router-for-effective-internet-monitoring/" title="Permanent link to Hacking your router for effective internet monitoring">Hacking your router for effective internet monitoring</a></li></ol><a
class="thanks" style="font-size: smaller; text-decoration: none;" title="Related content found by the Better Related Posts plugin" href="http://www.nkuttler.de/wordpress-plugin/wordpress-related-posts-plugin/">Better Related Posts Plugin</a></div><p><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;counturl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;count=none&amp;text=A%20few%20helpful%20bash%20command-line%20one-liners" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;counturl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;count=none&amp;text=A%20few%20helpful%20bash%20command-line%20one-liners" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><!--<![endif]--><a
class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;linkname=A%20few%20helpful%20bash%20command-line%20one-liners" title="LinkedIn" rel="nofollow" target="_blank"><img
src="http://werxltd.com/wp/wp-content/plugins/add-to-any/icons/linkedin.png?9d7bd4" width="16" height="16" alt="LinkedIn"/></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2010%2F03%2F30%2Fa-few-helpful-bash-command-line-one-liners%2F&amp;title=A%20few%20helpful%20bash%20command-line%20one-liners" id="wpa2a_4">Share/Save</a></p>]]></content:encoded> <wfw:commentRss>http://werxltd.com/wp/2010/03/30/a-few-helpful-bash-command-line-one-liners/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Hacking your router for effective internet monitoring</title><link>http://werxltd.com/wp/2009/12/01/hacking-your-router-for-effective-internet-monitoring/</link> <comments>http://werxltd.com/wp/2009/12/01/hacking-your-router-for-effective-internet-monitoring/#comments</comments> <pubDate>Tue, 01 Dec 2009 12:00:42 +0000</pubDate> <dc:creator>wes</dc:creator> <category><![CDATA[bash]]></category> <category><![CDATA[it industry]]></category> <category><![CDATA[java]]></category> <category><![CDATA[software development]]></category> <category><![CDATA[dd-wrt]]></category> <category><![CDATA[firmware]]></category> <category><![CDATA[hardware]]></category> <category><![CDATA[monitoring]]></category> <category><![CDATA[openwrt]]></category> <category><![CDATA[router]]></category> <category><![CDATA[security]]></category> <category><![CDATA[wrt54gl]]></category><guid
isPermaLink="false">http://werxltd.com/wp/?p=245</guid> <description><![CDATA[The Why: Preamble Working in the information technology sector, one of the most common questions I get asked by parents is about monitoring internet access of their children.1 Most parents want to know what their children are doing online but also recognize that most off-the-shelf products are just as easy to disable or circumvent (or are [...]]]></description> <content:encoded><![CDATA[<h2>The Why: Preamble</h2><p>Working in the information technology sector, one of the most common questions I get asked by parents is about monitoring internet access of their children.<sup><a
href="http://werxltd.com/wp/2009/12/01/hacking-your-router-for-effective-internet-monitoring/#footnote_0_245" id="identifier_0_245" class="footnote-link footnote-identifier-link" title="Most actually ask about &amp;#8220;controlling what their kids see online&amp;#8221; but I generally argue for a observe-only approach as it helps open lines of communication with your child whereas silently blocking &amp;#8220;bad&amp;#8221; sites will only start a silent war which will only frustrate you once they do find a suitable workaround, such as a proxy.">1</a></sup></p><p>Most parents want to know what their children are doing online but also recognize that most off-the-shelf products are just as easy to disable or circumvent (or are far more restrictive/bloated than they want) as they are to install or operate. And sadly, <a
href="http://www.mcafee.com/us/enterprise/products/email_and_web_security/web/web_gateway.html">enterprise solutions</a> that capture and control network traffic at the most basic level (making circumvention next to impossible) is still very expensive and therefore out of reach for the average family.</p><p>What I needed was a cheap and hackable router that I could modify to send captured URLs to a central source for storage and processing.</p><h2>The What: WRT54G</h2><p><a
href="http://books.google.com/books?id=GBtJdvMeAJQC&amp;dq=Linksys+WRT54G+Ultimate+Hacking&amp;printsec=frontcover&amp;source=bn&amp;hl=en&amp;ei=06oTS_GDHpC1tgfyqeCsCQ&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=4&amp;ved=0CB4Q6AEwAw#v=onepage&amp;q=&amp;f=false"><img
class="alignright size-full wp-image-264" style="margin: 5px;" title="Linksys-WRT54G-Ultimate-Hacking" src="http://werxltd.com/wp/wp-content/uploads/2009/11/Linksys-WRT54G-Ultimate-Hacking.jpg?9d7bd4" alt="Linksys-WRT54G-Ultimate-Hacking" width="210" height="258" /></a>After studying my options I remembered reading a lot about the Linksys WRT54G-series routers and how they were originally based on a heavily modified version of Linux and how <a
href="http://www.linuxinsider.com/story/43996.html?wlc=1259579841">Linksys made headlines</a> when it lost a court case regarding the GPLed code it used in their router&#8217;s firmware.</p><p>So I did a little digging.</p><p>What I found was a whole router-hacking subculture built around the WRT54G. While it seems that much of the initial fervor has subsided, many of the packages show a last update time of 2007 or so, the documentation is still valid for the most part. The most popular projects which provide custom firmware are the <a
href="http://openwrt.org/">OpenWRT</a> and DD-WRT. While OpenWRT is the original, I found DD-WRT to be a lot more polished and (as we&#8217;ll see later) configurable without much headache.</p><p>It&#8217;s important to note here that the WRT54G has many variants and its easy to fall into the trap of thinking that any old WRT54G will do but a little diligence and <a
href="http://en.wikipedia.org/wiki/Linksys_WRT54G_series#Hardware_and_revisions">study of the differences between the hardware revisions</a> will certainly save you time and money.</p><p>After buying a few different routers and bricking one (a Buffalo AirStation WHR-HP-54G<sup><a
href="http://werxltd.com/wp/2009/12/01/hacking-your-router-for-effective-internet-monitoring/#footnote_1_245" id="identifier_1_245" class="footnote-link footnote-identifier-link" title="I might have had better luck had I seen this helpful guide. Oh well, this gives me a future project in figuring out how to de-brick my WHR-HP-54G">2</a></sup> ) and a false start with a newer WRT54G v7 (anyone need a highly configurable, albeit not-very-hackable router?) I discovered that the best router for hacking is the <a
href="http://www.linksysbycisco.com/US/en/products/WRT54GL">WRT54GL</a> (which was designed by Linksys to allow for user modifications).</p><h2>The How: URLSnarf and custom shell scripts</h2><p>Space on a router is very limited. On the WRT54GL model I eventually ended up using I had 4Megs of space to work with.</p><p>The first order of business was to find a package that could monitor all of the network connections (wired and wireless) on the router and capture requested URLs. For this task I discovered  that <a
href="http://www.irongeek.com/i.php?page=backtrack-3-man/urlsnarf">URLSnarf</a>, part of the <a
href="http://en.wikipedia.org/wiki/DSniff">dsniff</a> <a
href="http://downloads.openwrt.org/whiterussian/newest/packages/">OpenWrt package</a>, worked quite well.</p><p>To install packages I used DD-WRT&#8217;s <a
href="http://www.bitsum.com/firmware_mod_kit.htm">firmware modification kit</a> which allowed me to simply add the scripts and packages I wanted without having to recompile everything.</p><p>Next I needed to transform the captured URL into a URLencoded string in order to send it to my monitoring service via a simple wget request. Initially I tried using several variations of user-generated Python and PHP packages but they both took up far more space than I could afford so, instead, I searched for a pure command-line based solution.</p><p>After some more digging I <a
href="http://www.unix.com/shell-programming-scripting/59936-url-encoding.html">found a handy sed substitution script</a> that worked like a charm. The script worked in two parts, the first one being the substitution script (/usr/bin/urlencode.sed):</p><pre class="brush:bash">s/%/%25/g
s/ /%20/g
s/ /%09/g
s/!/%21/g
s/"/%22/g
s/#/%23/g
s/\$/%24/g
s/\&amp;/%26/g
s/'\''/%27/g
s/(/%28/g
s/)/%29/g
s/\*/%2a/g
s/+/%2b/g
s/,/%2c/g
s/-/%2d/g
s/\./%2e/g
s/\//%2f/g
s/:/%3a/g
s/;/%3b/g
s//%3e/g
s/?/%3f/g
s/@/%40/g
s/\[/%5b/g
s/\\/%5c/g
s/\]/%5d/g
s/\^/%5e/g
s/_/%5f/g
s/`/%60/g
s/{/%7b/g
s/|/%7c/g
s/}/%7d/g
s/~/%7e/g
s/	/%09/g</pre><p>and the command line to use it:</p><pre class="brush:bash">sed -f urlencode.sed</pre><p>To tie it all together, we can pass captured URLs to it via pipes from the command-line with:</p><pre class="brush:bash">urlsnarf | sed -f urlencode.sed</pre><p>At this point, the only missing link of the capture chain is a script to continually read from the command line and send the urlencoded capture data to our storage application (described in the next part). For this task I used the following script (/usr/bin/urlmon.sh):</p><pre class="brush:bash">HOSTNAME=`hostname`
while read url; do
    DATE=`date +%s`
    echo $(wget -q -O- "http://myapp.appspot.com/log?l=$url&amp;h=$HOSTNAME&amp;t=$DATE")
done

exit 0</pre><p>Finally, we need to have the router start listening for URLs as soon as it is booted. In a Linux environment this is generally done by init scripts. Since our router has limited capabilities, we don&#8217;t need to write a full init script. Here is the slimmed down <a
href="http://www.debian-administration.org/article/Making_scripts_run_at_boot_time_with_Debian">init script</a> I used (/etc/init.d/S50urlmon):</p><pre class="brush:bash">#!/bin/sh

/usr/sbin/urlsnarf -v "/(192.168.1.1|https\://myapp\.appspot\.com)/" | sed -f /usr/bin/urlencode.sed | /usr/bin/urlmon.sh</pre><h2>The Where: Google App Engine</h2><p>I&#8217;ve been itching to try out <a
href="http://code.google.com/appengine/">Google&#8217;s App Engine</a> for a while now and this project seemed to be a great fit since I didn&#8217;t know how much data to expect and I needed my receiving/processing/display application to be highly available and scalable. Especially if this works well enough that others might want to use it.</p><p>Since my initial phase is to merely capture the URLs requested from devices behind the router, and since the capture process should be as efficient and lean as possible (I don&#8217;t want the router to take very long logging a URL when it&#8217;s primary job is to retrieve that URL for the initial requester) I decided to make a simple Java servlet which simply takes the URLencoded log line generated by URLSnarf.</p><p>Google App Engine uses <a
href="http://en.wikipedia.org/wiki/Java_Data_Objects">Java Data Objects</a> enhanced by <a
href="http://www.datanucleus.org/">DataNucleus</a> to store data in Google&#8217;s massive cluster. Here is the annotated JDO (LogLine.java) I used to store the captured URL:</p><pre class="brush:java">import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class LogLine {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String host;

	@Persistent
	private Long time;

	@Persistent
	private String line;

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setLine(String line) {
		this.line = line;
	}

	public String getLine() {
		return line;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public Long getTime() {
		return time;
	}

	public void setTime(Long time) {
		this.time = time;
	}
}</pre><p>And here is the <a
href="http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/">servlet</a> that processes the GET request (containing the captured URL in <a
href="http://httpd.apache.org/docs/1.3/logs.html#common">Apache Common Log format</a>)</p><pre class="brush:java">import java.io.IOException;
import java.net.URLDecoder;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.werxltd.webmon.data.LogLine;

public class Log extends HttpServlet {
	private final static long serialVersionUID = 3;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
    	throws IOException {
			try {
				resp.setContentType("text/plain");

				LogLine logline = new LogLine();
				String logStr = URLDecoder.decode(req.getParameter("l"));
				logline.setLine(logStr);
				logline.setHost(req.getParameter("h"));
				logline.setTime(Long.parseLong(req.getParameter("t")));

				PersistenceManager pm = PMF.get().getPersistenceManager();
				pm.makePersistent(logline);
				pm.close();	

				resp.getWriter().println("OK");
			} catch (Exception e) {
				e.printStackTrace();
				resp.getWriter().println("FAIL");
			} finally {

			}

	}
}</pre><h2>The future</h2><p>This project is still in it&#8217;s early stages. There is no real way to view the captured data just yet, though I plan on incorporating <a
href="http://polliwog.sourceforge.net/">Polliwog</a>, and the router software hasn&#8217;t been tested as much as I would like. I&#8217;m also leery of any security holes I may have introduced.</p><p>So if you have any suggestions or would like to know more, feel free to leave a comment below!</p><div
class="betterrelated"><p><strong>Related content:</strong></p><ol><li> <a
href="http://werxltd.com/wp/2010/03/30/a-few-helpful-bash-command-line-one-liners/" title="Permanent link to A few helpful bash command-line one-liners">A few helpful bash command-line one-liners</a></li><li> <a
href="http://werxltd.com/wp/2010/10/12/fred-brooks-on-the-promise-of-object-oriented-programming/" title="Permanent link to Fred Brooks on the promise of object oriented programming">Fred Brooks on the promise of object oriented programming</a></li><li> <a
href="http://werxltd.com/wp/2009/08/26/getting-starte-with-hadoop-and-mapreduce/" title="Permanent link to Getting started with Hadoop and MapReduce">Getting started with Hadoop and MapReduce</a></li></ol><a
class="thanks" style="font-size: smaller; text-decoration: none;" title="Related content found by the Better Related Posts plugin" href="http://www.nkuttler.de/wordpress-plugin/wordpress-related-posts-plugin/">Better Related Posts Plugin</a></div><ol
class="footnotes"><li
id="footnote_0_245" class="footnote">Most actually ask about &#8220;controlling what their kids see online&#8221; but I generally argue for a observe-only approach as it helps open lines of communication with your child whereas silently blocking &#8220;bad&#8221; sites will only start a silent war which will only frustrate you once they do find a suitable workaround, <a
href="http://www.google.com/search?sourceid=chrome&amp;ie=UTF-8&amp;q=web+proxy">such as a proxy</a>.</li><li
id="footnote_1_245" class="footnote">I might have had better luck had I seen <a
href="http://dotmrt.wordpress.com/2008/07/03/wifi-router-firmware-tomato-on-buffalo-whr-hp-54g/">this helpful guide</a>. Oh well, this gives me a future project in figuring out how to de-brick my WHR-HP-54G</li></ol><p><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service facebook_like" src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;layout=button_count&amp;show_faces=false&amp;width=75&amp;action=like&amp;colorscheme=light&amp;height=20&amp;ref=addtoany" scrolling="no" style="border:none;overflow:hidden;width:90px;height:21px"></iframe><!--<![endif]--><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;counturl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;count=none&amp;text=Hacking%20your%20router%20for%20effective%20internet%20monitoring" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service twitter_tweet" src="http://platform.twitter.com/widgets/tweet_button.html?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;counturl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;count=none&amp;text=Hacking%20your%20router%20for%20effective%20internet%20monitoring" scrolling="no" style="border:none;overflow:hidden;width:55px;height:20px"></iframe><!--<![endif]--><!--[if IE]><iframe
frameborder="0" allowTransparency="true" class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><![endif]--><!--[if !IE]><!--><iframe
class="addtoany_special_service google_plusone" src="https://plusone.google.com/u/0/_/%2B1/fastbutton?url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;size=medium&amp;count=false" scrolling="no" style="border:none;overflow:hidden;width:32px;height:20px"></iframe><!--<![endif]--><a
class="a2a_button_linkedin" href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;linkname=Hacking%20your%20router%20for%20effective%20internet%20monitoring" title="LinkedIn" rel="nofollow" target="_blank"><img
src="http://werxltd.com/wp/wp-content/plugins/add-to-any/icons/linkedin.png?9d7bd4" width="16" height="16" alt="LinkedIn"/></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwerxltd.com%2Fwp%2F2009%2F12%2F01%2Fhacking-your-router-for-effective-internet-monitoring%2F&amp;title=Hacking%20your%20router%20for%20effective%20internet%20monitoring" id="wpa2a_6">Share/Save</a></p>]]></content:encoded> <wfw:commentRss>http://werxltd.com/wp/2009/12/01/hacking-your-router-for-effective-internet-monitoring/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using apc
Page Caching using apc
Database Caching 1/27 queries in 2.091 seconds using apc
Object Caching 574/640 objects using apc

Served from: werxltd.com @ 2012-02-08 12:40:13 -->
