Link to home
Start Free TrialLog in
Avatar of projects
projects

asked on

curl: (6) Couldn't resolve host 'ifconfig'

From a Linux box, I can run;

# curl ifconfig

which typically returns my public IP.

However, now and then, there is an error;

curl: (6) Couldn't resolve host 'ifconfig'

I know the DNS server is up and running so why does this work and why does it not work at times?
Avatar of gheist
gheist
Flag of Belgium image

It never worked.
SOLUTION
Avatar of Stampel
Stampel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gerwin Jansen
Try this:

curl echoip.com
Avatar of projects
projects

ASKER

@gheist;
>It never worked.

Sorry, that was a typo and meant to be; curl ifconfig.me

@Stampel;
>That should work :
># curl ifconfig.me

Yes, it does but my question is wanting to understand what would cause it not to work sometimes. It seems to be a dns timeout yet it's not because I tested a dns request at the same time I tested this and dns worked fine.

@Gerwin Jansen;
>Try this:
>curl echoip.com

I guess my code should use more than one service, randomly.

The line is;

MYIP=$(curl ifconfig.me)

I guess I need a little logic here so that if one service times out, use the next to get the IP into $MYIP. However, I would prefer using say two or three services, and randomly.

If someone could give me the necessary code, that would solve my question.
The ifconfig.me site is not accessible for me, don't know why, echoip.com is accessible and is working. So why ifconfig.me is not working all the time for you I don't know just that echoip.com is - I'd use echoip.com
Maybe you hist rate limit of site because of such heavy usage...
I don't think it's a limit problem because it works now and then, randomly and with various servers. I don't hit them very often, maybe once a day with 5 servers.

Either way, the solution would be a little code to allow for randomly switching between providers so that none ever hit a limit, are overused, etc.
Your subject was
"curl: (6) Couldn't resolve host 'ifconfig'"

This is the response you would get when asking to shell
"[root@xxx ~]# curl ifconfig
=> curl: (6) Couldn't resolve host 'ifconfig'

When you are running curl ifconfig.me you would not get the error
[root@xxx ~]# curl ifconfig.me
=> 215.1.2.3

If you get other errors you can tcpdump to see what curl tries to hit.
Probably the host is not available at this time or a dns problem at this time thats all.
For the curiosity i checked with tcpdump ...
curl ifconfig.me is connectoing to host www1266uf.sakura.ne.jp and port http

check http://www1266uf.sakura.ne.jp/
:)
Just ping ifconfig.me and you would have seen the same 😁
would not give you the host curl is looking for and neither your IP address neither the port curl is querying (http)
I am not sure what we are all talking about anymore at this point.

Yes, 'curl ifconfig.me' is what my code uses but sometimes it times out which is why I'd like to use a random source in the code itself. The 'curl ifconfig' was a typo.
You could just change your code to 'curl echoip.com' or 'curl ip.appspot.com' or 'curl  icanhazip.com', how does your code look like where the curl command is implemented? We can create some 'random' select I suppose, what shell are you using?
Quick bash example:

number=$(echo $RANDOM % 4 + 1 | bc)
case $number in
  1 )
    ip=$(curl 2>/dev/null echoip.com) ;;
  2 )
    ip=$(curl 2>/dev/null ip.appspot.com) ;;
  3 )
    ip=$(curl 2>/dev/null icanhazip.com) ;;
  4 )
    ip=$(curl 2>/dev/null ifconfig.me) ;;
esac
echo "IP address is: $ip"

Open in new window

Linux, bash script.
Yes, I could just use another service but I'd rather have something a little better.
Perhaps a timeout so that if one service doesn't respond, move to the next and always random.

All I'm looking for is to stick the public IP only into MYIP.

My current line reads;

MYIP=$(curl ifconfig.me)

This particular device doesn't have 'bc' on it.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Does the trick, thank you.
That code is fine but I'm surprised not one person mentioned the fact that the client is connecting to the php app means that php could simply pick up the client's IP, no need for any external services.

I'm not sure how php would do that but the script is sending $MYIP back to php and so long as php would put the connecting IP into $MYIP, then I would not even need the code posted for this question.

php code section is;

 myip = "' . $_POST['myip']