Shell Script help required

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.

Shell Script help required

Postby shad0w » Sat Dec 16, 2006 4:00 am

As Salaam O Alaikum,

I am working on a script to copy all the denied entries from iptables to a file such that i can have source address formated in a particular way i am doing the following but not getting the required results

#!/bin/bash
ipaddr=`iptables -L | grep DENY | awk '{print$4}'`
total=`iptables -L | grep DENY | awk '{print$4}' | wc -l`
for((i=1; i<="$total"; i++))
do
echo $ipaddr | awk '{print$(i)}'
done
exit $?

The problem lies in this line " echo $ipaddr | awk '{print$(i)}' " as i am not able to use loop in here.. if i am using {print$2} its working fine ...
I am not able to figure out the problem also I am not able to assign
var=$ipaddr | awk '{print$(i)}'
The format for out i require is some thing like this

The address block is $var for spamming reason
The 2nd address is $var for spamming reason

Your help in this will be very valuable as i am stuck with this and cant figure out the solution

Wasalaam
Abdus Samad
shad0w
Cadet
 
Posts: 2
Joined: Sun Sep 24, 2006 2:23 am

Re:

Postby LinuxFreaK » Sat Dec 16, 2006 1:14 pm

Dear shad0w,
Salam,

Use below script.

Code: Select all

#!/bin/sh

IPADDR=`iptables -L -n | grep DROP | awk '{print $4}'`
LOGFILE="/root/ip.txt"

for i in $IPADDR
do
   echo $i >> $LOGFILE
done


Best Regards,
Farrukh Ahmed
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Postby shad0w » Sun Dec 17, 2006 11:38 am

Salaam,
Thanks LinuxFreak got the problem solved.

Wasalaam
Abdus Samad
shad0w
Cadet
 
Posts: 2
Joined: Sun Sep 24, 2006 2:23 am


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron