Shell Script help required

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
shad0w
Cadet
Posts: 2
Joined: Sun Sep 24, 2006 2:23 am

Shell Script help required

Post by shad0w »

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
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re:

Post by LinuxFreaK »

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
shad0w
Cadet
Posts: 2
Joined: Sun Sep 24, 2006 2:23 am

Post by shad0w »

Salaam,
Thanks LinuxFreak got the problem solved.

Wasalaam
Abdus Samad
Post Reply