By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

perl script to find the host name

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

Expand|Select|Wrap|Line Numbers
  1. (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.

Expand|Select|Wrap|Line Numbers
  1. $host =(grep {chomp;} system ('hostname -s'))[0];
  2. print "The host name is $host \n";
Please let me know. Thanks.
Aug 4 '08 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 971

Expand|Select|Wrap|Line Numbers
  1. use Net::Domain qw(hostname hostfqdn hostdomain);
  3. my $host = hostname();
  4. print "The host name is $host \n";
Aug 4 '08 #2

P: 89
Thank you so much and it worked.
Aug 4 '08 #3

P: 7
Also you can try as,

Expand|Select|Wrap|Line Numbers
  1. use Sys::Hostname;
  3. my  $host = hostname;
Aug 6 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.