Script one
The first script will be a simple aid in changing your network configuration. It will stop the NIC, open the file interfaces to be edited, and then restart the NIC after you have saved the interfaces file.
Create a file named netconfig.sh using your version of vi. Note that .sh is a de facto extension used for script files. The file will reside in your home directory, but since it is working with network files an id with root authority will be need to execute it.
Change to your home directory:
cd ~
Start vi and enter the following text (note: the # lines are comments):
#!/bin/bash
NETCONFIGFILE=/etc/network/interfaces
RESOLVECONF=/etc/resolv.conf
ifdown $1
vi $NETCONFIGFILE
#vi $RESOLVECONF
ifup $1
echo 'nameserver 172.16.1.245' >> $RESOLVECONF
echo 'nameserver 172.16.1.219' >> $RESOLVECONF
Save and exit vi. Make sure when you exit vi you have a file named netconfig.sh.
-
The first line tells the system which shell to run (bash in this case
-
The next two line are variables that are used for the fully qualified name of the interfaces and resolv.conf files
-
To use a variable in a script you put a $ in front of the name
-
The forth line stops the NIC card
-
$1 is the first (and only in this case) parameter passed to the script
-
The fifth line starts the vi editor to edit the interfaces file
-
The sixth line is commented out. It is to edit the resolv.conf file. To edit that file uncomment that line.
-
The next line restarts the NIC card
-
The last two lines add the IP address of two servers in the lab to the resolv.conf file
Use chmod to make the file netconfig.sh executable:
chmod 744 netcofig.sh
This will make the file readable, writeable and executable by your user id and only readable by those in the group and rest of the world. Note that although the file is now executable by your userid, it will fail the ifdown and ifup commands since only root can do those commands.
To run the script enter: ./netconfig.sh eth0
The script expects one parameter, the name of the NIC card, in this case eth0. Change eth0 if it is different on your system. That beginning period and slash is important. The ./ is the "command" (it really means to run the program from the current directory) to run the following script program netconfig.sh in the PWD and that program is not in your PATH. Remember the script you created has a command to start vi. The script will pause at the vi command until you close vi. When vi opens make the desired changes to the interfaces file. When done save and close. The script will then continue by starting the NIC card, and then appending the nameserver information string to the end of the resolv.conf file.
Confirm and document** that the script executed properly by checking the contents of the interfaces file and the resolv.conf file. You can make any changes you wish when editing the interfaces file. Hint: you might use this script when installing Apache (the next Step in this lab). Note that when the script is executed the first time the ifdown command may generate an error if the network has not been started yet.
You will probably want to keep this script (with minor modifications) to run in later labs when you need to edit your interfaces file.
|