awk question - getting ip from ifconfig


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk question - getting ip from ifconfig
# 1  
Old 12-11-2009
awk question - getting ip from ifconfig

Hi there, I have a script which loops through the physical interfaces of a box and populates a variable called $NIC with the interface name

so if for example $NIC is equal to "e1000g0", I am trying to figure out some awk that will get the IP for that interface from ifconfig output

Code:
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000 
e1000g0: flags=201000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4,CoS> mtu 1500 index 2
        inet 10.1.1.1 netmask ffff0000 broadcast 10.1.255.255
        ether 0:14:4f:xx.xx.xa
e1000g1: flags=201000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4,CoS> mtu 1500 index 2
        inet 10.1.1.2 netmask ffff0000 broadcast 10.1.255.255
        ether 0:14:4f:xx.xx.xb

Ive been trying to set up an awk statement that sets a variable called X to "1" when it find the interface in question (in this case e1000g0) and then start searching for the line containing the string "inet", then print £2 and set X=0 again so that it wont print any more

Apologies for my inexperience with awk, but does am i close with the following syntax ? please feel free to shoot me down :-) ive been trying all sorts of variatiosn but i cant get it to work

Code:
#echo $NIC
e1000g0
# ifconfig -a|nawk 'BEGIN { NIC=$NIC ; X=0} ; /NIC/ {X=1} ; /inet/&&X{print $2 ; X=0}'

Any help on this would be really apreciated
# 2  
Old 12-11-2009
Code:
NIC=$NIC

shell replacement will not be handled by awk

you should probably do something like..

Code:
awk -v nic=$NIC '{ awk statments.....}'

# 3  
Old 12-11-2009
It seems to work when i do this

Code:
# ifconfig -a | nawk  'BEGIN {x=0};/e1000g0/{x=1};/inet/&&x{print $2}'
10.1.1.1

i.e if i define the nic name specifically in the command
# 4  
Old 12-11-2009
correct.
because in your previous statement NIC contains actually nothing. ( it is not replaced by e1000g0 what you were expecting )

if you really dont want to hardcode the value then use -v command line option as i suggested earlier.

instead
Code:
# ifconfig -a | nawk  'BEGIN {x=0};/e1000g0/{x=1};/inet/&&x{print $2}'

this should work

Code:
# ifconfig -a | nawk  -v nic="$NIC" 'BEGIN {x=0};/nic/{x=1};/inet/&&x{print $2}'

# 5  
Old 12-11-2009
Or:

Code:
ifconfig -a | awk -v var="e1000g0" '$0 ~ var{getline;print $2;exit}'

# 6  
Old 12-11-2009
Quote:
Originally Posted by anchal_khare
correct.
because in your previous statement NIC contains actually nothing. ( it is not replaced by e1000g0 what you were expecting )

if you really dont want to hardcode the value then use -v command line option as i suggested earlier.

instead
Code:
# ifconfig -a | nawk  'BEGIN {x=0};/e1000g0/{x=1};/inet/&&x{print $2}'

this should work

Code:
# ifconfig -a | nawk  -v nic="$NIC" 'BEGIN {x=0};/nic/{x=1};/inet/&&x{print $2}'



Thanks for your help on this, I have tried

Code:
#echo $NIC
e1000g0
# ifconfig -a | nawk  -v nic="$NIC" 'BEGIN {x=0};/nic/{x=1};/inet/&&x{print $2}'
#

unfortunately returns no output

again returns to the next line with no output .... i think i am missing something here

---------- Post updated at 09:39 AM ---------- Previous update was at 08:44 AM ----------

Quote:
Originally Posted by Franklin52
Or:

Code:
ifconfig -a | awk -v var="e1000g0" '$0 ~ var{getline;print $2;exit}'


Franklin, if i replace the awk with a nawk this works perfectly (thankyou). if i use awk, i get syntax errors

although i dont quite understand how your statement works as im more used to the learning the if/then awk constructs, i will use it because i cant get the ones that seem logical to me to work Smilie

thankyou very much all

Last edited by rethink; 12-11-2009 at 10:37 AM..
# 7  
Old 12-11-2009
Quote:
Originally Posted by rethink

although i dont quite understand how your statement works as im more used to the learning the if/then awk constructs, i will use it because i cant get the ones that seem logical to me to work Smilie

thankyou very much all
Explanation:

Code:
$0 ~ var		# if the pattern matches the line
{getline;print $2;exit}	# then read the next line, print the second field and exit

In contrast with the next statement getline reads the next line and continues with the next statement
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

2. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

3. Shell Programming and Scripting

ifconfig!!!!

why the ifconfig command is not working in my machine? it says "-bash: ifconfig: command not found" why its says that? actually i m looking for "how can I know the Network Interface Card physical address?" Requesting u all for help. thanks (3 Replies)
Discussion started by: moco
3 Replies

4. Solaris

Solaris 10 question on ifconfig

On prior versions to solaris 10 when I want to add/change the IP address of an interface I will use this command. ifconfig eri0 down ifconfig eri0 192.xxx.xxx.xxx netmask 255.xxx.xxx.xxx up then change /etc/hosts so that /etc/hostname.eri0 picks up on the correct IP. I strange thing... (2 Replies)
Discussion started by: sparcguy
2 Replies

5. UNIX for Dummies Questions & Answers

ifconfig eth0 and awk and grep

if i have the command ifconfig eth0, what will the output be and how can i use either awk or grep to isolate and display the IP address, subnet mask and physical address of my host (1 Reply)
Discussion started by: carlvernon
1 Replies

6. OS X (Apple)

ifconfig

Hi, I type (as root) "ifconfig en1 ether aa:aa: " but it doesnt change the mac of my airport. Can anyone help me? Thx. (0 Replies)
Discussion started by: Jariya
0 Replies

7. UNIX for Dummies Questions & Answers

ifconfig

I am trying to change an IP address on a machine running HPUX10 After I change it I can ping it from the outside but it completely locks the console. After a reboot it returns back to its previous IP. Any ideas?? Thanks Brian (7 Replies)
Discussion started by: breigner
7 Replies

8. HP-UX

HP-UX ifconfig question.

This will most likely be a real dumb question for a HP-UX admin, but here it goes anyhow. lan0 - is up and configured lan1 - is down, I want to bring it up. lanconfig....ifconfig is there a difference? My real question is, in solaris there is an /etc/hostname."?" file, is there a... (4 Replies)
Discussion started by: viRaven
4 Replies

9. UNIX for Dummies Questions & Answers

Newbie ifconfig question

SunOS Release 5.7. Original ifconfig -a output: pnt0: flags=863<UP, BROADCAST,NOTRAILERS........ inet 10.2.34.49 netmask fffffe00 broadcast 10.2.34.255 ... Attempted to change ip address for pnt0. Typed the following command and forgot the "netmask" "ifconfig pnt0 10.2.35.49... (1 Reply)
Discussion started by: forbin24
1 Replies

10. UNIX for Dummies Questions & Answers

Further question on 'ifconfig' output

I asked a similar question earlier and got a very good answer but a new doubt came up. This is a few lines of a '/sbin/ifconfig' command on my PC: RX packets:3781025 errors:0 dropped:0 overruns:0 frame:0 TX packets:1941909 errors:0 dropped:0 overruns:0 carrier:0 Does the RX and TX packets... (1 Reply)
Discussion started by: mint1981
1 Replies
Login or Register to Ask a Question