Solved

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

Posted on 2014-10-24
19
1,574 Views
Last Modified: 2014-11-04
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?
0
Comment
Question by:projects
  • 6
  • 6
  • 4
  • +1
19 Comments
 
LVL 61

Expert Comment

by:gheist
ID: 40403602
It never worked.
0
 
LVL 7

Assisted Solution

by:Stampel
Stampel earned 250 total points
ID: 40403834
That should work :

# curl ifconfig.me
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40403923
Try this:

curl echoip.com
0
 

Author Comment

by:projects
ID: 40404060
@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.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40404311
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
0
 
LVL 61

Expert Comment

by:gheist
ID: 40404322
Maybe you hist rate limit of site because of such heavy usage...
0
 

Author Comment

by:projects
ID: 40404371
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.
0
 
LVL 7

Expert Comment

by:Stampel
ID: 40404693
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.
0
 
LVL 7

Expert Comment

by:Stampel
ID: 40404740
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/
:)
0
New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40404836
Just ping ifconfig.me and you would have seen the same 😁
0
 
LVL 7

Expert Comment

by:Stampel
ID: 40404915
would not give you the host curl is looking for and neither your IP address neither the port curl is querying (http)
0
 

Author Comment

by:projects
ID: 40404938
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.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40405011
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?
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 40405034
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

0
 

Author Comment

by:projects
ID: 40405268
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.
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 250 total points
ID: 40405472
Add --max-time <seconds> to the curl command(s) - random + timeout doesn't make much sense to me.

Just put 3 or 4 of them in sequence, quit when one has returned IP address:
ip=""
for site in echoip.com ip.appspot.com icanhazip.com ifconfig.me
do
  ip=$(curl -f --max-time 2 2>/dev/null ${site})
  if [ "${ip}" = "" ]; then
    continue
  else
    break
  fi 
done;

if [ "${ip}" = "" ]; then
  echo "IP not found"
else
  echo "Site used: ${site}"
  echo "IP address is: ${ip}"
fi

Open in new window

0
 

Author Comment

by:projects
ID: 40406491
Does the trick, thank you.
0
 

Author Comment

by:projects
ID: 40406496
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']
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now