[comp.unix.programmer] Easy way to find hostname/domainname

nms@saukh.rd.jvd.su (Nickolay Saukh) (09/28/90)

I need advice/software to figure out the hostname & domain name for C
programs and shell scripts. In C for hostname it seems to be quite easy:

	#if defined(BSD)
		gethostname(...);
	#endif

	#if defined(USG)
		uname(...);
	#endif

	#if defined(ANYTHING_ELSE)
		popen("uuname -l", ...);
	#endif

But how to find domain name? Look at some file with magic name? What the
name should be (with portabily issues in mind)?

The same problem for shell scripts. I know about 'hostname' command in
BSD world, 'uuname -l' for SV world (and 'uname' also).

This problem is one more 'iron curtain' (-:, which separates *NIXes
of the world. Help them unite!!!
-- 
| Nickolay Saukh | nms@saukh.rd.jvd.su | ...!fuug!demos!jvdrd!saukh!nms |

verber@pacific.mps.ohio-state.edu (Mark Verber) (10/01/90)

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

towfiq@interlan.Interlan.COM (Mark Towfiq) (10/03/90)

In article <1990Sep28.162012.14325@saukh.rd.jvd.su>
nms@saukh.rd.jvd.su (Nickolay Saukh) writes:

   I need advice/software to figure out the hostname & domain name for C
   programs and shell scripts. In C for hostname it seems to be quite easy:

   [how to get hostname deleted]

   But how to find domain name? Look at some file with magic name? What the
   name should be (with portabily issues in mind)?

In C, I would do a gethostbyname() to get the FQDN, and then take
everything after the first "." to be the domain.

   The same problem for shell scripts. I know about 'hostname' command in
   BSD world, 'uuname -l' for SV world (and 'uname' also).

So you want the FQDN in a shell script?  I do not know of a portable
way to do this.  You could look for /etc/resolv.conf, or you could
look for a program like "host", or "nslookup", and maybe AWK its
output?