Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-10-24
19
Medium Priority
?
2,182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 4
  • +1
19 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

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 …
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Suggested Courses

661 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