Back to the How-To List
Create a CGI script using only shell scripting
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.
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:
If you're using Freesco v0.3.0, type:
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.
if [ "$R" = "0" ]; then echo "Current external IP address is:"; fi
cat /etc/live.cfg | grep IPADDR | sed 's/.*=//'
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: /'`
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";
cat /proc/uptime | sed 's/[.].*//';
if [ "$R" = "0" ]; then echo "Router is running on:"; fi
cat /proc/version | sed 's/(.*//'
# get /bin/probes from our "probe detection" article
if [ -f /bin/probes ]; then
R=`echo $QUERY_STRING | sed '/.*raw=/!d; s///'`
Q=`echo $QUERY_STRING | sed '/^req=*/!d; s///; s/\&.*//'`
if [ "$R" != "1" ]; then R="0"; fi
case "$Q" in
echo "Invalid request.";;
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.
Alt+X to exit, and press
Y to save when prompted.
Next, you need to make the script accessible to the webserver. Type:
Now, fire up a web browser, and open the following webpage:
chown nobody.nobody info.cgi
chmod 755 info.cgi
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).