Archive for March, 2010

shell scripting syntax

March 30, 2010

– Printing/Redirecting PID of current program

echo $$ >/tmp/

– Comparing floating value –


res=`expr $a > $b` [ if true then res =1 else res =0 ]

– Running a script with a particular user

if [ “$LOGNAME” != “tarique” ]; then
echo “Only user bea can run this script”
exit 1

– Logical AND with if condition

if [[ ! -f /tmp/ && $res -eq 1 ]] ; then

– Nested for loop

for (( j = 0 ; j <=1 ; j++ ))
for (( i = 0 ; i <=9 ; i++ ))

– Date formatting

D=`date '+%y-%d-%m %H:%M:%S'`

– Adding value of two variables

result=$(($Value1 + $Value2))

– Remote command execution –

ssh tarique@remotesrv 'touch /tmp/test;rm -f /tmp/test'

linux box as a router

March 17, 2010

Two scenario –

1. Private – Public
2. Private – Private ( inside your LAN)

1. Private – Public

a) Assign Public IP address to the Fast Ethernet Card with the followings;

IP Address ( change with your public IP address
Net Mask (Provided by the Internet service provider)
Default Gateway
Preferred DNS

b) Create a virtual IP address on this Fast Ethernet Card

Copy and paste the configuration file of the eth0 with a new name eth0:0
Assign a private IP Address like you have assigned the other computers in your local area network

IP Address (
Net mask (
Default Gateway (leave this blank)

c) Creating forwarding rules with iptables:

Delete and flush. Default table is “filter”. Others like “nat” must be explicitly stated.

iptables –F – Flush all the rules in filter and nat tables
iptables –t nat –F
iptables –delete-chain

Delete all chains that are not in default filter and nat table
iptables –table nat –delete-chain

# Set up IP FORWARDing and Masquerading
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -j ACCEPT

d) Enables packet forwarding by kernel
echo 1 > /proc/sys/net/ipv4/ip_forward

e) Create a route for internal packets:

route add -net gw dev eth0

Change with your Gateway IP Address

2. Private – Private


Your current net – and gateway/firewall –
Now wanna create a separate Lan and forward packet through to outside world using a box inside your network.

Choose a box you wanna turn it as router and follow the steps

a) IP setting will be as follows –
eth0 – (gw)
eth0:0 –
gw ( leave blank)

b) Then on gw machine ( make sure the followings –


iptables -t nat -A POSTROUTING -o eth0 -s -d ! -j SNAT –to-source

[ This will forward and reverse packets from gw network to outside world ]

And add a route for your lan –

route add -net gw

Also make sure your DNS allow query from outside your lan –

allow-query {;

c) Enables packet forwarding by kernel
echo 1 > /proc/sys/net/ipv4/ip_forward

Now you go!! you are done! your 2.0 network will be able to talk to outside world through ->

Note: On your client machine ( say use as your default gateway and as nameserver.

If you want to restrict that none from 2.0/24 network can reach anyone of 1.0/24 network – except 1.1 then
add the following rules with your firewall –

iptables -I FORWARD -s -d -p all -j REJECT -o eth0
[ This will block all traffic of 1.0/24 lan from 2.0/24 lan ]

iptables -I FORWARD -s -d -p all -j ACCEPT -o eth0
[ only allow traffic for gw ]

Apache + tomcat (mod_jk)

March 10, 2010

1. Install Apache, Tomcat and mod_jk

Install Apache (with devel package), Tomcat (with webapps package) and apache2-mod_jk. start both. You can check Apache by pointing your browser at localhost and Tomcat by pointing your browser at localhost:8080 (you should get the default start page).

2. Configure mod_jk

Next, edit /etc/apache2/httpd.conf and add:

# Load mod_jk module
LoadModule jk_module modules/

JkWorkersFile /local/tomcat/conf/jk/

# Where to put jk logs
JkLogFile /local/apache2/logs/mod_jk_log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the log format
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”

# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat “%w %V %T”

# Send servlet for context /examples to worker named worker1
JkMount /your-url/* worker1

You can do this alternatively in your vhost. This configuration will send all jsp’s and all in the path /servlets-examples/* to Tomcat. If you know the exact path to your servlet, you can write:

JkMount /trn-webapp-0.8.1/map worker1

for example, where map is the servlet

Next, create /etc/apache2/ with the following content:


Then, goto /etc/tomcat5/base/ and check your server.xml. You should find something like this:

Make sure, it is enabled (without ).

At this point, you can edit also your /etc/tomcat5/base/tomcat-users.xml. You can replace it with this:

You should replace root and password with your own settings

For testing purposes, edit httpd.conf and set Document Root, Directory and Options:

ScriptAlias /modjk/ /usr/local/apache/modjk/

AllowOverride None
Options none
Order allow,deny
Allow from localhost

Now first restart Tomcat, then Apache. (If you change something in Tomcat, everytime restart Tomcat first and then restart Apache, too!)

Apache unusual bugs or warning

March 10, 2010

1. [warn] (128)Network is unreachable: connect to listener on [::]:80

Your apache config has some serious bugs.

Please check the IP addresses. Either of
the Listen-statement or of your Virtual Hosts.
“[::]:80” does not look good.


You can try changing these to “Listen” and :80 respectively including virtual host
listen address too.