The point of gethostbyname is to resolve hostnames into IP addresses. If you pass an IP address, there is nothing to resolve, so, of course, you get back the IP address.

It sounds like what you want to do is see if there is a host "out there" that will respond to an IP address. You could try gethostbyaddr. That function maps an IP address to a host. If there is no associated host, it will fail.

However, success may not mean much. Most importantly, it won't tell you if the remote IP address is connected to the inter/intranet and ready to recieve requests. Many systems cache IP-host address associations. Unless you clear the cache, you could get a hostname back without even being on line. Even if you are on line, getting a host name back still doesn't tell you if the IP address is on-line. All it means is that you successfully connected to a domain name server (DNS) that has an entry for the hostname-IP address association.

To see if the IP address is on-line, you'd need to actually connect to the other machine. A common way to do that is "ping" (see Net::Ping or ping). However, pinging isn't 100% reliable. Some internet service providers (see Wikipedia) are afraid of ping being used for a DOS attack and do not allow pings to enter into their networks from the outside. Systems and local networks may be configured to block outgoing ping requests as well. (I seem to recall that Vista's default firewall configuration blocks either incoming or outgoing ICMP (the protocol used by ping) requests, but it has been a while. In any case if you use ping on a machine/network with a firewall, do check your firewall configuration if you have problems.

Alternatively, if you are having trouble using ping and you know something about the services offered by the remote IP address, you can try another protocol. For example, you could try sending an http, ssh, or ftp request and see if you get a response.

Best, beth


In reply to Re: To know the existence of an ip address by ELISHEVA
in thread To know the existence of an ip address by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.