Как ping определяет IP-адрес веб-сайта?

2186
isomorphismes

Я пытаюсь лучше понять различные инструменты сетевого запроса, такие как nc, curl, whois, dig, nslookupи в то же время немного больше об архитектуре интернет - запросов, серверов и т.п. (NIC, запись, MX) в то время как уже зная, немного о TCP, серверах имен, регистраторах доменов, снифферах пакетов, HTTP-заголовках и IP-адресах. Это мое прошлое, вот мой вопрос.

Когда я digили whoisскажем www.valgrind.org (или valgrind.org) я получаю как минимум два разных IP-ответа: 178.250.76.80 и 172.16.0.23 # 53.

$ nslookup valgrind.org Server: 172.16.0.23 Address: 172.16.0.23#53  Non-authoritative answer: Name: valgrind.org Address: 178.250.76.80 

Попытка перейти к одному из них w3mили chromiumприводит к 403 Forbiddenошибке. Кроме того, я не уверен, к какому из них перейти, но ping каким-то образом способен это выяснить! Если я ping valgrind.org(или ping www.valgrind.org) это выбирает

$ ping valgrind.org PING valgrind.org (178.250.76.80) 56(84) bytes of data. 64 bytes from 178.250.76.80: icmp_req=1 ttl=50 time=80.2 ms 

Откуда он это знает? А другой инструмент, который я должен использовать, чтобы узнать, как мой браузер переходит от 178.250.76.80к valgrind.orgи загружает что - то?

1
Это довольно не по теме здесь. Я помогу вам начать, хотя: первые «два ответа» вы получаете не два разных ответа. Первый адрес - это адрес вашего DNS-сервера; сервер nslookup отправил запрос. Единственный ответ - 178.250.76.80. Это для профессионалов, а не людей, стремящихся изучить основы. yoonix 10 лет назад 0
@yoonix Я действительно смотрел на "man ping", прежде чем спрашивать здесь, но это не отвечает на мой вопрос. isomorphismes 10 лет назад 0

4 ответа на вопрос

4
Iain

There are different programs and protocols at work here each is configured to respond independently of the others.

To find an IP address, a linux host will generally use the hosts: directive in /etc/nsswitch.conf to determine which order to query the various sources. A typical entry would be

hosts: files dns 

which says check the files (typically /etc/hosts ) then the dns system. So a search will be conducted of the /etc/hosts file and if it contains an entry for valgrind.org the associated IP address will be returned. If /etc/hosts doesn't contain an entry for valgrind.org, then a request will be made to the DNS. This involves reading the contents of /etc/resolv.conf to determine which servers should be contacted to request DNS information ...

Looking at the output from your command, you're not getting two different addresses. The first part of the output is telling you which server was contacted by nslookup (172.16.0.23) and also on which port (#53) to get the answer. We also know that there is no correctly configured PTR record for 172.16.0.23 as, as RobM's answer shows, the Server would be shown as a name if it were.

The second part of the output is the information that you requested and tells you that 178.250.76.80 is the IPv4 address of valgrind.org.

When you contact 178.250.76.80 on port 80 (http) directly you get a 403 forbidden because that's how the http server at that address has been configured. It is most likely configured as a name based virtual server and thus requires a valid http Host: (sec 14.23) header to route your request to the appropriate vhost.

The ping command is part if the ICMP protocol suite and is an ICMP echo request, the host at 178.250.76.80 responds to ping because it has been configured to do so and sends an ICMP Echo Reply to each Echo Request.

1
Rob Moir

You need to understand how nslookup works.

nslookup

The first part of the answer, as should become clearer here, is the name server you're talking to. The answer it provides is the second part

So in my example, I'm asking nslookup to query my currently configured DNS server about where it thinks www.serverfault.com is to be found.

nslookup replies with the address of the server it used to run the query, and the answer it gave. This is important if you're specifying a particular DNS server to run dig or nslookup against, which is a fairly normal part of troubleshooting name resolution issues.

1
psusi

Вы неправильно понимаете вывод nslookup. Он не дает два ответа, а только один. Первый адрес, откуда он получил ответ. pingвыполняет точно такую ​​же задачу и получает точно такой же результат. Причина, по которой вы получаете 403 при попытке дать IP-адрес браузеру, заключается в том, что на сервере работает виртуальный хостинг. Виртуальный хостинг позволяет серверу иметь несколько сайтов с одним IP-адресом. Он решает, какой сайт будет обслуживать вас, основываясь на имени, которое браузер сообщает, что вы ввели. Когда вы вводите IP-адрес, сервер не знает, какой сайт вы хотели.

0
johnshen64

it will depend on your OS and ping version. if it is linux, you can find out all what is doing by using strace.

strace ping valgrind.org

step by step it will show you. too long to post, but here is my output so that i know at some point it checks the hosts file

.

. . open("/etc/host.conf", O_RDONLY) = 3 . . .