head     1.1;
access   ;
symbols  ;
locks    ; strict;
comment  @# @;


1.1
date     86.04.01.12.58.18;  author mark;  state Exp;
branches ;
next     ;


desc
@@



1.1
log
@Initial revision
@
text
@#! /bin/csh
#
# $Header: check_hostnam,v 1.2 86/03/25 12:06:39 mark Exp $
#
# $Log:	check_hostnam,v $
# Revision 1.2  86/03/25  12:06:39  mark
# I think it now puts a comment line immediately before the server's
# entry in /etc/hosts.  I think.
# 
# Revision 1.1  86/03/25  11:19:02  mark
# Initial revision
# 
#
#################
#
# Variables:
#	HOSTNAME	= name of this machine
#	HOSTID		= hostid in form xx000000
#	ADDRS		= internet address
#	ADX		= 4 element array, holds 4 fields ADDRS
#
#
set noglob
set OHOSTNAME = `grep '^[ \t]*/bin/hostname' /etc/rc.local | awk '{print $2}'`
set OHOSTID = `grep '^[ \t]*/bin/hostid' /etc/rc.local | awk '{print $2}'`
set OIFCONFIGLINE = `grep '^[ \t]*/etc/ifconfig' /etc/rc.local`
if ( ($OHOSTNAME == UNKNOWN || $#OHOSTNAME == 0 ) \
    &&  ($OHOSTID == 00000000 ||  $#OHOSTID == 0 )) then
	set valid_rc = false
else 
	set valid_rc = true
endif

if ($valid_rc == true) then
	if ($#OHOSTNAME != 0 ) then
		echo The existing hostname in /etc/rc.local is $OHOSTNAME
	endif
	if ($#OHOSTID != 0 ) then
		echo The existing hostid in /etc/rc.local is $OHOSTID
	endif
	echo -n "To update these entries, type 'y': "
	set resp = ($<)
	if ($resp != 'y' && $resp != 'Y') then
		  goto c_loop
	endif
endif

#
#
# now get the hostname from the user.
#
get_host:
echo -n  "Please enter the hostname by which this machine is to be known: "
set HOSTNAME = ($<)
if ( $#HOSTNAME == 0) then
	goto get_host
endif
#
# get address, and check for validity
#
get_addrs:
echo -n  "Please enter the Internet address for this machine: "
set ADDRS = ($<)
if ( $#ADDRS == 0) then
	goto get_adrs
endif
#
# make sure it looks like nn.nn.nn.nn
#
grep -s '^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' << EOFEOFEOF
$ADDRS
EOFEOFEOF
if ( $status != 0 ) then
	echo ""
	echo "Address must be in form aa.nn.nn.bb where"
	echo "1 <= aa <= 255"
	echo "0 <= nn <= 255"
	echo "1 <= bb <= 255"
	echo "Please re-enter."
	echo ""
	goto get_addrs
endif
set ADX=`echo $ADDRS | awk '{FS="."} {print $1 " " $2 " " $3 " " $4}'`
if ( $ADX[1] < 1 || $ADX[1] > 255 || $ADX[2] < 0 || $ADX[2] > 255 \
  || $ADX[3] < 0 || $ADX[3] > 255 || $ADX[4] < 1 || $ADX[4] > 255) then
	echo ""
	echo "Address must be in form aa.nn.nn.bb where"
	echo "1 <= aa <= 255"
	echo "0 <= nn <= 255"
	echo "1 <= bb <= 255"
	echo "Please re-enter."
	echo ""
	goto get_addrs
endif
#
# FAR OUT! We now have what looks like a valid hostname and net address.
# Now make sure that there isn't already a name/net combination like that
# in the hosts file.  First check for an identical net address, and if
# that's ok, look for a the hostname in the same local net as the specified
# one.
#
grep -s "^${ADDRS}[ \	]" /etc/hosts
if ( $status == 0 ) then
	echo ""
	echo -n "The entry "
	grep  "^${ADDRS}[ 	]" /etc/hosts
	echo "is already in /etc/hosts.  Use another internet address" 
	echo "or remove that entry from the host file before proceding."
	echo ""
	goto get_host
endif
grep -s "^${ADX[1]}.*[ \t]*${HOSTNAME}" /etc/hosts
if ( $status == 0 ) then
	echo ""
	echo -n "The entry "
	grep  "^${ADX[1]}.*[	 ]*${HOSTNAME}" /etc/hosts
	echo "is already in /etc/hosts.  Use another name" 
	echo "or remove that entry from the host file before proceding."
	echo ""
	goto get_host
endif
#
# We have a hostname ($HOSTNAME) and address ($ADDRS), now need to get
# interface type.
#

set HOSTID = `/usr/lib/admin.scripts/dec_to_hex $ADX[4]`000000

cp /etc/rc.local /etc/rc.local.orig
echo "saving /etc/rc.local in /etc/rc.local.orig"

if ($#OHOSTNAME == 0) then
ex /etc/rc.local << EOF > /dev/null
1
i
#
/bin/hostname	$HOSTNAME
.
w
q
EOF
else
	sed "/^[ \t]*\/bin\/hostname/s/^.*/\/bin\/hostname	$HOSTNAME/" \
		/etc/rc.local  > /tmp/admin.$$
	cp /tmp/admin.$$ /etc/rc.local
	/bin/rm -f /tmp/admin.$$
	
endif

if ($#OHOSTID == 0) then
ex /etc/rc.local << EOF > /dev/null
/^\/bin\/hostname/a
/bin/hostid	$HOSTID
.
w
q
EOF
else
	sed "/^[ \t]*\/bin\/hostid/s/^.*/\/bin\/hostid	$HOSTID/"  \
		/etc/rc.local > /tmp/admin.$$
	cp /tmp/admin.$$ /etc/rc.local
	/bin/rm -f /tmp/admin.$$
endif

#
# Now find out what kind of interface the guy has, and set up the ifconfig
# in /etc/rc.local and the standard alias in the host file.
#
				# ask what kind of controller
echo ""
get_control:
echo "What type of network controller does this machine have"
echo -n " (ex, il, none)? "
set IF = ($<)
switch ( $IF )
	case none:
	case ex:
		set IF = ex0
		breaksw
	case il:
		set IF = il0
		breaksw
	default:
		echo ""
		echo Illegal controller, please re-enter.
		goto get_control
		breaksw
endsw

if ($#OIFCONFIGLINE == 0) then
ex /etc/rc.local << EOF > /dev/null
/^[ \t]*\/bin\/hostid/a
/etc/ifconfig	$IF \`hostname\`.net
.
w
q
EOF
else
	if ($IF == ex0) then
	 sed -e '/^[ \t]*\/etc\/ifconfig.*ex0/s/^.*/\/etc\/ifconfig	ex0 \`hostname\`.net/' \
		/etc/rc.local > /tmp/admin.$$
	else
	 sed -e '/^[ \t]*\/etc\/ifconfig.*il0/s/^.*/\/etc\/ifconfig	il0 \`hostname\`.net/' \
		/etc/rc.local > /tmp/admin.$$
	endif
	cp /tmp/admin.$$ /etc/rc.local
	/bin/rm -f /tmp/admin.$$
endif

#
# Now ready to modify /etc/hosts
# The checks above on /etc/hosts ensure us that we can just add the entry
#

cp /etc/hosts /etc/hosts.orig
echo "saving /etc/hosts in /etc/hosts.orig"

ex /etc/hosts << EOF > /dev/null
/^.*\..*\..*\..*/
i
$ADDRS	$HOSTNAME $HOSTNAME.net
.
w
q
EOF
chmod 644 /etc/hosts

				# ask if they want to continue w/ set_nodes
c_loop:
echo ""
echo -n "Do you wish to add other entries to node address file? (y or n) "
set contin = ($<)
if ( ($contin != y) && ($contin != n)  ) then
    goto c_loop
endif
if ( $contin == y ) then
	set_nodes
endif

exit(0)
@
