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.

#!/usr/bin/php
<?php

$opt = getopt('', array('host:'));

if(isset($opt['host'])) {
  
  // get ip from host
  $host = starts_with($opt['host'], 'http') ? parse_url($opt['host'], PHP_URL_HOST) : $opt['host'];
  $ip = gethostbyname($host);
  echo $ip . PHP_EOL;
  
} else {
  
  // get internal ip for computer

  $ip = shell_exec("/sbin/ifconfig | grep 'inet 192' | awk '{ print $2 }'");
  echo trim($ip) . PHP_EOL;

}


// helper functions

function starts_with($haystack, $needle) {
  return $needle === "" || strpos($haystack, $needle) === 0;
}

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:

// let's just save the file as getip.php
./getip.php --host=thebarton.org

That will output the ip address for thebarton.org. You could also put http://thebarton.org 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.

$opt = getopt('', array('host:'));

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.

if(isset($opt['host'])) {
  
  // get ip from host
  $host = starts_with($opt['host'], 'http') ? parse_url($opt['host'], PHP_URL_HOST) : $opt['host'];
  $ip = gethostbyname($host);
  echo $ip . PHP_EOL;
  
}

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).

$ip = shell_exec("/sbin/ifconfig | grep 'inet 192' | awk '{ print $2 }'");
echo trim($ip) . PHP_EOL;

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:

dig +short thebarton.org

You would replace thebarton.org 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 comment

Find the Best SEO in Fort Lauderdale, Florida (Plantation SEO) | Drupal & Worpress Develpment