Linux Command to Translate Domain Name to Ip

Linux command to translate domain name to IP

Use this

$ dig +short stackoverflow.com

69.59.196.211

or this

$ host stackoverflow.com

stackoverflow.com has address 69.59.196.211
stackoverflow.com mail is handled by 30 alt2.aspmx.l.google.com.
stackoverflow.com mail is handled by 40 aspmx2.googlemail.com.
stackoverflow.com mail is handled by 50 aspmx3.googlemail.com.
stackoverflow.com mail is handled by 10 aspmx.l.google.com.
stackoverflow.com mail is handled by 20 alt1.aspmx.l.google.com.

How to get the ip address of the server in linux?

If you are trying to get this information from BASH, you probably want to use nslookup. For example:

[michaelsafyan@codemage ~]$ nslookup redmine.org
Server: 8.8.8.8
Address: 8.8.8.8#53

Non-authoritative answer:
Name: redmine.org
Address: 46.4.36.71

I should add that an IP address does NOT represent a computer, but rather a network interface. And a computer can have any number of network interfaces (and IP addresses). Also, a website or domain may have many machines (and consequently many more network interaces and IP addresses). When querying with nslookup you will get at least one IP address for the given domain name (assuming DNS is working and it doesn't fail for one reason or another), but it won't necessarily give you all the addresses.

Portable way to resolve host name to IP address

If you want something available out-of-the-box on almost any modern UNIX, use Python:

pylookup() {
python -c 'import socket, sys; print socket.gethostbyname(sys.argv[1])' "$@" 2>/dev/null
}

address=$(pylookup google.com)

With respect to special-purpose tools, dig is far easier to work with than nslookup, and its short mode emits only literal answers -- in this case, IP addresses. To take only the first address, if more than one is found:

# this is a bash-specific idiom
read -r address < <(dig +short google.com | grep -E '^[0-9.]+$')

If you need to work with POSIX sh, or broken versions of bash (such as Git Bash, built with mingw, where process substitution doesn't work), then you might instead use:

address=$(dig +short google.com | grep -E '^[0-9.]+$' | head -n 1)

dig is available for cygwin in the bind-utils package; as bind is most widely used DNS server on UNIX, bind-utils (built from the same codebase) is available for almost all Unix-family operating systems as well.

Bash, convert IP address in batch

dig has the -f command line option to read a list of domain names from a file. From the manpage,

The -f option makes dig operate in batch mode by reading a list of lookup requests to process from the file filename. The file contains a number of queries, one per line. Each entry in the file should be organized in the same way they would be presented as queries to dig using the command-line interface.

Running

dig +short -f list

where the file named list contains

www.google.com
www.yahoo.com
www.facebook.com

will give you output like

74.125.239.114
74.125.239.112
74.125.239.116
74.125.239.113
74.125.239.115
fd-fp3.wg1.b.yahoo.com.
206.190.36.45
206.190.36.105
star.c10r.facebook.com.
31.13.77.6

Print a certain line in linux or options for nslookup


$ host 75.126.153.202 |sed -e 's/.* //'
www.cyberciti.biz.

How to get hostname from IP (Linux)?

In order to use nslookup, host or gethostbyname() then the target's name will need to be registered with DNS or statically defined in the hosts file on the machine running your program. Yes, you could connect to the target with SSH or some other application and query it directly, but for a generic solution you'll need some sort of DNS entry for it.

Easy way to get IP address from hostname using a Unix shell

You can do this with standard system calls. Here's an example in Perl:

use strict; use warnings;
use Socket;
use Data::Dumper;

my @addresses = gethostbyname('google.com');
my @ips = map { inet_ntoa($_) } @addresses[4 .. $#addresses];
print Dumper(\@ips);

produces the output:

$VAR1 = [
'74.125.127.104',
'74.125.127.103',
'74.125.127.105',
'74.125.127.106',
'74.125.127.147',
'74.125.127.99'
];

(On the command-line, the same script can be written as: perl -MSocket -MData::Dumper -wle'my @addresses = gethostbyname("google.com"); my @ips = map { inet_ntoa($_) } @addresses[4 .. $#addresses]; print Dumper(\@ips)')

You can do this similarly in other languages -- see the man page for the
system calls at man -s3 gethostbyname etc.

How to get the URL or IP of a load balancer from DNS of a website?

I think you are getting confused by the IP addresses returned by nslookup. Those IP addresses are the actual addresses of your ELB, not the balanced instances.

The reason why a load balancer offers multiple addresses is basically to provide a fallback, cover different availability zones (if you would have 3 instances in 3 different availability zones, you would see 3 IPs) etc.
However please bear in mind that those IP addresses are not fixed and might change any time, therefore use the provided DNS for the balancer instead whenever applicable.

Apart from that you are on the right track using getaddrinfo.



Related Topics



Leave a reply



Submit