Get full computer name using Perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajays
    New Member
    • Apr 2007
    • 7

    Get full computer name using Perl

    I want to get the full computer name using the ActiveState Perl on windows .
    Actually i can get that using the "hostname" command, but i want to have the output of the "hostname" command stored in some variable using perl. And i wan to use that variable in my script.

    Is it possibel to write a script which will store the return value of the command executed on the command prompt and how ???

    Kindly help
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    You have posted this in the Articles section. I am moving it to the Perl forum.

    ADMIN

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      I don't understand your question well enough to answer it. You can store return values various ways: in variables or in a database or in a text file. If the value is only needed for the life of the script store it in a variable, if the value needs persistence, store it in a file or database or look into the Storable module, which comes with perl.

      Comment

      • prn
        Recognized Expert Contributor
        • Apr 2007
        • 254

        #4
        I'm guessing that what ajays means is that he doesn't know how to capture the information that "hostname" outputs. OTOH, I might also note that "hostname" does not reliably put out an FQDN (fully qualified domain name). Assuming that ajays is using the Active State Perl (that's by far the most common Perl for windows), he should have the Net:: Domain module available. See http://www.xav.com/perl/site/lib/Net/Domain.html for specs. This may prove more reliable than just "hostname".

        Try:
        Code:
        use strict;
        use Net::Domain qw (hostname hostfqdn hostdomain);
        
        my $hostname = `hostname`;
        print "my hostname is $hostname\n";
        
        my $fqdn = hostfqdn();
        print "my fqdn is $fqdn\n";
        and see what you get. On my windows box, I get:

        Code:
        my hostname is phobos
        my fqdn is phobos.bsu.edu
        HTH,
        Paul

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          You're probably right Paul. Hopefully he comes back and reads your reply.

          Comment

          • HeinV
            New Member
            • Oct 2013
            • 1

            #6
            I stumbled into this topic years later.
            Just want to point out that prn's example appears to work, but does not do that it suggests.

            prn>> use Net::Domain qw (hostname hostfqdn hostdomain);
            prn>> my $hostname = `hostname`;

            The first line 'activates' the hostname function,
            But the second name executes (back-tick / qx) a command using the string 'hostname'... which typically gives the same result, and does appear to work, but less reliably so.
            Just drop the back-ticks and it will use the function, not a fresh process.

            Cheers,
            Hein

            Comment

            • RonB
              Recognized Expert Contributor
              • Jun 2009
              • 589

              #7
              Hein,

              I believe you misinterpreted what Paul was demonstrating.

              The OP was asking about how to capture the output of the system's hostname command. Paul was demonstrating the difference between doing that and using the hostfqdn() function from the module. It would have been more clear if he didn't include/import the hostname() and hostdomain() functions in the use statement.

              Here's an updated (corrected?) version.
              Code:
              #!/usr/bin/perl
              
              use warnings;
              use strict;
              use Net::Domain qw(hostname hostfqdn);
               
              my $hostname = `hostname`;
              print "my hostname is $hostname\n";
              
              my $fqdn_hostname = hostname();
              print qq(my "fqdn" hostname is $fqdn_hostname\n\n);
              
              my $fqdn = hostfqdn();
              print "my fqdn is $fqdn\n";

              Comment

              Working...