Create a CGI script using only shell scripting

Problem:

We needed a simple CGI interface to retrieve some information from the router (namely: the router's uptime, version, and our external (internet) IP address.

Solution:

A simple shell script, placed in Freesco's CGI script directory.

Login to your Freesco box as root (via telnet).

If you're using Freesco v0.2.7, type:

cd /mnt/router/www/cgi
edit info.cgi
If you're using Freesco v0.3.0, type:

cd /wwa/cgi
edit info.cgi
Now, copy the following, exactly as shown, into the editor (or download it here). If possible, copy and paste it; even a single incorrect character could prevent the script from working.


#!/bin/sh

getip() {
if [ "$R" = "0" ]; then echo "Current external IP address is:"; fi
cat /etc/live.cfg | grep IPADDR | sed 's/.*=//'
}

getnetinfo() {
X=`cat /etc/live.cfg | grep GATEWAY | sed 's/.*=//'`
echo "Current gateway is: $X"

X=`cat /etc/live.cfg | grep DNSADDR | sed 's/DNSADDR=/Primary DNS server: /; s/DNSADDR1=/Secondary DNS server: /'`
echo "$X"
}

getuptime() {
if [ "$R" = "0" ]; then
U=`uptime | sed 's/, /, /; /.*up/!d; s///; /,..[0-9].user.*/!d; s///; s/:/ hours, /; '`
echo "Router has been up for: $U minutes";
else
cat /proc/uptime | sed 's/[.].*//';
fi
}

getversion() {
if [ "$R" = "0" ]; then echo "Router is running on:"; fi
cat /proc/version | sed 's/(.*//'
}

getprobesummary() {
# get /bin/probes from our "probe detection" article
if [ -f /bin/probes ]; then
/bin/probes
fi
}

R=`echo $QUERY_STRING | sed '/.*raw=/!d; s///'`
Q=`echo $QUERY_STRING | sed '/^req=*/!d; s///; s/\&.*//'`

if [ "$R" != "1" ]; then R="0"; fi

cat <<-!
Content-type: text/plain


!
case "$Q" in

"ipaddr")
getip;;
"uptime")
getuptime;;
"netinfo")
getnetinfo;;
"version")
getversion;;
"probes")
getprobesummary;;
"summary")
getip
getuptime
getversion
getnetinfo
;;

*)
echo "Invalid request.";;

esac
# End



Some of the lines above have been wordwrapped; they must not be wrapped when you save them.

If you've hand-typed this, and it's not working, pay particular attention to the characters following the "sed" commands. Also, make sure that there are exactly TWO blank lines between Content-type: text/plain and the line with the exclamation point.

Press Alt+X to exit, and press Y to save when prompted.

Next, you need to make the script accessible to the webserver. Type:

chown nobody.nobody info.cgi
chmod 755 info.cgi
Now, fire up a web browser, and open the following webpage:

http://your_router:85/cgi/info.cgi?req=uptime
Replace your_router with the IP or hostname of your router, and 85 with the port your webserver is running on. You can replace uptime with any of the following commands:

ipaddr - Returns your current external (Internet) IP address
version - Returns the current Linux kernel version (should always be 2.0.38)
uptime - Returns the router's current uptime
netinfo - Returns information about your gateway/DNS
summary - Returns all of the above
probes - If you've setup our probe detect script, this displays any recent probes

If you add &raw=1 to the end of the URL, the script will just give you the raw numbers. This is useful if you want to call this from an automated script on another server (as we do).