Final exam exercises. July 6 2022. Time 6:00 p.m. – 10:00 p.m.

Number of exercises: 5. Each exercise has a maximum score.

In home directory of user hostadm, you need to create a new subdirectory, FINAL and a file within it, answers.txt, where you will be writing answers to the exam exercises.

In the file, first, put your name, then proceed with answering to the questions below.

All the exercises should be done on the VDI desktop.

1. Shell scripting and task scheduling (max score 4)

In directory FINAL on the desktop, create a shell script, fsuidroot.sh, with the following content

#!/bin/bash

DIR1='/home/hostadm/FINAL'
cp /dev/null $DIR1/pkgs.txt

for i in $(find /usr -type f -perm /u=s)
do
 echo $i
 pkg=$(dpkg -S $i)
 echo $pkg >> $DIR1/pkgs.txt 
done 

A) Run the script. What does the script do?

B) Schedule the script to run once on July 8 at 8 pm.

C) Schedule the script to run every Wednesday at 10:15 AM.


Answer

A)

chmod a+x fsuidroot.sh
./fsuidroot.sh

The script finds setuid and setgid executables under /usr, then identifies the packages that own them, and save their list in file pkgs.txt.

B)

at -f /home/hostadm/FINAL/fsuidroot.sh 8 pm July 8

C) In file /etc/crontab add the following line:

15 10  *  * 3   root   /home/hostadm/FINAL/fsuidroot.sh

2. systemctl and unit files (max score 4)

A) Create unit service file fsuidroot.service with rule start for the shell script in the previous exercise.

B) Add fsuidroot.service into system startup.


Answer

A) Create script /lib/systemd/system/fsuidroot.service with the following content:

fsuidroot.service

[Unit]
Description=fsuidroot startup

[Service]
Type=simple
ExecStart=/home/hostadm/FINAL/fsuidroot.sh

[Install]
WantedBy=default.target

B) Execute commands

systemctl daemon-reload
systemctl enable fsuidroot
systemctl status fsuidroot

3. Code compilation and Makefile (max score 6)


In directory FINAL, download a tar ball archive, containing four source codes in it, from http://linuxcourse.rutgers.edu/summer/html/lessons/application_compilation/downloads/sources_final.tgz.

A) In the same directory, create a Makefile with targets to compile each source code, and also targets ‘all’, and ‘clean’.

B) Run the commands below to make sure the Makefile works and explain what happens after each command:

make
make all
make clean

Disregard the warnings messages at compilation of del.c and substr.c


Answer:

A)

Makefile

del.x: del.c
        gcc -o del.x del.c

sclient.x: sclient.c
        gcc -o sclient.x sclient.c

sserver.x: sserver.c
        gcc -o sserver.x sserver.c

substr.x: substr.c
        gcc -o substr.x substr.c


all: del.x sclient.x sserver.x substr.x

clean:
        -rm -f *.o *.x

B)

make

Executable del.x has been built.

make all

Executables sclient.x, sserver.x, substr.x have been built.

make clean

All the executables and the object files, *.x and *.o, have been removed

4. Command grep and regular expressions (max score 6)

By using command grep and regular expression syntax, find all the lines in file sclient.c from the exercise above that contain

A) string #include

B) array symbols [ and ] with any alphanumeric data between them.

C) arithmetic <0 or < 0 expression.


Answer:

A)

grep '#include' sclient.c

B)

grep '\[[0-9]*[a-z]*\]' sclient.c

C)

egrep '(< 0|<0)' sclient.c

or

grep -E '<\s?0' sclient.c

5. Python scripting (max score 6)

By running jupyter notebook in directory FINAL, create a python3 script, that

A) creates nested directory final_exam/scripts

B) in the nested directory, final_exam/scripts, creates file exam.sh with the following content:

#!/bin/bash
cmd1='ls -l > exam.txt'
cmd2='grep hostadm /etc/passwd >> exam.txt'
eval $cmd1
eval $cmd2

C) makes exam.sh readable, writable, and executable, then runs it.

Hint: you need to use modules os and stat.

For the file permission settings, consule the link below:

os.chmod and permissions

D) On the desktop, verify that directory final_exam/scripts exists with files exam.sh and exam.txt in it.


Answer

A)

cd FINAL
jupyter notebook &
import os, stat
os.mkdir("final_exam")
os.chdir("final_exam")
os.mkdir("scripts")
os.chdir("scripts")

B)

string = """
#!/bin/bash

cmd1='ls -l > exam.txt'
cmd2='grep hostadm /etc/passwd >> exam.txt'
eval $cmd1
eval $cmd2
"""
f = open('exam.sh','w')
f.write(string)
f.close()

C)

os.chmod("exam.sh", stat.S_IRWXU)
os.system("./exam.sh")
0
ls final_exam/scripts

It should showe files exam.sh and exam.txt