0

I am making a simple ping script in python, and was looking to add functionality for getting host names for IPs that are up. To do this, im getting the output of nmblookup -A {ip} using os.popen, and parsing the output. the problem im running into is that for systems where nmblookup wont work (such as routers), the command takes a long time to get an error, whereas when the command runs successfully, it retruns results in under a second. My question is how to only wait N seconds for the nmblookup command to return something, and if it doesn't, move on with the program? PS, this is all in linux.

AWein
  • 1
  • 1
  • Does this answer your question? [Kill or terminate subprocess when timeout?](https://stackoverflow.com/questions/4158502/kill-or-terminate-subprocess-when-timeout) – Evgeny Jul 19 '20 at 04:02

1 Answers1

0

You can prefix the command with timeout. Refer to the man page of it.

You can do a Popen and run your command as

root@ak-dev:~# timeout 1 sleep 20
root@ak-dev:~# echo $?
124