M01: Introduction to Operating Systems
TU3: Installing specific purpose software
ASIX1
Pract. Exerc. 3: Introduction to shell scripts (2nd part)  30-04-20

Practical exercise 3: Introduction to shell scripts (2nd part)


GENERAL CONDITIONS
 1- Deadline: 28-05-2020.
2- Create a tar.gz file 
including every script.
3- Send a tar.gz file attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending who is your teacher.
     b) File Name:
          b.1) ASIX1: asix1_surname_name_m01tu03pr3.tar.gz
          b2.) DAW1: daw1_surname_name_m01tu03pr3.tar.gz

         Please note that "surname" means your real surname. Do not write "surname". Write your real surname . Additionally, "name" means your real name. Do not write "name". Write your real name.
       
Please note that you have to write asix1 if you are an asix1 student but you have to write daw1 if you are a daw1 student.
        Files sent with a wrong name will be
redirected to /dev/null
    c)
Subject:
        
c.1) ASIX1: asix1_surname_name_m01tu03pr3
         c.2)
DAW1: daw1_surname_name_m01tu03pr3

      
   Please note that "surname" means your real surname. Do not write "surname". Write your real surname . Additionally, "name" means your real name. Do not write "name". Write your real name.
       
Please note that you have to write asix1 if you are an asix1 student but you have to write daw1 if you are a daw1 student.
        Mails sent with a wrong subject will be zeroed with the help of dd and /dev/zero

4- Make your scripts individually.

PRACTICAL EXERCISES


1- Write a script called rocket2-1.0.sh that:
a) Creates a folder called rocket2 in your personal folder if this folder does not exist.
b)
Downloads the following source code:
http://www.collados.org/asix1/m01/tu3/rocket2.c
c)
Creates a  Makefile with
your personal information at the top and the following options:
    * Compile rocket2.c
    * Install rocket2 on your system in the /usr/local/bin/ directory
    * Uninstall rocket2
    * Clean temporary files
d)
Build a tarball package called rocket2-1.01.tar.gz with rocket2.c and Makefile.
e)
Your script must terminate returning an exit code equals to 0.


HELP 1: echo -e "\tHello World!!!"  prints a messatge with a TAB before Hello World!!
HELP 2: Special character > creates and add contents to  the new file
HELP 3:
Special character >> creates and add contents to  the new file.

2- Write a script called rocket2-1.1.sh that creates a .deb and a .rpm  package with the following features:
    * Maintainer: xxyyzz@inf1-xxyyzz where
where xx=two first letters of your name, yy=two first letters of your first surname and zz=two first letters of your second surname
    * Name: rocket2
    * Version: --> provides by the user
    * Release:
--> provides by the user
    * License: GPL
    * Group: Science
    * Provides: rocket2
Your script:
    a) will create automatically the .deb, and you do not need to interact with checkinstall.
    b) has to ask the user the package version and release.
    c) has to run checkinstall with the option to avoid the installation of  the deb package on your system.
    d) has to run checkinstall with the options to remove backup and description-pack upon termination.
    e) must
terminate returning an exit code equals to 0.

HELP: Read checkinstall man page and pay attention to the following options: -y,  -D, --pkgname, --pkgversion, --pkgrelease, --maintainer, --pkglicense, --pkggroup, --provides,
--install, --nodoc, --deldoc, --deldesc and --backup

3- Write a script called inst_rocket2_1.2.sh that:
a) Firstly, it checks if the script is being run by the root user. If it is not being run by the root user, it will terminate returning an exit code equal to 1 and showing: "This script must be run as root".  
b) Secondly, it shows a menu with the following options:                   
    1) Run rocket2-1.0.sh in order tor create Makefile
    2)
Run rocket2-1.1.sh in order tor create deb and rpm packages
    3) Install a rocket2 deb package  - (with the help of dpkg).  Your script
has to ask the user the package version and release.
        If the .deb package
can not be succesfully installed on your system, your script will display "Debian package not installed" and it will be terminated returning an exit code equal to 2.
    4) Remove  rocket2 - (with the help of dpkg).
    5) Show if rocket2 is installed or not - (running  dpkg -l rocket2). 
If you do not select a valid option then, your script must show the following message “Wrong option”.
c) Thirdly, the selected option is run.
d) Finally, your script asks the user if  he/she wants to start again from the beginning. If the user answers y, your script should clear screen and show again the menu. If the user does not answer y, your script should terminate returning an exit code equals to 0.

4- Write a script called backup_1.0.sh that:
a) Firstly, it checks if the script is being run by the root user. If it is not being run by the root user, it will terminate returning an exit code equal to 1 and showing: "This script must be run as root".  
b
) Creates a folder called /srv/backups if that folder does not exist.
c) Creates a folder called /srv/backups/home if that folder does not exist.
d)
Creates a folder called /srv/backups/etc if that folder does not exist.
e
) With the help of tar and gzip creates a backup of the /home directory. No warnings or any kind of message should be shown by the system when tar o gzip are run. Additionally,  the option -f should be used with gzip to force overwrite of files.  The backup filename has to be stored in /srv/backups/home and its name has to follow the next convention:  backup_home_DATE.tar.gz where DATE is the real date of the backup creation using the format YYYYMMDD where YYYY = year (4 digits), MM = month (2 digits) and DD = day of the month. For instance, a proper backup file name would be backup_home_20200514.tar.gz. If your script can not create the backup file, it will terminate returning an exit code equal to 2 and showing: "Error: A backup of /home has not been  created".  
f) With the help of tar and gzip creates a backup of the /etc directory. No warnings or any kind of message should be shown by the system when tar o gzip are run. The backup filename has to be stored in /srv/backups/etc and its name has to follow the next convention:  backup_etc_DATE.tar.gz where DATE is the real date of the backup creation using the format YYYYMMDD where YYYY = year (4 digits), MM = month (2 digits) and DD = day of the month. If your script can not create the backup file, it will terminate returning an exit code equal to 3 and showing: "Error: A backup of /etc has not been  created"
g) Your script must terminate returning an exit code equals to 0.

5- Write a script called crontab_1.0.sh that:
a) Firstly, it checks if the script is being run by the root user. If it is not being run by the root user, it will terminate returning an exit code equal to 1 and showing: "This script must be run as root".  
b
) Creates a folder called /srv/backups/crontabs if that folder does not exist.
c) With the help of  cp and gzip creates a backup of the /etc/crontab . No warnings or any kind of message should be shown by the system when gzip is run.
Additionally,  the option -f should be used with gzip to force overwrite of files. The backup filename has to be stored in /srv/backups/crontabs and its name has to follow the next convention:  backup_crontab_DATE.gz where DATE is the real date of the backup creation using the format YYYYMMDD where YYYY = year (4 digits), MM = month (2 digits) and DD = day of the month. If your script can not create the backup file, it will terminate returning an exit code equal to 2 and showing: "Error: A backup of crontab has not been  created".  
d) Asks the root user to provide a:
    * Minute
    * Hour
    * Day of month
    * Month
    * Day of week
    * Username
    * Command to be executed
e) With the information provided by root in the previous step,  a new scheduled job will be added to /etc/crontab.  
If your script can not create a new scheduled job, it will terminate returning an exit code equal to 3 and showing: "Error: A new scheduled job has not been  created".
f) Restarts the cron service 

g) Your script must terminate returning an exit code equals to 0.

6- Write a script called sshd_config_1.0.sh that:
a) Firstly, it checks if the script is being run by the root user. If it is not being run by the root user, it will terminate returning an exit code equal to 1 and showing: "This script must be run as root".  
b
) Creates a folder called /srv/backups/sshd_configs if that folder does not exist.
c) With the help of  cp and gzip creates a backup of the /etc/ssh/sshd_config . No warnings or any kind of message should be shown by the system when gzip is run.
Additionally,  the option -f should be used with gzip to force overwrite of files. The backup filename has to be stored in /srv/backups/sshd_configs and its name has to follow the next convention:  backup_sshd_config_DATE.gz where DATE is the real date of the backup creation using the format YYYYMMDD where YYYY = year (4 digits), MM = month (2 digits) and DD = day of the month. If your script can not create the backup file, it will terminate returning an exit code equal to 2 and showing: "Error: A backup of sshd_config has not been  created".  
d) Asks the root user to provide a new port number for the ssh service.
e) With the information provided by root in the previous step,  the port number of the ssh service will be changed. 
If your script can not change create a new scheduled job, it will terminate returning an exit code equal to 3 and showing: "Error: A new scheduled job has not been  created".
f) Restarts the ssh service. 

g) Your script must terminate returning an exit code equals to 0.
               

HELP 1: 
old_port=$(systemctl status ssh | grep 0.0.0.0 | cut -d " " -f 11 | tr -d ".")   --> Get the current port number and assigns the value to a variable called old_port
HELP 2:  sed  -i  "s/Port ${old_port}/Port ${new_port}/g" /etc/ssh/sshd_config --> Change Port number in /etc/ssh/sshd_config