0% found this document useful (0 votes)
22 views11 pages

CSC308 Spring25 Lab 1

Lab 1 for CSC308 focuses on operating systems, with an emphasis on using Ubuntu commands and Bash scripting. It includes evaluation criteria for submissions, guidelines for formatting and screenshots, and detailed instructions for tasks such as file management, email automation, and setting up cron jobs. The lab aims to enhance students' practical skills in using command-line interfaces and automating tasks through scripting.

Uploaded by

s647822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views11 pages

CSC308 Spring25 Lab 1

Lab 1 for CSC308 focuses on operating systems, with an emphasis on using Ubuntu commands and Bash scripting. It includes evaluation criteria for submissions, guidelines for formatting and screenshots, and detailed instructions for tasks such as file management, email automation, and setting up cron jobs. The lab aims to enhance students' practical skills in using command-line interfaces and automating tasks through scripting.

Uploaded by

s647822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab 1

CSC308: Operating Systems


Spring 2024-25

Objective and Overview Marks: out of 5


Grading Rules - rubric Individual
Submission
 Evaluation Criteria:
o Correctness and clarity:
 The answer is correct and has no factual mistakes.
 The answer is explained with clear language and is
supported by illustrating examples whenever
necessary.
o Accuracy of the results:
 Conducting proper experimental steps.
 Correct Explanation of the results
Part 1: 2.5 Marks
 Screenshots of the setup and results Part 2: 2.5 Marks
o Critical analysis
 Justifications and reasoning are logical and supported
by logical arguments.
 The student makes reflections from his/her own
opinion whenever necessary.
o Format
 The report is properly formatted using consistent font
size/style, consistent alignment, page numbers,
captions, and headers.
 Proper citation and referencing is applied wherever
appropriate.
Submission Guidelines
 Submit in Word or PDF format on Blackboard.
 All the screenshots included in the report must show your student ID, as shown in the
Lab Setup Section. Failure to do so will result in a penalty of one mark.
 Include the front page with All the details (Course description, student ID, name, etc.)
 Due on 22th of Saturday 2025 – late submissions will be penalized (5% for each day).
Reports will be scanned for similarity, and high similarity scores will be penalized.
Lab Part 1-Copy, Run, and Screenshot
In Ubuntu, login and launch the command prompt. The keyboard shortcut is <CTRL><ALT><T>. Or, you can
search for "Terminal" by clicking on the Dash button in the upper-left corner (i.e., the Ubuntu equivalent of the
Windows "start" button).

From the terminal and run the following command

>>PS1=”\d \@ [Your_Student-ID]”.
Use your own student ID. The prompt will change, as shown below.

All the screenshots from now onwards must show your studentID as shown above. Please make sure
to include your own screenshots for each step.

List files: ls
When you first login, your current working directory is your home directory. Your home directory has the same
name as your user-name, for example, vivek, and it is where your personal files and subdirectories are saved.
To find out what is in your home directory, use the ls command. (ls is short for "list") There may be no files
visible in your home directory, in which case, nothing will print but the command prompt again.
List contents of your current working directory:

1198888> ls

The ls command does not show hidden files by default. Hidden files have file names that begin with a dot (.).
To list all files in your home directory including hidden files, use the -a option with the ls command.
List all contents of your current working directory, including hidden files:

1198888> ls -a

Make directory: mkdir


To make a directory called ecpe170 inside your current working directory, type
1198888> mkdir csc308

To see the directory you have just created, type

1198888> ls

Change to a different directory: cd


The command cd directory means change the current working directory to 'directory'. The current working
directory may be thought of as the directory you are in, i.e. your current position in the filesystem. To change to
the directory you have just made, type
1198888> cd csc308
To go up one level in the directory tree, use two dots:

1198888> cd ..

At this point you should be back in your home directory.

Show the current directory: pwd


Pathnames enable you to work out where you are in relation to the whole file system. For example, to find out
the absolute pathname of your current directory, use:

1198888> pwd

The full pathname will look something like /home/jshafer which means that jshafer (your home directory)
is inside the directory home (the directory containing all user accounts).

Copy file: cp
Before doing these commands, enter the ecpe170 directory, and create two empty files using
the touch command:
1198888> cd csc308
1198888> touch file1
To make a copy of file1 and give it the name file2 (in the current working directory), use:

1198888> cp file1 file2

Now you should have two files: file1 and file2.

Note that you can use relative or absolute paths to identify files. For example, to copy file1 from the current
working directory and save it as the name file2 in one directory up from the current directory, use:

1198888> cp file1 ../file2

Move file: mv
To move (or rename) file1 into file2, use:

1198888> mv file1 file2

Delete file: rm
To delete ("remove") a file, use:

1198888> rm file2

Caution! The rm command is very powerful. Once you remove a file, recovering that file can be very difficult, if
not impossible. The command line does not provide the safety of "are you sure?" questions like the GUI
interface does. Further, no "unremove" or "undelete" command. If you're paranoid about deleting something
accidentally, use this instead:
1198888> rm -i file2
Delete ("remove") directory: rmdir
1198888> mkdir folder1
1198888> rmdir folder1
Note that the directory must be empty first!

Clear the terminal window: clear


1198888> clear

View a (text) file on screen: less


1198888> less /var/log/syslog

The command less writes the contents of syslog (located in the /var/log/ directory) onto the screen a
page at a time.
 Press the [space-bar] if you want to see another page

 Press the [arrow keys up/down] if you want to scroll

 Press [q] if you want to quit reading.

Read documentation on a command: man


1198888> man command-name

For example, use man ls to view the manual for the ls utility. These are colloquially referred to as "man
pages".
 Press [q] if you want to quit reading

Search document for a matching command: apropos


When you are not sure of the exact name of a command, use apropos to search the "man pages" for a
command matching a keyword. For example, to search for programs that have something to do with "sort", use:

1198888> apropos sort

Note that this is the same as using the "-k" option with man:

1198888> man -k sort

Run a command as the administrative ("root") user: sudo


Sudo allows you (a regular user) to run a command as an Administrative ("root") user by typing your password.
Everything that comes after sudo is the command to run as an Administrative user. Administrative access is
required to install software and read/move/delete files not owned by your current user.

1198888> sudo <<command-to-run-as-administrator>>


For example, the apt-get utility that we used to install software packages must be run as the root user.

Downloading Files Using wget


wget is a command-line tool used to download files from the web over HTTP, HTTPS, and FTP.

1198888> wget http://crl3.digicert.com/sha2-ha-server-g6.crl -O digicert.crl

This command downloads a certificate revocation list (CRL) from DigiCert's website and saves it as digicert.crl.
The -O option specifies the output filename.

Edit a text file: gedit


There are many text editors available on Linux/Unix systems.

The ancients - still present on modern computers, but "user friendly" was not a design requirement...

 ed - Written by Ken Thompson, co-creator of Unix


The classics - still widely used!

 vi - Written by Bill Joy, who later founded Sun Microsystems


 emacs - Written by Richard Stallman, who founded the GNU Project and Free Software Foundation
The newcomers:

 nano - Simple
 gedit - Simple and graphical!
 mousepad / leafpad - Simple and graphical!

I don't care what text editor you use. Just pick one and learn it well enough to become comfortable.
Gedit is easiest for novices due to its graphical design.
Tip 1: Turn on "Display Line Numbers" in Gedit preferences, so debugging programming errors will be
easier later this semester.
Tip 2: Disable "Create a backup copy of files" in Gedit preferences, so your directories won't be littered
with temporary files ending in "~" later on.

Run gedit to create an untitled document:

1198888> gedit

Run gedit to create the document myfile.txt

1198888> gedit myfile.txt

Place the following text in myfile.txt and save it (we'll use it next):

Neo: What are you trying to tell me? That I can dodge bullets?
Morpheus: No, Neo. I'm trying to tell you that when you're ready, you won't have to.

Count the number of characters, words, or lines in a file: wc


1198888> wc myfile.txt
What do the first three numbers in the output of the wc utility mean?
…………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………

Redirect input and output: < and >


Many programs take their input from "standard input" (i.e. the console keyboard) and display results to
"standard output" (i.e. the console monitor). However, you can change this, so either the input, output, or both
are files. This works even if the program was never written to expect a file!

Change the input to a program from standard input to a file: <


1198888> sort < myfile.txt

Change the output of a program from standard output to a file: >


1198888> man wc > docs_for_wc_program.txt
1198888> less docs_for_wc_program.txt
Change the output of a program from standard output to a file, appending to the end: >>
(You should have two copies of the docs in the file now!):

1198888> man wc >> docs_for_wc_program.txt


1198888> less docs_for_wc_program.txt

Lab Part 2 - Automating Log Collection and Email Reporting


Using Bash Scripts
Bash (Bourne Again Shell) is a command-line interface provided by the operating system (OS)
that allows users to interact with and control the system. It is one of the most common ways to
automate tasks, execute commands, and interact with files and system processes. Bash scripts are
simple text files containing a series of commands that are executed sequentially.

In this part, you will work with a Bash script that collects system logs, processes them, and sends
an email with the log summary. You will also configure a cron job to run the script periodically
(every 1 hour) without manual intervention.

Step 1: Introduction to Bash and Script Setup

1. Understanding Bash:
 Open your terminal.
 Bash is a powerful shell that provides users with an interface to interact with the OS. It
allows you to execute commands, automate tasks, and control system processes through
scripting.
2. Create a New Bash Script:
 Open a text editor such as nano:
nano logcollector.sh
 Paste the following sample Bash script inside the editor. This script collects system logs,
processes them, and sends a summarized email.
 Replace Your_email@gmail.com in the script with your actual Gmail email address. This
is the email address where the log summaries will be sent.
 After pasting the script, press Ctrl + X, then press Y to confirm saving the changes, and
press Enter to exit the editor.

#!/bin/bash
# Log file to collect logs from (modify as needed)
LOG_FILE="/var/log/syslog"
# Output file for summary
SUMMARY_FILE="/tmp/log_summary.txt"
# Email details
TO_EMAIL="Your_email@gmail.com"
SUBJECT="Hourly Log Summary"
# Collect logs from the last hour
echo "Log Summary for $(date)" > "$SUMMARY_FILE"
echo "------------------------------------" >> "$SUMMARY_FILE"
# Count occurrences of log levels
echo "INFO: $(grep -c 'INFO' $LOG_FILE)" >> "$SUMMARY_FILE"
echo "WARNING: $(grep -c 'WARNING' $LOG_FILE)" >> "$SUMMARY_FILE"
echo "ERROR: $(grep -c 'ERROR' $LOG_FILE)" >> "$SUMMARY_FILE"
# Include the last 20 lines of the log
echo -e "\nLast 20 log entries:\n" >> "$SUMMARY_FILE"
tail -n 20 "$LOG_FILE" >> "$SUMMARY_FILE"
# Send the email
mail -s "$SUBJECT" "$TO_EMAIL" < "$SUMMARY_FILE"

3. Make the Script Executable: After saving the script (Ctrl + X, then Y and Enter in nano), make
it executable using the following command:
sudo chmod +x logcollector.sh

Screenshots: Insert the screenshots of all tasks completed in Step 1

Insert your screenshots here

Step 2: Use and configure an External SMTP Server (e.g., Gmail)

SMTP (Simple Mail Transfer Protocol) is used to send emails from one server to another. In this
lab, we will configure your system to send log summaries via email using Gmail's SMTP server.
By using SMTP, you can automate the process of sending emails without needing to set up a
local mail server. This is ideal for sending automated notifications, like system log summaries,
directly to your inbox. Gmail's SMTP server will handle the email delivery securely, using
encryption (TLS/SSL) to protect the data.
In the lab, you’ll configure your system to send emails through Gmail using ssmtp (a lightweight
SMTP client), making it easy to integrate email notifications into your scripts.

1. Install ssmtp and mailutils:


 First, install the necessary packages for sending emails via an external SMTP server.
Open a terminal and run the following command to install ssmtp and mailutils:
sudo apt install ssmtp mailutils -y

2. Configure ssmtp for Gmail:


 Next, you'll need to edit the ssmtp.conf file to set up Gmail’s SMTP server. Open the
configuration file:
sudo nano /etc/ssmtp/ssmtp.conf

 Add the following lines to the configuration file, replacing the placeholders with your
actual Gmail credentials:
root=your_email@gmail.com
mailhub=smtp.gmail.com:587
AuthUser=your_email@gmail.com
AuthPass=your_app_password
FromLineOverride=YES
UseSTARTTLS=YES

o root: Your Gmail email address that will be used to send the email.
o mailhub: Gmail's SMTP server address and port (smtp.gmail.com:587 for TLS).
o AuthUser: Your Gmail email address again.
o AuthPass: Your App Password (more on this below).
o FromLineOverride: Allows setting a custom "From" address in outgoing emails.
o UseSTARTTLS: Uses STARTTLS for email transmission security.

3. Generate an App Password:


Important: Gmail now requires the use of an App Password instead of your normal Gmail
password for third-party applications (like sending emails through a script).
To generate an App Password:
 Go to Google Account Settings.
 Under Security, find App Passwords.
 Select Mail as the app and Other for the device, then generate the password.
 Name the app password as LogCollectorXYZ, where X, Y, and Z are the first, third, and
fifth digits of your student ID.
 Copy the generated App Password and replace your_app_password in the ssmtp.conf
configuration with it.

For more details about how to generate an App Password, watch this video:
https://www.youtube.com/watch?v=N_J3HCATA1c
4. Once you’ve configured ssmtp, you can test the email setup by sending a simple test email or
running the script (logcollector.sh) to check the full functionality.
 Send a test email to verify that your email configuration is working. Run the following
command, replacing your_email@gmail.com with your Gmail address:
echo "Hello Sir" | ssmtp your_emai@gmail.com

If the email is received successfully, the setup is correct.

 Run the Bash script (logcollector.sh) to test the full log collection and email functionality.
Use the following command:

sh logcollector.sh

Screenshots: Insert the screenshots of all tasks completed in Step 2

Insert your screenshots here

Step 3: Set Up a Cron Job

In this lab, we use a cron job to automate the execution of the log collection script
(logcollector.sh) at regular intervals, ensuring that the process runs without requiring manual
intervention.

Cron is a time-based job scheduler in Unix-like operating systems, such as Ubuntu, and is ideal
for automating tasks like:

 Running scripts periodically (e.g., every hour, day, etc.).


 Reducing the need for manual monitoring or intervention.
 Ensuring that system logs are regularly collected and sent via email without forgetting or
skipping any important tasks.

By scheduling the script to run at specific intervals (e.g., every hour or every day), the cron job
ensures that your system logs are consistently gathered, summarized, and emailed as part of
regular system maintenance, improving efficiency and reliability.

1. Edit the Cron Table:

crontab -e

2. Add a New Cron Job: Add the following line to run the script every 1 hour:
0 * * * * /path/to/logcollector.sh

Replace /path/to/logcollector.sh with the actual path where your script is located.

o 0: Run at minute 0.
o *: Run every hour.
o *: Every day of the month.
o *: Every month.
o *: Every day of the week.

3. Save and Exit the Cron Table:


In nano, save and exit the editor by pressing Ctrl + X, then Y and Enter.
4. To verify that the cron job was added successfully, you can list the current cron jobs by
typing:
crontab -l

5. Now that the cron job is set up, the script will automatically run every hour. To monitor
its execution. You can check cron logs to confirm that the cron job is running correctly
by looking at the cron logs:
grep CRON /var/log/syslog

6. Demonstrate that the script is working as expected by showing two emails received
within a two-hour period, as shown below.

Screenshots: Insert the screenshots of all tasks completed in Step 3

Insert your screenshots here


Important!

Once you have finalized the lab and no longer need the cron job running, it is important to
deactivate it to avoid receiving logs continuously. Follow these steps:
1. Edit the cron job configuration: Open the crontab file by running the following command:

crontab -e

2. Comment out the cron job: In the crontab file, add a # at the beginning of the line that
schedules the log collection script. It should look like this:

# 0 * * * * /path/to/logcollector.sh

By commenting out the line, you will prevent the cron job from running and stop the log summaries from
being sent.

3. Save and exit the crontab: Press CTRL+X, then Y to save and exit the file.

This action will stop the cron job from sending log summaries every hour. It is crucial to do this
to avoid receiving continuous logs. If needed, you can reactivate it later by simply removing the
# and saving the file again.

You might also like