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

Hi, I'm trying to find the name of my computer when it is on a network, from a remote connection I can see that is something along the lines of: dhcp-offices8-187.x.x.x, how do I use perl to find this? Oh, and a very merry christmas and a happy new year to all.

Replies are listed 'Best First'.
Re: Find a networked computer name
by dempa (Friar) on Dec 20, 2002 at 13:22 UTC
    I don't know how portable this is. I'm on Unix. Also, this code needs checking of system call return values before serious use!
    use Sys::Hostname; use Socket; my $hostname = gethostbyaddr(gethostbyname(hostname()),AF_INET); print "$hostname\n";

    Update: The reason I included the calls to gethostby* is in case you want the domainname also. hostname() from Sys::Hostname doesn't always include that.

    -- 
    dempa

Re: Find a networked computer name
by tachyon (Chancellor) on Dec 20, 2002 at 13:31 UTC

    System calls are quick and easy and you probably have at least one of nslookup dig and host. hostname will also tell you who you are.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Find a networked computer name
by BrowserUk (Patriarch) on Dec 20, 2002 at 13:34 UTC

    Update: I just re-read your question and...I'm not sure if I read it correctly the first time.

    1) Do you want to find, from within Perl, the network visible name of the local computer?

    Or

    2) Find the name of your computer, as seen by the network, using Perl on a remote computer?

    If you meant 1), then the following should do it. If you meant the second, that's different and more info is needed.

    </update>

    use Sys::Hostname; print hostname;

    Should work pretty much anywhere, as the docs say

    Sys::Hostname - Try every conceivable way to get hostname

    On (some/most/all) Win32 you can do

    print $ENV{COMPUTERNAME};

    but the first one above is portable.


    Examine what is said, not who speaks.

Re: Find a networked computer name
by gjb (Vicar) on Dec 20, 2002 at 13:22 UTC

    Have a look at the gethost* family of built-in functions documented in perlfunc.

    Hope this helps, -gjb-

Re: Find a networked computer name
by surfmonkey (Acolyte) on Dec 20, 2002 at 13:53 UTC
    Thanks for all the answers, but they aren't quite what I'm looking for! I can find out my local IP address and the name of the computer and which workgroup it is in. But what I really want is the DHCP network address for it. If I run netstat on a remote connection I can see the DHCP name ( dhcp-offices8-187.xxxx.xx.xx) and ipconfig gives me the DHCP IP address. Thx in advance.
      OK I think we have some nomenclature problems here. I assume you are running on a windos box.

      You know the IP address of your computer? yes? (if not then ipconfig)

      That IP address may refer to two different names. Firstly there is the netbios name - which is what you have when you use explorer to find other computers in your workgroup. Secondly there is the domain name, which is what your computer is called when people try to use standard IP and DNS to find it.

      It sounds like you want the second one. You can find this name by doing a reverse DNS lookup of your ip address on your local DNS Server. The dig program is a good way to do this and there exists a script called perldig if you don't have a real dig.

      Actually in your case the DHCP/DNS name appears to be 'dhcp-offices8-'.$ip_address where $ip_address is your IP address so why bother doing anything more complex?

      Dingus


      Enter any 47-digit prime number to continue.
Re: Find a networked computer name
by Marza (Vicar) on Dec 20, 2002 at 22:41 UTC

    You can use the tieregistry module as long as the machines are windows and have remote registry capability. Windows ME does not! You also neeed authority for remote access.

    Here is a snippit:
    use Win32::TieRegistry( Delimiter=>"/" ); my $key; my $computer = "xxx.xxx.xxx.xxx"; my $name; if ($key = $Registry->Open("//$computer/LMachine/SYSTEM/Cu +rrentControlSet/" . "Control/ComputerName/ComputerName/", {Access=>KEY_REA +D}) ) { unless ($name = $key->GetValue("ComputerName")) { print "Could not attach to read ComputerName o +n $computer\n"; } print "Name is $name\n"; } else { print "failed\n"; }

    Update:

    Guess I should read the question next time. The above will work if you were trying to get a remote host name from an ip address. Where is my coffee!