[GLLUG] alias for host name?

Jeffrey Utter utterjef@zelda.cl.msu.edu
Tue, 16 Jul 2002 13:43:03 -0400 (EDT)


On Tue, 16 Jul 2002, Tom Rockwell wrote:

> Hi,
> 
> I have a laptop that gets a DHCP IP address when plugged in at MSU. 
> Along with the IP number, it also gets assigned a hostname.  While this 
> hostname is reasonable (pilotusername-N.user.msu.edu), I'd like to be 
> able to refer to the machine using a different name from other computers 
> I use.
> 
> So on my desktop, I'd like to be able to refer to the notebook with a 
> short name (say "laptop"...).  Is there a builting way of doing this, or 
> would it require some custom scripts?
> 

Like this one...  Grabs IP address, and adds an entry to the hosts
file.  You will have to come up with how you want to remove that entry
when your laptop is no longer at that location.  Another quick script
should be able to take care of that for you, or a quick hand edit of the
hosts file. 

(requires nslookup.. but could be replaced with other method of gaining
the ip address)
--------  Start Script --------------
#!/usr/bin/perl -w
#
#   quick and very dirty...
#
#   Usage:   alias_host offical_name alias_desired
#
#
#############################################

unless(@ARGV == 2){ print "Usage: alias_host official_name
alias_desired\n"; die;}


my $ip = `nslookup $ARGV[0] | grep Address: | tail -1 | sed
"s/Address:  //"`;
chomp($ip);
open(HOSTS, ">>/etc/hosts") or die "Cannot open /etc/hosts! \n\n"; 
print HOSTS "$ip        $ARGV[0] $ARGV[1]\n";
close HOSTS;

--------- End Script -----------------

It may take up to 5 mins for MSU DNS to register that the laptop is there
and then this method should work fine.