Lab4-Scripts
Lab4-Scripts
CMSC 240
Fall 2024 Due Tuesday, Nov. 18th 2024 by 11:59:59pm
In this lab, we're going to practice some of the bash shell scripting we’ve talked about.
Step 0. Setting Up
Let’s start by writing a script that will log in to some of the lab systems and run basic commands. In your Lab4
folder, create a file named “clients.txt” that contains the following IP addresses:
192.168.50.100
192.168.50.101
192.168.50.102
1. Copies any command line arguments given to the script into an environment variable named “COMMAND”.
Test your script by using it to run the command “df -h” on each system to see how much disk space it has free:
./loop.sh df -h
This works okay, but if we had more than two or three systems to manage, typing the password in each time
would be a problem. We can avoid this by using public keys to authenticate rather than passwords. To set this
up, you will need to create a public-key/private-key pair in your lab account.
cp -r ~/.ssh ~/.ssh-backup
If you already have SSH keys set up, we do not want to overwrite them.
At the top of “loop.sh”, add an if statement that checks whether the file /home/${USER}/.ssh/id_ed25519
exists. If not, it should use ssh-keygen to generate an ED25519 key pair with NO passphrase. By default, the
ssh-keygen command is interactive. It asks several questions. We instead want this to be entirely automated.
You can use the “yes” command to trick ssh-keygen into thinking we’ve pressed “enter” to take the default
value on most of these options, but the “new passphrase” option requires special options to ssh-keygen.
Inside the if statement, add a command that appends the contents of id_ed25519.pub to the file /home/$
{USER}/.ssh/authorized_keys (Be sure you append and do NOT overwrite this file)
The “last” command prints a list of the last ten or more users who have logged in to a system. This produces an
awful lot of output that can be difficult to parse.
Write a script that will search the logs for a particular username and print out the time that user last logged in.
Your script should be named “checklog.sh”. It should expect one command line argument (the username). It
should print out the date (and only the date) on which the user last logged in.
You should use the following Unix commands:
last
grep
cut
You may use other commands such as sed or tr if you wish, but you MUST use last, grep, and cut.
Write a script named “apache.sh” that takes a filename as an argument and uses the “sed” command to change
the word “httpd” to “www” in any line of the file that begins with the string “ServerRoot”. Make sure it only
makes the change if “ServerRoot” is right at the beginning of the line.
https://www.cs.longwood.edu/~robert/Lab4/hosts.txt
https://www.cs.longwood.edu/~robert/Lab4/photle.py
https://www.cs.longwood.edu/~robert/Lab4/presidents.tsv
https://www.cs.longwood.edu/~robert/Lab4/questions.txt
Submitting
Tar up your Lab4 directory and submit it through the course web site:
cd ..
tar czpvf Lab4.tar.gz Lab4/