Reverse Lookup of NetBIOS Name

If you are a network administrator or must work with large Windows user, you may want to obtain the netbios name of each machine programmatically. Why? There are many reasons to know netbios name by given IP address.

  1. Create a list of IP and associate name
  2. Find name conflict
  3. Obtain machine information from IP

Fortunately, samba package provides a tool called "nmblookup" to help us in this situation. It is very easy to reverse lookup the name using option "-A". However, there are lots of information returned.

[sugree@magi1 admintools]$ nmblookup -A 158.108.34.56
Looking up status of 158.108.34.56
        VIKRIT          <00> -         M  
        WORKGROUP       <00> -  M  
        VIKRIT          <20> -         M  
        WORKGROUP       <1e> -  M  
 
        MAC Address = 00-C0-9F-60-C9-E2

Then we should easily get the name using awk. As a result, I wrote a script namely "ip2nb.sh" as follow.

#!/bin/sh
 
IP=$1
 
nmblookup -A $IP | awk '/<20>/ { print tolower($1) }'

So I can obtain name of 158.108.34.56 in just one command.

[sugree@magi1 admintools]$ ./ip2nb.sh 158.108.34.56
vikrit

Very cool

Thanks for this simple solution. Not knowing AWK would have required me to create a less elegant solution to this problem. PS. I had to escape the '$' with '\$' in the tolower function to get this to work on my system.

single quote

Oops! It should be single quote.

Post new comment