Visit Our UNIX and Linux User Community


Hostname in Perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hostname in Perl?
# 1  
Old 10-16-2009
Hostname in Perl?

I have a few servers whose hostname is "app.prod.cbo.im.foobar.com"

prod is the environment I want to capture in a perl script. I want the script to run on qa and it should be environment independent.So if I am running the script on app.qa.cbo.im.foobar.com I want to capture qa out of that hostname and use that later on in my perl script.

Generally in a shell script I do this -

hostname |awk -F"." '{print $2}'

What would be the perl equivalent of this so that I can put that in a variable.

I did use a2p but got some very verbose results which I did not understand. I want something one liner which I can feed into a variable.

Thanks,
Jack.
# 2  
Old 10-16-2009
How about something like:

Code:
$var = `hostname |awk -F"." '{print $2}' `

# 3  
Old 10-16-2009
Yes thats what I use in a shell script but I need a PERL equivalent so I can use in a PERL script.

Thanks,
Jack.
# 4  
Old 10-16-2009
check out Sys::Hostname on CPAN.

On another note PERL should really be spelled Perl.
# 5  
Old 10-16-2009
Code:
#!/usr/bin/perl

$var = `hostname`;
@alist = split(/\./, $var) ;
print $alist[1];



---------- Post updated at 07:28 PM ---------- Previous update was at 07:21 PM ----------

Or using Frank's suggestion:

Code:
#!/usr/bin/perl
use Sys::Hostname;
$var = hostname;
@alist = split(/\./, $var) ;
print $alist[1];


Previous Thread | Next Thread
Test Your Knowledge in Computers #423
Difficulty: Medium
C# is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

deleted (0 Replies)
Discussion started by: hce
0 Replies

2. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

3. UNIX for Dummies Questions & Answers

get the server's hostname

i read that if i issue : cat /etc/sysconfig/network > textfile i will be able to determine the hostname of the server that my linux workstation is connected to. but there are several other lines outputted that i do not need. i just need the hostname part. is there any other unix... (2 Replies)
Discussion started by: mbaste2
2 Replies

4. Shell Programming and Scripting

mailx and hostname

Hi how to include the hostname in the subject of an email send by mailx? mailx -s "here goes the subject `uname -n`" abc@xyz.com or `hostname` list the words as is thank you (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

5. UNIX for Advanced & Expert Users

What hostname is a client using?

Is there a way to find out what name a client is using to connect to a server? For example: client is telnet'ing or ftp'ing to Server B.domain.com, using name C.domain.com. (B and C resolve to the same IP). Can we, on the server B find out that the client used C.domain.com to connect? I have tried... (1 Reply)
Discussion started by: mugambo
1 Replies

6. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies

7. UNIX for Dummies Questions & Answers

change hostname

Hi, I asked this question whenI was running solaris 8 and got some very good answers. I've just uograded to Solaris 10 and there seems to quite a few changes so I need to know again. https://www.unix.com/showthread.php?t=8547 ------------ Hi (now solaris 10) I know how to change the... (2 Replies)
Discussion started by: fishman2001
2 Replies

8. HP-UX

Change IP and Hostname

Hello.. I wanted to know how to change IP and Hostname on HPUX system, and I don't want to make it as NIS master either. Could someone tell me the files I need to modify and make sure it's on network? Thanks! :o (3 Replies)
Discussion started by: catwomen
3 Replies

9. IP Networking

looking up hostname

Using Solaris 8 (or WINXP). I am trying to look up a specific DNS hostname, but I don't know which DNS server houses that entry. How can I find the hostname? nslookup gives me the following: C:\>nslookup hostname Server: dnsserver Address: x.x.x.x *** dnsserver can't find hostname:... (2 Replies)
Discussion started by: dangral
2 Replies

10. UNIX for Dummies Questions & Answers

Hostname

Hello, I am installing redhat linux 6.2 on an intel based system. Whether i want to know any naming conventions should i follow. ie Any convention to follow to name a linux machine(To give hostname). Simillarly for domain name also. Please suggest in this regard (1 Reply)
Discussion started by: bache_gowda
1 Replies

Featured Tech Videos