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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Piping parameters to awk

Status
Not open for further replies.

CarlPender

Technical User
Apr 25, 2003
12
IE
Hi, I have a Suse7.3 Linux PC acting as a gateway with an APache server running. I have a web site set up and what I want to do is allow only certain MAC addresses onto the network as I choose. I have a script that adds certain MAC addresses onto the network which works perfectly if I type the MAC address in manually but I need to automate it. I'll nearly there I think but I need a little help.


Here goes.... I have a acript that matches an IP
address with it's respective MAC address via the
'arp' command. The script is as follows:

#!/bin/bash

sudo arp > /usr/local/apache/logs/users.txt

sudo awk '{if ($1 =="157.190.66.1" print $3}'
/usr/local/apache/logs/users.txt |
/usr/local/apache/cgi-bin/add

Here is a typical output from the arp command:

Address HWtype HWaddress Flags Mask Iface
157.190.66.13 ether 00:10:5A:B0:30:ED C eth0
157.190.66.218 ether 00:10:5A:5B:6A:11 C eth0
157.190.66.1 ether 00:60:5C:2F:5E:00 C eth0

As you can see I send this to a text file from which I
capture the MAC address for the respective IP address
("157.190.66.1") and then send this MAC address to
another script, called "add", which allows this MAC
address onto the network. This works perfectly when I
do it from a shell with the ip address typed in
maually.

My problem is that instead of actually typing in the
IP address (e.g "157.190.66.1"), I want to be able to
pipe the remote IP address of the user that is
accessing my web page at the time to this script as an
input.

In order to do this, I tried:

#!/bin/bash

read ip_address

sudo arp > /usr/local/apache/logs/users.txt
sudo awk '{if ($1 ==$ip_address) print $3}'
/usr/local/apache/logs/users.txt |
/usr/local/apache/cgi-bin/add

But I'm afraid this doesn't work. I'm wondering where
I'm going wrong. I also tried putting quotations
around the variable $ip_address but that doesn't work
either. On my CGI script I have the line 'echo
"$RENOTE_ADDR" | /usr/local/apache/cgi/bin/change' to
pipe the ip address of the user. I know this is
working because if I include the line 'echo
"$ip_address"' in my script then the ip address is
echoed to the screen


Basically what I want to know is how do you pipe something to a script(e.g. an IP address) and then use this variable in the awk script



I hope that I have made myself clear.



Thanks

Carl
 
#!/bin/bash

read ip_address

sudo arp > /usr/local/apache/logs/users.txt
sudo nawk -v awkAddr=${ip_address} '{if ($1 == awkAddr) print $3}'
/usr/local/apache/logs/users.txt |
/usr/local/apache/cgi-bin/add

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
No that doesn't seem to work either. I'm getting a lot of errors in my error log file but one of is:

sudo: nawk: command not found

what is nawk?
 
try using either

(a) the &quot;-v&quot; option
e.g.
awk -v myIP=$ip_address '{if ($1 == myIP) print $3}'


(b) the enviroment variable
e.g.
awk '{if ($1 == ENVIRON[&quot;ip_address&quot;]) print $3}'


A simple script to check what's available for you in your enviroment array, run this:
awk 'BEGIN {for(env in ENVIRON) print env &quot;=&quot; ENVIRON[env]}'
 
I'm not running this command on my sctual web page, I'm calling it from the script from the web page. The IP address is being sent from the sgi script to the script with the awk line in it so it's not one of the environmental variables
 
Then the &quot;-v&quot; should do the trick. Have you tried that?
 
ok, use 'awk' with '-v' as specified.

Are you on SOlaris by any chance?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Regarding the question 'what's nawk?':
Revised awk, offering more support for writing larger programs and tackling general-purpose programming problems. This new version, with minor improvements, is now codified by the POSIX standard. (from O'Reilly's &quot;sed & awk&quot;, page 13)
 
On HP-UX-11.00 both work:

awk -v myIP=${ip_address} 'BEGIN{print myIP}'

and

awk -v myIP=$ip_address 'BEGIN{print myIP}'

I don't know about Linux ...
 
Yeah they both work, so I can see why the other scripts dont work.

Thanks
 
if I use this script:

#!/bin/bash

read ip_address

sudo arp > /usr/local/apache/logs/users.txt

sudo awk -v awkAddr=$ip_address '{if ($1 == &quot;awkAddr&quot;) print $3}'
/usr/local/apache/logs/users.txt


I call it using a script called test which pipes an IP address to the script. When I call test I get

linux:/usr/local/apache/cgi-bin # ./test
/usr/local/apache/logs/users.txt: Address: command not found
/usr/local/apache/logs/users.txt: 157.190.66.13: command not found
/usr/local/apache/logs/users.txt: 157.190.66.218: command not found
/usr/local/apache/logs/users.txt: 157.190.66.1: command not found


This seems to me that all of the first column of the arp table is being printed out instead of the 3rd column of the 157.190.66.1 row.
 
sorry, but I thought my ORIGINAL post was like that:

sudo awk -v awkAddr=${ip_address} '{if ($1 == awkAddr) print $3}'

Why are you double-quoting?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Post the output of awk -W version, it should be a stable
gawk of the pre 3.1 series.
I use SuSE for all my dev and consulting work, very
good choice.

Vgersh is right as usual: you're replacing the variable
passed into awk with a string literal when using the
quotes.
 
I got it working. I used:


sudo awk '{if ($1 == &quot;'&quot;$ip_address&quot;'&quot;) print $3}' /usr/local/apache/logs/users.txt | /usr/local/apache/cgi-bin/add


Thanks very much guys you've been extremely helpful and I really appreciate you trying to help me.

Thanks again

Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top