Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I want to find whether the given ipaddress/hostname is present in the network.

I've tried the gethostbyname and when I give unavailable hostname, it is returning undef, but when I give unavailable ip address, it is returning the same IP.

use Socket; $packed_ip = gethostbyname shift; if (defined $packed_ip) { $ip_address = inet_ntoa($packed_ip); print $ip_address; } else { print "Host not exist\n"; }

Replies are listed 'Best First'.
Re: To know the existence of an ip address
by Old_Gray_Bear (Bishop) on Sep 14, 2009 at 09:58 UTC
    When you ask gethostbyname() "Is this a valid host name?" what you are doing is sending a query to the Domain Name System asking "Is there a mapping between this Human Readable string and an IP address?" DNS responds either 'yes, and here it is' or 'no, such a mapping does not exist'.

    When you ask "Is this a valid IP address", the form of the string is checked, and if it matches the specification in the RFC (four strings separated by periods; each string numeric and in the range 0 -- 255), then the the IP address is valid.

    If you want to know whether a valid IP address is actually in use, then you will have to try it out. The usual tool is ping() with an appropriate time-out; or traceroute() if you want to know the path information as well.

    Update -- straightened out the syntax in the second paragraph.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: To know the existence of an ip address
by ikegami (Patriarch) on Sep 14, 2009 at 07:08 UTC

    Define "present" and "available". Ping is usually used. (I'm sure someone will explain the caveats...)

    when I give unavailable ip address, it is returning the same IP.

    While a domain ceases to exist when it's no longer registered, 1.2.3.4 is a perfectly good name for IP address 1.2.3.4 whether 1.2.3.4 is currently "available" or not. An IP address doesn't cease to exist when it's not "available".

      Sounds like a job for nmap then just grep for the host/ip - I know that's a nasty way to go about it though -sL (list scan) Also check out the Nmap::Parser module here http://search.cpan.org/~apersaud/Nmap-Parser/

      You may be getting a call from the security team at your company after running lol
Re: To know the existence of an ip address
by ELISHEVA (Prior) on Sep 14, 2009 at 10:14 UTC

    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

Re: To know the existence of an ip address
by Bloodnok (Vicar) on Sep 14, 2009 at 10:30 UTC
    The traditional means of establishing the existence (on a network) of a host is, as mentioned elsewhere in this thread, to use one of the forms of ping.

    However, be warned that, when pinging hosts/addresses on the inter-web, the results may not provide a definitive answer since an ever increasing number of sites/networks have firewall rules configured to prevent, by one means, or another, the forwarding of ping requests &/or replies - thus a target host that appears to be failing to respond to a ping, may in fact exist and be on-line but the traffic is disallowed.

    A user level that continues to overstate my experience :-))