2

I'm using dig utility to find public IP address.

The command I used is:

dig +short myip.opendns.com @resolver1.opendns.com

but I had problem since on some other computers it took so long, so I added a timeout after 5 seconds:

dig +short +time=5 +tries=1 myip.opendns.com @resolver1.opendns.com

and it fails on some as expected, but on some other computers it never finishes. All it does is hangs...

Any idea why and how can this be fixed to timeout after 5 seconds as expected ?

EDIT:

Already tried timeout but it still hangs. I think it stucks while resolving DNS. For example, I can't ping www.google.com, but can 172.217.20.4.

1 Answer 1

5

As for using aplicational timeouts in some utilities, when there are connectivity issues, it is not always guaranteed the application will stop on it's own with some outside "coercion".

I would use an external utility to kill it after some time just in case, for the problematic situations where it hangs.

As in:

timeout 5 dig +short +time=5 +tries=1 myip.opendns.com @resolver1.opendns.com

From man timeout

NAME top

   timeout - run a command with a time limit

SYNOPSIS top

   timeout [OPTION] DURATION COMMAND [ARG]...
   timeout [OPTION]

DESCRIPTION top

   Start COMMAND, and kill it if still running after DURATION.
5
  • I suspect 5s can be slow in some situation, but I do not know the OPs network. Commented Jun 3, 2019 at 10:05
  • I already tried timeout, but it still hangs. I don't know why... Commented Jun 3, 2019 at 10:11
  • I think it stucks while resolving DNS. I can't ping www.google.com, but can 172.217.20.4 Commented Jun 3, 2019 at 10:13
  • timelimit might work better. Will add it as an alternative when i get to my Debian machine. Commented Jun 3, 2019 at 12:29
  • (At least FreeBSD and GNU) timeout has a -k option to SIGKILL after an additional time.
    – A.B
    Commented Jun 4, 2019 at 1:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .