Thursday, January 3, 2008

BASH and CGI : A sample script for collecting IPs of your colleagues


A sample BASH CGI script for collecting IPs of your colleagues in your network.

Prerequisites on your local machine:
1) apache/cgi configuration
2) a Mysql installation

The script "retrip.sh" under /var/www/cgi-bin:

#!/bin/bash
#Getting IP address CGI-BASH-script

#MySQL settings
MHOME=/usr/local/mysql/bin
HOST=127.0.0.1
USER=root
PASS=""
DB=jks

OWNER=`echo "$QUERY_STRING" |awk -F "=" '{print $NF}'`
IP=$REMOTE_ADDR
#QUERY_STRING = Query information that follows the ? in the URL that referenced this script.
#REMOTE_ADDR = IP address of the remote host making the request

#HTML Output
echo "Content-type: text/html"
echo ""
echo "<html><head><title>YOUR IP</title></head><body><h1>I am collecting the IPs</h1><pre>";
echo "IP: <em>$IP</em><br />"
echo "You Are: <em>$OWNER</em><br />"

$MHOME/mysql -u$USER -h$HOST --password=$PASS -e "INSERT INTO jks.ips(rdate,ip,owner) VALUES(NOW(),'$IP','$OWNER')" $DB

echo "</pre></body></html>";


Now create the mysql schema:

mysql> create table jks.ips (
-> rdate DATE,
-> ip VARCHAR(20) NOT NULL PRIMARY KEY,
-> owner VARCHAR(25)
-> );
Query OK, 0 rows affected (0.02 sec)

Suppose the IP of the box where you are running the cgi is "172.22.23.188" (your local ip). Now construct the urls for your colleagues this way:

http://172.22.23.188/cgi-bin/retrip.sh?owner=rajeshb
http://172.22.23.188/cgi-bin/retrip.sh?owner=ssarma
...
...

Send them in mail, request all of them to click the url, just to help you collecting the ips.

Once they click, your database table will populate with datas like this:

mysql> select * from jks.ips;
+------------+---------------+---------+
| rdate | ip | owner |
+------------+---------------+---------+
| 2008-01-02 | 172.22.23.121 | rajeshb |
| 2008-01-03 | 172.22.23.82 | ssarma |
+------------+---------------+---------+
2 rows in set (0.00 sec)

No comments:

© Jadu Saikia www.UNIXCL.com