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/console/SSH), and 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

Installation

If some of the lines above have been word wrapped; 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

Usage

Now, fire up a web browser, and open the following webpage:

  http://your_router:82/cgi/info.cgi?req=uptime

Replace your_router with the IP or hostname of your router. You can replace uptime with any of the following commands:

command result
ipaddr Returns your current external (Internet) IP address
version Returns the current Linux kernel version (should always be 2.0.38, 2.0.39 or 2.0.40)
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).

made by Steve Blinch


Notes

Notes by — dingetje 2004/12/06 13:21

  • The default port for the internal web server is 82 not 85
  • I don't understand why for 0.2.7 the script is placed in the “public” CGI directory and for 0.3.x it's placed in the “web control panel” CGI directory?! If the directories /www/cgi (public) or /wwa/cgi (web control panel) were used, this howto would be generic for both FREESCO versions. The choice to place the script in either the public or web control panel CGI directory is a matter of security.

Mathieu CATTIN 2004/12/11 01:49 Fixed now.

 
freesco/howtos/create_a_cgi_script_using_only_shell_scripting.txt (95129 views) · Last modified: 2005/09/14 00:49 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki