I don't write a lot of shell scripts any more. For some reason, today I was inspired.

The inspiration is an evil little content traffic cop called WebSense. This big brother beast is willing to let me through, but only as long as I declare and authenticate myself on a frustratingly constant basis. In the past I've placed a graphical browser on a web page the auto refreshes, but that's not a realistic option for the console. The solution is to do a wget and pass the credentials, but this needs to be done repetitively.

The original script was kind of small, basically the 8 lines found somewhere in the middle of the final version. So, after descending into a full on coding geekout, we have a 90+ line script that can even clean up it's mess and limit itself to a single instance process.

I don't know if anyone else would find this useful or even interesting. By putting it here I'll be able to find it again if I need it, and someone else might get something out of it. I call it stayalive.



#!/bin/sh

delay_secs=60
URL="http://www.google.com"
username="http-user"

lockfile="/var/lock/stayalive"
outfile="/var/tmp/stayalive.doc.html"
outnohup="/var/tmp/stayalive.log"
runcmd="/var/tmp/stayalive.run"

show_status() {
 if [ -e $lockfile ]; then
  echo "Running:"
  pid=`cat $lockfile`
  ps -ef | grep " \+$pid \+"
 else
  echo "Not Running."
 fi
}


cleanup() {
 if [ -e $lockfile ]; then
  kill `cat $lockfile`
 fi
 rm -f $lockfile $outfile $outnohup $runcmd
}


load_password() {
 echo "Enter password for '$username':"
 read -s password

 if [ -z $password ]; then
  echo "user abort"
  exit 1
 fi
}


check_lock() {
 if [ -e $lockfile ]; then
  echo "process locked, check $lockfile"
  exit 1;
 fi
}


execute() {
 check_lock
 cleanup
 load_password

 cat > $runcmd << EOT
 echo \$\$ > $lockfile
 isGood=0
 while [ \$isGood -eq 0 ]; do
  wget --http-user="$username" --http-passwd="$password" --output-document=$outfile $URL
  isGood=\$?
  if [ \$isGood -eq 0 ]; then
   sleep $delay_secs
  fi
 done
 rm $lockfile
EOT

 chmod 700 $runcmd
 nohup $runcmd > $outnohup &

 exit 0
}


usage() {
 echo "Usage: $0 [-c | -x | -s]";
 echo " -c cleanup"
 echo " -x execute"
 echo " -s status"
 exit 1;
}


if [ $# -ne 1 ]; then
 usage
fi

case $1 in
 -c) cleanup; exit 0;;
 -x) execute;;
 -s) show_status;;
  *) usage;;
esac

Tags:
.

Profile

baavgai: (Default)
baavgai

Links

Most Popular Tags

Powered by Dreamwidth Studios

Style Credit

Expand Cut Tags

No cut tags