1827854 Members
1495 Online
109969 Solutions
New Discussion

AWK Help Needed

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

AWK Help Needed

Hello masters of scripting,

I am trying do a small script that will do linkloop tests on interfaces, and I need your help.

Here is a sample output of just a bare lanscan (minus a few interfaces):

lanscan
Hardware Station Crd Hdw Net-Interface NM MAC HP-DLPI DLPI
Path Address In# State NamePPA ID Type Support Mjr#
0/5/1/0/4/0 0x001A4BF56734 2 UP lan2 snap2 1 ETHER Yes 119
0/5/1/0/4/1 0x001A4BF56735 3 UP lan3 snap3 2 ETHER Yes 119

I thought I was on to something when I was able to get the following formated output:

# lanscan | grep -v Hardware | grep -v Path | awk '{print $3, $2}'
2 0x001A4BF56734
3 0x001A4BF56735
4 0x001A4BF56736
5 0x001A4BF56737
6 0x001A4BF56394
7 0x001A4BF56395
8 0x001A4BF56396
9 0x001A4BF56397
0 0x001A4B0766B6
1 0x001A4B0766B7

And then I put the output into a for loop, and it falls apart:

for i in `lanscan | grep -v Hardware | grep -v Path | awk '{print $3, $2}'`
do
echo linkloop $i
done
linkloop 2
linkloop 0x001A4BF56734
linkloop 3
linkloop 0x001A4BF56735
linkloop 4
linkloop 0x001A4BF56736
linkloop 5
linkloop 0x001A4BF56737
linkloop 6
linkloop 0x001A4BF56394
linkloop 7
linkloop 0x001A4BF56395
linkloop 8
linkloop 0x001A4BF56396
linkloop 9
linkloop 0x001A4BF56397
linkloop 0
linkloop 0x001A4B0766B6
linkloop 1
linkloop 0x001A4B0766B7


Please help.
12 REPLIES 12
Patrick Wallek
Honored Contributor

Re: AWK Help Needed

linkloop works on the link address, or mac address, or the card, so your extraneous information (the INT #, field 3) is useless.

Try this:

for INT in $(lanscan | grep -v -e Hardware -e Path | awk '{print $3}')
do
echo linkloop ${INT}
done
F Verschuren
Esteemed Contributor

Re: AWK Help Needed

this will do the trick:
for i in `netstat -in|grep -vE "lo0|Name|lan[0-9*]:"|awk '{print substr($0,4,6)}'`
do
echo $i | grep -q 90[0-9]
if [[ $? -eq 0 ]]; then
for j in `lanscan -q | grep $i`
do
linkloop -i $i `yes|lanadmin -g $j | grep "Station Address" | awk '{print $4}'` 2>&1 | grep -q "OK" && echo "lan$j is OK" || echo "lan$j is NOT OK"
done
else
linkloop -i $i `lanadmin -g $i | grep "Station Address" | awk '{print $4}'` 2>&1 | grep -q "OK" && echo "lan$i is OK" || echo "lan$i is NOT OK"
fi
done
Patrick Ware_1
Super Advisor

Re: AWK Help Needed

Sorry folks.
This:

for i in `lanscan | grep -v Hardware | grep -v Path | awk '{print $3, $2}'`
do
echo linkloop $i
done
linkloop 2
linkloop 0x001A4BF56734
linkloop 3
linkloop 0x001A4BF56735
linkloop 4
linkloop 0x001A4BF56736
linkloop 5
linkloop 0x001A4BF56737
linkloop 6
linkloop 0x001A4BF56394
linkloop 7
linkloop 0x001A4BF56395
linkloop 8
linkloop 0x001A4BF56396
linkloop 9
linkloop 0x001A4BF56397
linkloop 0
linkloop 0x001A4B0766B6
linkloop 1
linkloop 0x001A4B0766B7


Should be:

for i in `lanscan | grep -v Hardware | grep -v Path | awk '{print $3, $2}'`
do
echo linkloop -i $i
done
linkloop -i 2
linkloop -i 0x001A4BF56734
linkloop -i 3
linkloop -i 0x001A4BF56735
linkloop -i 4
linkloop -i 0x001A4BF56736
linkloop -i 5
linkloop -i 0x001A4BF56737
linkloop -i 6
linkloop -i 0x001A4BF56394
linkloop -i 7
linkloop -i 0x001A4BF56395
linkloop -i 8
linkloop -i 0x001A4BF56396
linkloop -i 9
linkloop -i 0x001A4BF56397
linkloop -i 0
linkloop -i 0x001A4B0766B6
linkloop -i 1
linkloop -i 0x001A4B0766B7
F Verschuren
Esteemed Contributor
Solution

Re: AWK Help Needed

my last post just checks the configgured lans..
you need:
lanscan | awk '{ print $3 " " $2 }'|grep -v -e Sta -e Add |while read line
do
linkloop -i $line
done

so you awk you heve to replease the , whit " ".

The rest was fine...
Sandman!
Honored Contributor

Re: AWK Help Needed

Replace the for loop with a while loop as in:

lanscan | awk '/[0-9]/{print $3, $2}' | while read i
do
linkloop -i $i
done
James R. Ferguson
Acclaimed Contributor

Re: AWK Help Needed

Hi Patrick:

Perhaps:

#!/usr/bin/sh
LANS=$(lanscan | awk '!/Hardware|Path/ {print $3":"$2}' )
for LAN in ${LANS}
do
LNBR=$(echo ${LAN} | cut -d: -f1)
echo "LAN #${LNBR}"
LINK=$(echo ${LAN} | cut -d: -f2)
linkloop ${LINK}
done

...The most important point is NOT to use 'grep' whwn 'awk' is designed to pattern match. Avoid the useless, extra process!

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: AWK Help Needed

Thanks all. Here is what I went with:

lanscan | awk '/[0-9]/{print $3, $2}' | while read i
do
echo linkloop -i $i
echo "----------------------"
linkloop -i $i
echo
done


The output:

linkloop -i 0 0x00306E13F682
----------------------
Link connectivity to LAN station: 0x00306E13F682
error: expected primitive 0x30, got DL_ERROR_ACK
dl_error_primitive = 0x2d
dl_errno = 0x04
dl_unix_errno = 57
error - did not receive data part of message

linkloop -i 2 0x00306E0C541F
----------------------
Link connectivity to LAN station: 0x00306E0C541F
-- OK

linkloop -i 3 0x00306E38D363
----------------------
Link connectivity to LAN station: 0x00306E38D363
-- OK

linkloop -i 1 0x00306E21D5B7
----------------------
Link connectivity to LAN station: 0x00306E21D5B7
-- OK

Patrick Ware_1
Super Advisor

Re: AWK Help Needed

lanscan | awk '/[0-9]/{print $3, $2}' | while read i
do
echo linkloop -i $i
echo "----------------------"
linkloop -i $i
echo
done
Patrick Ware_1
Super Advisor

Re: AWK Help Needed

Final Solution:

lanscan | awk '/[0-9]/{print $3, $2}' | while read i
do
echo LAN `echo $i | awk '{print $1}'`
echo linkloop -i $i
echo "----------------------"
linkloop -i $i
echo
done

Output:

LAN 0
linkloop -i 0 0x00306E13F682
----------------------
Link connectivity to LAN station: 0x00306E13F682
error: expected primitive 0x30, got DL_ERROR_ACK
dl_error_primitive = 0x2d
dl_errno = 0x04
dl_unix_errno = 57
error - did not receive data part of message

LAN 2
linkloop -i 2 0x00306E0C541F
----------------------
Link connectivity to LAN station: 0x00306E0C541F
-- OK

LAN 3
linkloop -i 3 0x00306E38D363
----------------------
Link connectivity to LAN station: 0x00306E38D363
-- OK

LAN 1
linkloop -i 1 0x00306E21D5B7
----------------------
Link connectivity to LAN station: 0x00306E21D5B7
-- OK

James R. Ferguson
Acclaimed Contributor

Re: AWK Help Needed

Hi (again) Patrick:

Think optimization and reduce this to one 'awk' process:

# cat ./mylans
#!/usr/bin/sh
LANS=$(lanscan | awk 'NR>2 {print $3":"$2}' )
for LAN in ${LANS}
do
LNBR=${LAN%%:*}
LINK=${LAN##*:}
echo "\nlinkloop -i ${LNBR} ${LINK}\n----------------------"
linkloop ${LINK}
done

# ./mylans

linkloop -i 0 0x080009D97070
----------------------
Link connectivity to LAN station: 0x080009D97070
-- OK

linkloop -i 1 0x0060B083C26C
----------------------
Link connectivity to LAN station: 0x0060B083C26C
error: get_msg2 getmsg failed, errno = 4
-- FAILED
frames sent : 1
frames received correctly : 0
reads that timed out


Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: AWK Help Needed

James,

Whenever I run your script on my server, there are no lan interfaces that come back ok. I get the following:

root@server:/> cat linkloop_test1
#!/usr/bin/sh
LANS=$(lanscan | awk 'NR>2 {print $3":"$2}' )
for LAN in ${LANS}
do
LNBR=${LAN%%:*}
LINK=${LAN##*:}
echo "\nlinkloop -i ${LNBR} ${LINK}\n----------------------"
linkloop ${LINK}
done


root@server:> sh linkloop_test1


linkloop -i 0 0x00306E13F682
----------------------
Link connectivity to LAN station: 0x00306E13F682
error: expected primitive 0x30, got DL_ERROR_ACK
dl_error_primitive = 0x2d
dl_errno = 0x04
dl_unix_errno = 57
error - did not receive data part of message

linkloop -i 2 0x00306E0C541F
----------------------
Link connectivity to LAN station: 0x00306E0C541F
error: expected primitive 0x30, got DL_ERROR_ACK
dl_error_primitive = 0x2d
dl_errno = 0x04
dl_unix_errno = 57
error - did not receive data part of message

linkloop -i 3 0x00306E38D363
----------------------
Link connectivity to LAN station: 0x00306E38D363
error: expected primitive 0x30, got DL_ERROR_ACK
dl_error_primitive = 0x2d
dl_errno = 0x04
dl_unix_errno = 57
error - did not receive data part of message

linkloop -i 1 0x00306E21D5B7
----------------------
Link connectivity to LAN station: 0x00306E21D5B7
error: expected primitive 0x30, got DL_ERROR_ACK
dl_error_primitive = 0x2d
dl_errno = 0x04
dl_unix_errno = 57
error - did not receive data part of message

James R. Ferguson
Acclaimed Contributor

Re: AWK Help Needed

Hi (again) Patrick:

> Whenever I run your script on my server, there are no lan interfaces that come back ok.

Ah, my apologies. The manpages note that "-i PPA ...If this option is omitted, linkloop uses the first PPA it encounters in an internal data structure."

Frankly, I missed that and would need to change:

# linkloop ${LINK}

...to:

# linkloop -i ${LNBR} ${LINK}

...thus:

# cat ./mylans
#!/usr/bin/sh
LANS=$(lanscan | awk 'NR>2 {print $3":"$2}' )
for LAN in ${LANS}
do
LNBR=${LAN%%:*}
LINK=${LAN##*:}
echo "\nLAN ${LNBR}\nlinkloop -i ${LNBR} ${LINK}\n----------------------"
linkloop -i ${LNBR} ${LINK}
done

...BUT, this could be done without parameter substitution in the first place using a read (thinking as Sandman showed):

# cat ./mylans2
#!/usr/bin/sh
lanscan | awk 'NR>2 {print $3,$2}' | \
while read LNBR LINK
do
echo "\nLAN ${LNBR}\nlinkloop -i ${LNBR} ${LINK}\n----------------------"
linkloop -i ${LNBR} ${LINK}
done

...

If nothing else, we let the shell do most of our work and spawn as absolutely few processes as necessary.

Regards!

...JRF...