Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-10-24
19
Medium Priority
?
2,378 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
18 Comments
 
LVL 62

Expert Comment

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

Assisted Solution

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

# curl ifconfig.me
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40403923
Try this:

curl echoip.com
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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 38

Expert Comment

by:Gerwin Jansen, EE MVE
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 62

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
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
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 38

Expert Comment

by:Gerwin Jansen, EE MVE
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 38

Expert Comment

by:Gerwin Jansen, EE MVE
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 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 1000 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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month21 days, left to enroll

810 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