Set proxy if @work

I got tired of manually setting http_proxy & ftp_proxy on my job computer that I sometimes use at work (proxy needed) or at home (no proxy needed) so I came up with this dirty, but workable pice of bash script.

The ip x.x.x.x in the script below is the office IP (assigned by DHCP, but always resolved to static IP address based on my MAC)

#Set proxy if in office
ip=$(/sbin/ifconfig eth0 | grep -e".*inet addr.*" | sed -e "s/.*inet addr://g" | sed -e"s/ .*//g")
if [ $ip = "x.x.x.x" ]
  export http_proxy
  export ftp_proxy

The above is part of a bigger script ( that set other stuff as well, and this script is called from my bash profile (~/.bashrc) so every time I create a bash shell I get this set.

Posted in bash, shell | Leave a comment

ISP connection monitoring

I have had some issues with my internet connection lately so I made some scripts to monitor it from my NAS.

First, let’s look at my network topology to understand what we want to test. I have my NAS attached via LAN to my modem with built in router, in other words we will only check LAN connectivity (not WLAN) to Internet.

The scripts I wrote will first see if router is reachable, and if it isn’t ping test is aborted (no point testing ISP connection if we can’t get to the modem). If router is reachable we will in parallel try to ping a set of hosts. Let’s look at the script first:

SUCCESS=$(ping -c $count $host | tail -2 | head -1 | grep -c -e".* 0% packet loss.*")
  if [ "${SUCCESS}" -lt 1 ];then
    echo "${host} is dead at $(date)"

The script above will ping a host a number of times, and return an error string if there is packet loss. Example: 1 will send one ping message to Now to the main script,

hosts="bbb-router isp-gateway"
routerdead=$("${pinghostcommand}" bbb-router 1)
if [ "X" = "X${routerdead}" ]; then
  for host in $hosts
      $("${pinghostcommand}" $host $COUNT > "/tmp/$host") &

sleep 11
for host in $hosts
    cat /tmp/$host | grep dead >> /samba-share/stats/pingStats.txt
    rm /tmp/$host

With the above script set in crontab

# m h dom mon dow command
0,10,20,30,40,50 * * * * /samba-share/scripts/

I can check the ping stats log file to see if my sampling indicates packet loss to what hosts and at what time.

cat  /samba-share/stats/pingStats.txt | tail -n 10 is dead at Wed Sep 19 21:30:07 CEST 2012 is dead at Wed Sep 19 21:30:07 CEST 2012 is dead at Wed Sep 26 04:50:11 CEST 2012 is dead at Wed Sep 26 05:10:12 CEST 2012 is dead at Sat Sep 29 08:00:12 CEST 2012 is dead at Sat Sep 29 08:00:12 CEST 2012 is dead at Sat Sep 29 19:20:12 CEST 2012 is dead at Wed Oct 3 15:50:12 CEST 2012 is dead at Mon Oct 8 19:00:11 CEST 2012 is dead at Tue Oct 9 21:20:11 CEST 2012

Posted in sh, shell | Leave a comment