Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 16 Mar 1999 18:26:51 -0500 (EST)
From:      "Stan Brown" <stanb@awod.com>
To:        freebsd-mobile@freebsd.org (FreeBSD Mobile List)
Subject:   Proof of concept
Message-ID:  <19990317000254.4EE5D1501F@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help
	I am sendng this to the list in the hope that it will be usefuel to
	others. It's really just at the proof of concept stafe, and could I am
	ceratin benfit from _lots_ of improvements.

	Do you need your laptop to configure for multiple networks? Well this
	is for you. I based this on an original script that I got from somebody
	(I think on this list) about a year or so ago. I have tweaked it a bit,
	and have tested this version with 3.0 and 3.1

	I am submiting it in the hope that soenthing like this, clenaed up and
	sanatized, might make it inot the mainstream codebas.

	Anyway here it is. I think the comments explain how to use it pretty
	well. Warning once you think you have it ocrrect. TEST IT from the
	command line before puting it in to be called from the rc scripts.

	Here is the main script:


#!/usr/bin/perl

# Set up as many paramter sets as desired
# Primary key is the IP address that I should use
# on the given network.
#
# This general format could be extended to allow 
# for setting anything that would normally
# be ste in /etc/rc.conf,
#

# This really should be read from a configuration file
%parm_set = ('205.159.77.227 ' =>	{
									gw	=> '205.159.77.234',
									dns	=> ['205.159.77.240',
											'208.140.99.9',
											'198.81.225.1'],
									nm  => '0xffffff00 ',
									hostname => 'brown' ,
									dom => 'fas.com' ,
									nis_dm => 'beach' ,
									nis_clnt => 'YES' ,
									ypset => 'NO' ,
									nfs_srvr => 'YES' ,
									nfs_clnt => 'YES' ,
									amd_enbl => 'YES' ,
									set_default => 'YES' 
								},
			'170.85.106.146 ' =>	{
									gw	=> '170.85.106.144',
									dns	=> ['205.159.77.240',
											'208.140.99.9',
											'198.81.225.1'],
									nm  => '0xffffff80 ',
									hostname => 'brown' ,
									dom	=> 'westvaco.com',
									nis_dm => '' ,
									nis_clnt => 'NO' ,
									ypset => 'NO' ,
									nfs_srvr => 'NO' ,
									nfs_clnt => 'NO' ,
									amd_enbl => 'NO' ,
									set_default => 'NO' 
								},
			'170.85.109.104 ' =>	{
									gw	=> '170.85.109.1',
									dns	=> ['170.85.11.12',
											'205.159.77.240',
											'208.140.99.9',
											'198.81.225.1'],
									nm  => '0xffffff80 ',
									hostname => 'brown' ,
									dom	=> 'westvaco.com',
									nis_dm => '' ,
									nis_clnt => 'NO' ,
									ypset => 'NO' ,
									nfs_srvr => 'NO' ,
									nfs_clnt => 'NO' ,
									amd_enbl => 'NO' ,
									set_default => 'YES' 
								},
			'170.85.113.100 ' =>	{
									gw	=> '170.85.113.1',
									dns	=> ['170.85.11.12',
											'205.159.77.240',
											'208.140.99.9',
											'198.81.225.1'],
									nm  => '0xffffff80 ',
									hostname => 'brown' ,
									dom	=> 'westvaco.com',
									nis_dm => '' ,
									nis_clnt => 'NO' ,
									ypset => 'NO' ,
									nfs_srvr => 'NO' ,
									nfs_clnt => 'NO' ,
									amd_enbl => 'NO' ,
									set_default => 'YES' 
								},
			'24.6.61.166 ' =>	{
									gw	=> '24.6.61.1',
									dns	=> ['24.1.32.33',
											'24.1.32.34'],
									nm  => '0xffffff00 ',
									hostname => 'brown' ,
									dom	=> 'mtpls1.sc.home.com',
									nis_dm => '' ,
									nis_clnt => 'NO' ,
									ypset => 'NO' ,
									nfs_srvr => 'NO' ,
									nfs_clnt => 'NO' ,
									amd_enbl => 'NO' ,
									set_default => 'YES' 
								},
);


`route delete default 2>&1 > /dev/null`;
$up = 0;
$device="ed0";	# set to the device for your pccard NIC

foreach $key (keys(%parm_set))
{
	$thiskey = $key;
	foreach $cable ("link1", "link2")
	{
		if($cable eq "link1")
		{
			$c2 = 'link2';
			print "Trying $key on 10baset\n"
		}
		else
		{
			$c2 = 'link1';
			print "That did not work, Trying on 10base2\n"
		}

		# the interface names changed from link0 = 10baseT link1 = 10base2
		# to link0 = AUI link1 = 10baseT link2 = 10base2
		# at the 2.x to 3.x change
		# print "ifconfig $device inet $key netmask $parm_set{$key}{nm} -$c2 $cable\n";
		`ifconfig $device down`;
		`ifconfig $device inet $key netmask $parm_set{$key}{nm} -$c2 $cable`;
		`ifconfig $device up`;
		unless (system("ping -c 1 $parm_set{$key}{gw} > /dev/null"))
		{
			$up = 1;
			last;
		}
	}
	if($up == 1)
	{
		last;
	}
	print "Cannot ping $parm_set{$key}{gw}\n";
}

if($up)
{
	print "Checking  $thiskey\n";
	if($parm_set{$thiskey}{set_default} eq "YES" )
	{
		`route add default $parm_set{$thiskey}{gw}`;
	}

	 open RESOLVFILE, "> /etc/resolv.conf";
	 print RESOLVFILE "domain $parm_set{$thiskey}{dom}\n";
	 foreach $ipnum (@{$parm_set{$thiskey}{dns}})
	 {
 		print RESOLVFILE "nameserver $ipnum\n";
	 }
	 close RESOLVFILE;

	 open MOD_PARMS_FILE, "> /tmp/mod_parms";
	 print MOD_PARMS_FILE "hostname=\"$parm_set{$thiskey}{hostname}\.$parm_set{$thiskey}{dom}\"\n";
	 print MOD_PARMS_FILE "nisdomainname=\"$parm_set{$thiskey}{nis_dm}\"\n";
	 print MOD_PARMS_FILE "nis_client_enable=\"$parm_set{$thiskey}{nis_clnt}\"\n";
	 print MOD_PARMS_FILE "nis_ypset_enable=\"$parm_set{$thiskey}{ypset}\"\n";
	 print MOD_PARMS_FILE "nfs_server_enable=\"$parm_set{$thiskey}{nfs_srvr}\"\n";
	 print MOD_PARMS_FILE "nfs_client_enable=\"$parm_set{$thiskey}{nfs_clnt}\"\n";
	 print MOD_PARMS_FILE "amd_enable=\"$parm_set{$thiskey}{amd_enbl}\"\n";
	if($parm_set{$thiskey}{set_default} eq "YES" )
	{
	 	print MOD_PARMS_FILE "defaultrouter=\"$parm_set{$thiskey}{gw}\"\n";
	}
	else
	{
	 	print MOD_PARMS_FILE "defaultrouter=\"NO\"\n";
	}
	 close MOD_PARMS_FILE;

	# All the stuff here should realy only be run if we are
	# changing, an already configured system, not bringing
	# one up from scratch via rc files.
	#
	# One possible way to handle this would be to check to
	# se what runlevel we are n at this point.
	# If we do this, we can also suppress generation of
	# /tmp/mod_parms if this is a _change_
	if($parm_set{$thiskey}{nis_clnt} eq "NO" )
	{
		print "No NIS server on this net\n";
		`/usr/bin/killall ypbind`;
	}
	if($parm_set{$thiskey}{amd_enbl} eq "NO" )
	{
		print "Do not need to run amd on this net\n";
		`/usr/bin/killall amd`;
	}
	`hostname $parm_set{$thiskey}{hostname}.$parm_set{$thiskey}{dom}`;
	# end of this to be skiped if runing from an rc file

	print "Configured for network $parm_set{$thiskey}{dom}\n";

	# You can use this as examples of things you wnat to
	# do loacally based upon which networj you find
	# yourself on
	if($parm_set{$thiskey}{dom} eq "fas.com")
	{
		`/usr/local/bin/say "I feel right at home here"`;
	}
	if($thiskey eq "170.85.106.146 ")
	{
		`/usr/local/bin/say "I must be in the powerhouse"`;
	}
	if($thiskey eq "170.85.109.104 ")
	{
		`/usr/local/bin/say "I must be in the office building"`;
	}
	if($thiskey eq "170.85.113.100 ")
	{
		`/usr/local/bin/say "I must be out in the mill"`;
	}
}
else
{
	print "Cannot find a known network\n";
	# begining of things to do if changing networks
	`/usr/bin/killall ypbind`;
	`/usr/bin/killall amd`;
	`ifconfig $device down `;
	# end of things to do if changing networks

	# begining of things to do if runing from rc file
	 open MOD_PARMS_FILE, "> /tmp/mod_parms";
	 print MOD_PARMS_FILE "hostname=\"brown\"\n";
	 print MOD_PARMS_FILE "nisdomainname=\"\"\n";
	 print MOD_PARMS_FILE "nis_client_enable=\"NO\"\n";
	 print MOD_PARMS_FILE "nis_ypset_enable=\"NO\"\n";
	 print MOD_PARMS_FILE "nfs_server_enable=\"NO\"\n";
	 print MOD_PARMS_FILE "nfs_client_enable=\"NO\"\n";
	 print MOD_PARMS_FILE "amd_enable=\"NO\"\n";
	 close MOD_PARMS_FILE;
	# end of things to do if runing from rc file
}
`touch /tmp/multi_net_complete`;

	Edit this to suit, and put it in anyplace you like. Then add a call to
	this script from the "insert" action of your NIC in occardd.conf.
	Finaly modify the section of /etc/rc that calls pccardd to look like
	this.



# start up PC-card configuration
if [ -f /etc/rc.pccard ]; then
	rm -f /tmp/multi_net_complete
	. /etc/rc.pccard
	# Added 3/14/99 SDB to allow for pccardd, and it's chlidren
	# to complete, before we try to run network daemons
	until [ -f /tmp/multi_net_complete ]
	do
		sleep 1
	done
	# seting up for a given network may change
	# some paramters that are set in /etc/rc.conf
	# Since this is done by a perl script run as
	# a child of pccardd, we pass these parmaters back
	# here from a file created for this purpose, since
	# there is no way to for this chiled to modify our
	# environment variables
	if [ -f /tmp/mod_parms ]; then
		. /tmp/mod_parms
		rm -f /tmp/mod_parms
	fi
	rm -f /tmp/multi_net_complete
fi

	Works OK for me. really out to read from a configuration file rather
	than be hard coded, and there are some issues related to doing too much
	work, so that it cam be called from the rc scripts or direct from a
	runing systems, but it proves this can be done.

	Cheers.


-- 
Stan Brown     stanb@netcom.com                                    843-745-3154
Westvaco
Charleston SC.
-- 
Windows 98: n.
	useless extension to a minor patch release for 32-bit extensions and
	a graphical shell for a 16-bit patch to an 8-bit operating system
	originally coded for a 4-bit microprocessor, written by a 2-bit 
	company that can't stand for 1 bit of competition.
-
(c) 1999 Stan Brown.  Redistribution via the Microsoft Network is prohibited.


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-mobile" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990317000254.4EE5D1501F>