3

As part of a batch script, I have the following command:

hostname=$(nmblookup -A $ip_address | awk '$2 == "<20>" {print $1}')

Which works fine from a functinality perspective, even for unresolved hosts.

The problem is that when the IP address is not reachable or the remote machine does not respond to the SMB request, the command takes about ten seconds to complete. Therefore, the question is simple: is there a way to lower the elapsed time in such cases? Or, in other words, is there a way to set a custom timeout for the nmblookup command?

NOTE: I'm interested in solutions that do not make use of SIGALRM or similar mechanisms; if they exist. The nmblookup version is 3.6.3 from Ubuntu 12.04 LTS.

C2H5OH
  • 145
  • 6

3 Answers3

3

I don't think the if statement will work, because nmap returns zero if it executes without error regardless of whether or not any hosts have the port open. Try the following parse of the nmap output, substituting whatever IP address range you need:

for ip_address in $(nmap -p 137 192.168.0.0/24 -oG - | grep netbios |grep -v closed | awk '{print $2}')
do
        hostname=$(nmblookup -A $ip_address | awk '$2 == "<20>" {print $1}')
        echo "$ip_address: $hostname"
done
2

I just use

timeout 1 nmblookup -A $ip_address

It kills nmblookup if running for more than 1 second.

Shloim
  • 136
  • 2
0

Circumvent the nmblookup(1) limitation:

if (nmap -p "${port} -sU -P0 "${ip_address}" &> /dev/null); then
  hostname=$(nmblookup -A $ip_address | awk '$2 == "<20>" {print $1}')
else
  printf "Host unreachable.\n"
fi

I'm assuming you'd need to test for netbios-ns 137/udp, modify the test to use the appropriate options and "${port}".

dawud
  • 14,918
  • 3
  • 41
  • 61