UPDATE I need to clarify that Jon8RFC-LT and DOMAIN are also just generic examples of dynamic content, like the IP address and MAC address; nmblookup retrieves and displays entirely dynamic content based on the ip address. If awk is used, I need to have a way to pull 4 dynamic values from the nmblookup: IP, hostname/asset name, domain name, MAC address. Sorry for the confusion, I updated the code to make it more clear.
I have been searching and using my Linux book for a couple of days and cannot find what I need for awk/gawk/grep/egrep/sed (I think I need one or more of those, elegantly). In a bash script, I'm running:
su_nmblookup=$(nmblookup -A $ipaddress)
which returns
WARNING: The "idmap backend" option is deprecated added interface eth0 ip=a07d::a07d:a07d:a07d:a07d%eth0 bcast=b57d::ffff:ffff:ffff:ffff%eth0 netmask=ffff:ffff:ffff:ffff:: added interface eth1 ip=b57d::b57d:b57d:b57d:b57d%eth1 bcast=a07d::ffff:ffff:ffff:ffff%eth1 netmask=ffff:ffff:ffff:ffff:: added interface eth0 ip=234.234.234.234 bcast=12.12.12.12 netmask=255.255.0.0 Socket opened. Looking up status of 123.123.123.123 JON8RFC-LT <00> - B <ACTIVE> DOMAIN <00> - <GROUP> B <ACTIVE> JON8RFC-LT <20> - B <ACTIVE> DOMAIN <1e> - <GROUP> B <ACTIVE> MAC Address = 4F-A2-4F-A2-4F-A2
The best I've managed, is to chop it down with this code:
display=${su_nmblookup/#*Looking/\Looking}
Looking up status of 123.123.123.123 JON8RFC-LT <00> - B <ACTIVE> DOMAIN <00> - <GROUP> B <ACTIVE> JON8RFC-LT <20> - B <ACTIVE> DOMAIN <1e> - <GROUP> B <ACTIVE> MAC Address = 4F-A2-4F-A2-4F-A2
However, what I'd like to know is how to return either of these cleaned up formats. I want to learn how the grep/awk/sed works with extracting data with these two examples, one with preserving the formatting, and one with just new lines. I had a hell of a time even getting the quoting/coding to work properly here because of the formatting and gt/lt symbols!
Looking up status of 123.123.123.123 JON8RFC-LT DOMAIN 4F-A2-4F-A2-4F-A2
OR, simply
JON8RFC-LT DOMAIN 123.123.123.123 4F-A2-4F-A2-4F-A2
Thank you for your help!
Assuming the output of nmblookup -A 123.123.123.123
is redirected to file input.txt
awk '/Looking up status of/ {print} /JON8RFC-LT/ {if(a!=1){print "\t"$1;a=1}} /DOMAIN/ {if(b!=1){print "\t"$1;b=1}} /MAC Address/ {print "\t"$4}' input.txt
Updated as Etan Reisner suggested to be shorter:
awk '/Looking up status of/ {print} /JON8RFC-LT/ && !a {print "\t"$1;a=1} /DOMAIN/ && !b {print "\t"$1;b=1} /MAC Address/ {print "\t"$4}' input.txt
output:
Looking up status of 123.123.123.123
JON8RFC-LT
DOMAIN
4F-A2-4F-A2-4F-A2
Updated for Dynamic content
awk '/Looking up status of/,/MAC Address/ {print; getline;print "\t"$1;getline;print "\t"$1;getline;getline;getline;getline;print "\t"$4;exit 0}' input.txt
Assumes on the two lines after line Looking up ...
you want the first word. Then ignores three lines and then prints MAC address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With