How to get a domain’s ip address from the command line

There comes a time when you want to check the ip address of a domain, you know, just in case you have the host file overridden or when you’re transferring a domain over to a new server. Here’s a little script to help you do that, and it’s based on PHP because that’s God’s programming language of choice.

You’ll notice that I have a lot of stuff in there. Let’s go over it. But here’s how you would call it:

That will output the ip address for You could also put in there because I made it work for both the domain only, and the domain with URI.

Now, let’s go over the script line by line.

This is how I get a command argument so PHP can use it. I picked “host” as my command. You can change it to be whatever your heart desires.

I have it checking to make sure the $opt[‘host’] variable is set. If it’s not set, it pulls the internal ip address of your computer/server. I’m assuming you’re going to do this on your MAC because why would you have a Windows computer in this day and age?

It then checks if http is in the beginning, and if it is, it grabs the hostname so you can be sloppy and leave the http:// in there in case you’re copying and pasting from the browser.

Then it calls gethostbyname, which is a PHP function that does all the work.

Optionally, it will grab the internal ip address of your computer (works on MAC).

That parses the ifconfig command and spits out your computer’s local ip address.

Then at the end I have a helper function to check if a string starts with something. I used it to check if it starts with http.

Alternative, non-php way to check domain ip address

You don’t have to use PHP. You can type something as simple as:

You would replace with your domain to check against. You could go either way, but my php code let’s you copy and paste from the browser into the command line, and it’s easier to remember. How often do you use the dig command? Exactly.

Leave a Reply