Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a variable inside of a function with awk...

Status
Not open for further replies.

segment

ISP
Jun 15, 2004
225
US
bash function inside of an awk statement...

So I created a function called IPT ...

function IPT {

cat /etc/hosts.deny|\
awk '!/#/ &&
/./ &&
! a[$0]++
{print "iptables -A INPUT -s "$1" -i eth0 -d xxx.xxx.xxx.xxx -p TCP --dport 22 -j REJECT"}'|\
awk '/iptables/ && !/#/ && !/-s -i/'|sh

}

Anyway... What I need to do is this... (it's scripted)

ifaddr=$( ifconfig -a|awk '/inet/ && !/inet6/ && !/127.0/ && !/192.268/{print $2}' )

function IPT {

cat /etc/hosts.deny|\
awk '!/#/ &&
/./ &&
! a[$0]++
{print "iptables -A INPUT -s "$1" -i eth0 -d $ifaddr -p TCP --dport 22 -j REJECT"}'|\
awk '/iptables/ && !/#/ && !/-s -i/'|sh

}


For those who don't understand what's going... (UUOC police don't arrest me now!)

!/#/ && ... Ignore comments
/./ && ... grep decimal points
! a[$0]++ ... return uniq values..

then print it
{print "iptables -A INPUT -s "$1" -i eth0 -d xxx.xxx.xxx.xxx -p TCP --dport 22 -j REJECT"}'|\
Here I had to re-pipe it to awk. For some reason it still gave me comments... Not a problem
awk '/iptables/ && !/#/ && !/-s -i/'|sh
re-ignore comments, and the line "-s -i" then send it through to be processed...



perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
I need the value of $ifaddr to be taken into the awk statement:

ifaddr=$( ifconfig -a|awk '/inet/ && !/inet6/ && !/127.0/ && !/192.168/{print $2}' )

function IPT {

cat /etc/hosts.deny|\
awk '!/#/ &&
/./ &&
! a[$0]++
{print "iptables -A INPUT -s "$1" -i eth0 -d $ifaddr -p TCP --dport 22 -j REJECT"}'|\
awk '/iptables/ && !/#/ && !/-s -i/'

}

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Hi

Code:
ifaddr=$( ifconfig -a|awk '/inet/ && !/inet6/ && !/127.0/ && !/192.168/{print $2}' )

function IPT {

awk [red]-vifaddr="$ifaddr"[/red] '!/#/ &&
/./ &&
! a[$0]++
{print "iptables -A INPUT -s "$1" -i eth0 -d [red]"ifaddr"[/red]  -p TCP --dport 22 -j REJECT"}'|\
awk '/iptables/ && !/#/ && !/-s  -i/' [red]/etc/hosts.deny[/red]

}

Feherke.
 
Weird... Linux stood on standstill processing that... Net/FreeBSD passed it through with no problem...

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
awk '!/#/ && /\./ && !a[$0]++
{print "iptables -A INPUT -s "$1" -i eth0 -d [tt][!]'[/!][/tt]$ifaddr[tt][!]'[/!][/tt] -p TCP --dport 22 -j REJECT"}' /etc/hosts.deny |\
awk '/iptables/ && !/#/ && !/-s -i/'


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top