Solved

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

Posted on 2014-10-24
19
1,467 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on lā€¦
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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

708 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

13 Experts available now in Live!

Get 1:1 Help Now