Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lab4-Scripts

Uploaded by

coucou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab4-Scripts

Uploaded by

coucou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab 4 – Scripting

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

First create a folder named Lab4.

Step 1. Writing a configuration management script

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

Then write a bash script named “loop.sh” that:

1. Copies any command line arguments given to the script into an environment variable named “COMMAND”.

2. Loops through the IP addresses in “clients.txt”.

3. Prints the IP address of each client to the screen.

4. Uses “ssh” to run “COMMAND” on that client.

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

Step 2. Password-less SSH

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.

Make a backup copy of your .ssh folder:

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)

Step 3. User Logging

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.

Step 4. Configuration Management

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.

Step 5. Using text editing commands

Use wget to download the following files:

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

Use vim to edit questions.txt and answer all twelve questions.

Submitting
Tar up your Lab4 directory and submit it through the course web site:

cd ..
tar czpvf Lab4.tar.gz Lab4/

You might also like