Easy way to find hostname/domainname

Mark Verber verber at pacific.mps.ohio-state.edu
Tue Oct 2 02:02:38 AEST 1990


There are a variety of ways to try and get a hostname/fully qualified
domain name for a shell script.  Some of the stratagies I have seen
include:

1. Write a replacement version of hostname which will run under on any
machine and return the appropriate information.  You might want to add
a -fqdn switch to give a full qualified domain name since many people
run with shortnames (ie hostname without domainnames tacks on).

2. Create a file called something like /etc/sysinfo which contains
fields which decribe how to get domainame and a short hostname, and
whatever other useful things you need.  For example, on my Suns this
file would look something like this:
    `arch`:/vmunix:"bsd":"sunos-41":`/bin/hostname`:"mps.ohio-state.edu"
and have a shell/perl/awk/etc script that rips important info out.
	
3. Make some assumptions.  For example, the following script should
run on all the systems I currently maintain (I think... I am writing
off the top of my head).  On the other hand, I am sure the is some
version of Unix will break it.  Assuptions are that the person is either
running with a domain name resolver, or has a /etc/domainname file.

#! /bin/csh

if ( -e /etc/resolv.conf ) then
	set domain = `grep domain /etc/resolv.conf`
	set domain = $domain[2]
else if ( -e /etc/domainname ) then
	set domain = `cat /etc/domainname`
else
	unset domain
endif

if ( -e /bin/hostname ) then
	set host = `hostname`
else if ( -e /bin/uname ) then
	set host = `uname -n`
else if ( -e /usr/bin/uuname ) then
	set host = `uuname -l`
else
	set host = unknown
endif

set host = (`echo $host | tr '.' ' '`)
set host = $host[1]

if ( $?domain ) then
	setenv HOST $host.$domain
else
	setenv HOST $host
endif



More information about the Comp.unix.programmer mailing list