perl script to find the host name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    perl script to find the host name

    I am using following code to find the hostname/dns name of a linux server.


    Code:
    (grep {chomp;} system ('hostname -s'))[0];
    And the name of the server gets printed as XXX. And I am trying assign this value to $host as below. But it is not working and $host name is null. How do I assign XXX to $host.

    Code:
    $host =(grep {chomp;} system ('hostname -s'))[0];
    print "The host name is $host \n";
    Please let me know. Thanks.
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    perlfaq9

    Code:
    use Net::Domain qw(hostname hostfqdn hostdomain);
    
    my $host = hostname();
    print "The host name is $host \n";
    --Kevin

    Comment

    • lilly07
      New Member
      • Jul 2008
      • 89

      #3
      Thank you so much and it worked.

      Comment

      • Ritusriv
        New Member
        • Jul 2008
        • 7

        #4
        Also you can try as,

        Code:
        use Sys::Hostname;
        
        my  $host = hostname;

        Comment

        Working...