Changes

Jump to: navigation, search

OPS245 Lab 3

2,794 bytes removed, 9 March
no edit summary
{{Admon/caution|DO NOT USE THIS VERSION OF THE COURSE. This page will no longer be updated.|'''Debian version here:''' https://seneca-ictoer.github.io/OPS245
<br>'''CentOS version here:''' https://seneca-ictoer.github.io/OPS245-C7<br>'''Andrew's version here:''' http://wiki.littlesvr.ca/wiki/OPS245_Lab_3}}
= LAB PREPARATION =
{| width="40%" align="right" cellpadding="10"
|colspan="3" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:20px;"|Linux Command Reference<br>
H
|- valign="top"
===Part 1: Common Booting Issues===
There are a few "classic problems" that students can encounter with their virtual machines and their host machine after performing lab2. Although all OPS235 OPS245 students may not encounter these problems, it is good to be aware of what a potential problem may look like, and know how to solve that problem.
'''A few common problems are:'''
[[Image:grub2_1.png|thumb|right|600px|Add the boot option '''single''' and then press '''ctrl-x''' to boot into single user mode]]
|}
 
===Part 2: Booting into Single-User Mode===
If you cannot log graphically into your machine (first two common problems), there is a trick to boot your system into '''single-user''' mode. This mode does not provide networking, graphics, or logging in as other regular users, but can connect to a text-based interface as the '''root''' user. This method will only work if a GRUB (<u>'''Gr'''</u>and '''<u>U</u>'''nified <u>'''B'''</u>oot-loader) password has not been set, or that you haven't forgotten the GRUB password.
#Select Open Virtual Machine, and select the VM called c7host on your Solid State Drive.
#Launch your '''c7host''' machine, and login as as a regular user.
#Boot-up your '''centos1''' VM. '''when When the Grub Boot menu appears''', press the letter <b><code><span style="color:#3366CC;font-size:1.2em;">e</span></code></b> (for "edit").
#Using your arrow keys, scroll to next screen to '''linux''', or '''linux16''', or '''linux-efi''' command and type the word <b><code><span style="color:#3366CC;font-size:1.2em;">single</span></code></b> as an argument after '''quiet''' (see diagram for reference) and then press <b><code><span style="color:#3366CC;font-size:1.2em;">ctrl-x</span></code></b> to boot.
#The system should boot into text-based mode. Enter your '''root''' password.
[[Image:grub2_3.png|thumb|right|600px|Add '''rw init=/sysroot/bin/sh''' as shown and then press '''ctrl-x''' to boot into single user no root password mode]]
|}
 
===Part 3: Resetting Forgotten Root Password===
#The system should boot into text-based mode without prompting for root's password.
#Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">chroot /sysroot</span></code></b><br>
#Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">passwd root</span></code></b> in order to change your root password (press '''ctrl-c''' if you wish to abort - i.e. not change password).<br>'''NOTE:''' if you are using SELinux in '''enforcing''' or '''permissive''' mode, you will also need to issue the command: '''touch /.autorelabel'''. <br>This may take some time depending on the amount of files you have on the file system (usually about 2 minutes).
#To restart in graphical mode, simply enter the command <b><code><span style="color:#3366CC;font-size:1.2em;">exit</span></code></b>, then <b><code><span style="color:#3366CC;font-size:1.2em;">reboot</span></code></b>.
# Issue the command: <b><code><span style="color:#3366CC;font-size:1.2em;">which lbreakout2</span></code></b><br>Is there a program called '''lbreakout2''' on your system?
# Perform an Internet search for the pattern: <b><code><span style="color:#3366CC;font-size:1.2em;">lbreakout2 tar.gz</span></code></b>
# Go to an appropriate webpage and download a "zipped tarball" (compressed source code) for the '''lbreakout2''' game (filename should be something like: '''lbreakout2-2.6.5.tar.gz''')
#Change to the directory where you downloaded that file (most likely ''~/Download''s).
# Use the '''tar''' command to decompress the "zipped tarball" called something like (not may be exactly): '''lbreakout2-2.6.5.tar.gz'''.
# Change to the directory that contains that ''lbreakout2'' source code.
{| width="40%" align="right" cellpadding="10"
<li value="8">Issue the following command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">./configure && make</span></code></b><br>Most likely, you will get an '''ERROR!''' What do you think the error is telling you?<br></li>
<li>Issue the following command to install the files for application development by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo yum groupinstall "Development Tools"</span></code></b></li>
<li>Issue the command in step 78. You should get another ERROR! What dependency is missing?</li><li>Issue the following command to install the library files for SQL by issuing the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo yum install SDL-devel</span></code></b> (possibly <b><code><span style="color:#3366CC;font-size:1.2em;">sudo yum install libpng-devel</span></code></b> and <b><code><span style="color:#3366CC;font-size:1.2em;">sudo yum install zlib-devel</span></code></b> )</li>
<li>Reissue the <b><code><span style="color:#3366CC;font-size:1.2em;">./configure && make</span></code></b> command. Were you successful?</li>
<li>Run the command <b><code><span style="color:#3366CC;font-size:1.2em;">lbreakout2</span></code></b>. Were you successful?</li>
<li>You need to run a command to make this program available regardless of your current directory (as opposed to running command in the directory that contains the program). Login as root (be sure to be in the same directory as the source code after logging in as root) and So, issue the command:<br><b><code><span style="color:#3366CC;font-size:1.2em;">sudo make install</span></code></b></li>
<li>Run the command: <b><code><span style="color:#3366CC;font-size:1.2em;">lbreakout2</span></code></b>. Did it work? Issue the command:<br> <b><code><span style="color:#3366CC;font-size:1.2em;">which lbreakout2</span></code></b>. What do you think that the '''make install''' command did?</li>
</ol>
|- valign="top"
|
{{Admon/tip|Bash Shell Scripting Tips:|<br>'''<u>The Here Document</u>'''<br><ul><li>A neat little trick involving a special type of redirection of stdin symbol: '''&lt;&lt;''' that allows input to be redirected to a command from within the command.<br><br> </li><li>Examples:<br><br>''cat &lt;&lt;+<br>This is a test message<br>This is the second line<br>+''<br><br>''mail -s "test message" youremailaddr &lt;&lt;+<br>This is a test message<br>I hope you like it.''<br>+<br><br>''tr [a-z] [A-Z] &lt;&lt;+<br>i like ops235<br>i love scripting.<br>+''<br><br></li></ul>'''<u>Using sed to Manipulate Text</u>'''<ul><li>The Linux command '''sed''' stands for <u>'''S'''</u>treaming <u>'''Ed'''</u>itor which is an effective way to manipulate a text file, output sent from a command, or from within a "here document". This command can manipulate matching text on a variety of criteria (such as '''line number(s)''', '''regular expression match''', etc). Commands can then be used for manipulation such as '''omitting''', '''printing''', '''substituting''', '''adding''', and '''inserting''' text.<br><br></li><li>The sed option '''-n''' suppresses display of text so the print ('''p''') command can be used; otherwise, the text will be displayed (with edits via the sed command instructions).<br><br></li><li>Results of text manipulation with sed can be stored in a variable using command substitution, or redirected to a file. '''NEVER redirect the stdout from a sed command to the same input file (or the input file will be destroyed)!''' <br><br></li><li>Examples<br><br>''sed 's/&#124;/ /g' &lt;&lt;+<br>I&#124;like&#124;weekends!<br>+''<br><br>''sed 's/$/\n/g' &lt;&lt;+<br>This text<br>should be<br>double-spaced!''<br>+<br><br></li></ul>}}
|}
===Part 3: Generating Sofware Package Information with Shell ScriptsUsing sed to Filter Output from Commands===We will continue with using shell scripts commands to create a Software Information Report that manipulates output generated by the '''rpm''' command. The '''sed''' and '''awk''' commands are very useful tools in shell scripting to manipulate text. In this lab, we will be using ''sed'' to allow the user to select certain portions from the rpm command (options -qi).
'''Perform the following steps:'''
<ol>
<li> Use your '''c7host''' VM for this section.</li>
<li>Open a Bash terminal and run the command:
# Use your '''c7host''' VM for this section.
# Open a Bash shell terminal and use '''sudo -i''' to switch to a root session.
# Use a text editor (such as <b><code><span style="color:#3366CC;font-size:1.2em;">vi</span></code></b> or <b><code><span style="color:#3366CC;font-size:1.2em;">nano</span></code></b>) to create a Bash Shell script called: <b><code><span style="color:#3366CC;font-size:1.2em;">packageInfo.bash</span></code></b> in the '''/root/bin''' directory.
# Enter the following text content into your text-editing session:
<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">
<br>
&#35;!/bin/bash <br>
<br>
&#35; packageInfo.bash<br>
&#35; Purpose: Generates a report to displaying specified information of installed software<br>
&#35;<br>&#35; USAGE: ./packageInfo.bash [application-name]<br>
&#35;<br>
&#35; Author: *** INSERT YOUR NAME ***<br>
&#35; Date: *** CURRENT DATE ***<br>
<br>
if [ $(whoami) != "root" ] # only runs if root<br>
then<br>&nbsp;echo "You must be logged in as root." >&2<br>
&nbsp;exit 1<br>
fi<br>
</code>
<br>
<ol><li value="4">Save your editing session, but remain in the text editor.</li><li>The code displayed below will require the user to include only one argument after the command (script) which will be the application name. The following code will also generate the report title and current date. Add the following code</li></ol>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;font-weight:bold;">
 
if [ $# -ne 1 ]<br>
then<br>
&nbsp; echo "Your command must have a application-name as argument" >&2<br>
&nbsp; echo "USAGE: $0 [application-name]" >&2<br>
&nbsp; exit 1<br>
fi<br>
<br>
&#35; Create report title (echo with -e option allows newline \n character to be used)<br>
echo -e "\nSOFTWARE PACKAGE INFORMATION REPORT" > /root/package-info.txt <br>
echo -e "Date: $(date +'%A %B %d, %Y (%H:%M:%p)')\n\n " >> /root/package-info.txt<br>
 
</code>
<ol><li value="6">Save your editing session, but remain in the text editor.</li><li>The code displayed below uses a trick called the "Here Document" to redirect stdin from within the command (a quick way to display output on the screen). The read command will store the different information report items as words (separated by a space). The sed command used to convert the spaces to pipes (|) and stored into another variable. This allows the sed command to use extended regular expressions to print rpm elements that match those various patterns to be added to the report. Add the following code</li></ol>
<br>
<code style="color:#3366CC;font-family:courier;font-size:.9em;font-weight:bold;">
&#35; Clear screen and use Here Document to display select on report items to read into variable<br>
clear<br>
cat &lt;&lt;+<br>
Available Package Information Items:<br>
<br>
Name<br>
Summary<br>
Version<br>
License<br>
Source<br>
URL<br>
+<br>
read -p "Enter word(s) shown above separated by spaces: " choice<br>
<br>
&#35; Convert spaces to pipe symbol (|)<br>
processedChoice=$(echo $choice | sed 's/ /|/g')<br>
<br>
<code><span style="color:#3366CC;font-size:1.1em;"><b>
&#35; Use sed with extended regular expressions to only print those matching report elements<br>
rpm -qi $1 | sed -r -n "xchat</^($processedChoice)b></ p" &gt;&gt; span></rootcode><br /package>Notice that using the '''-i''' option with '''-infoq''' causes rpm to generate more output than we have seen from it so far.txt<br/li><brli>cat &lt;&lt;+Pick any five packages and run the <brcode>File <span style="/root/packagecolor:#3366CC;font-infosize:1.txt1em;" has been created><brb>rpm -qi</b>+<br/span></code>command on all five at once. This should produce dozens of lines of output. Consider the output it would generate if we used -a instead of picking just a few packages. <olli>Run <code><li valuespan style="8color:#3366CC;font-size:1.1em;">Save, set permissions, and then run that shell script for the application '''gedit'''. Did it create that report? Try running the script without an argument - What did it do? <lib>Use the rpm -qia</b><code/span>wget</code>now.</bli><li> command That is way too much output for us to downloadreasonably search through, study, and run but we have commands that can filter this down to a much more readable amount.</li><li>Try the following shell scripts on-linecommand:<blockquote><bbr><code><span style=" pointer-events:none;cursor:default;color:#3366CC;font-size:1.2em1em;">https<b>rpm -qia | sed -nre '/^Name&#91;&#91;:space:&#93;&#93;+:/p' -e '/ict.senecacollege.ca^Install Date&#91;&#91;:space:&#93;&#93;*:/~ops235p'</labs/packageInfoGraphical.bashb></span></code><br /b></blockquote>Instead of printing every line, this will only display the name and install date of each package.</li><li>Try modifying that command to understand what this Bash Shell script doesdisplay only the package Name and License.</li></ol>
'''Answer the Investigation 3 observations / questions in your lab log book.'''
=INVESTIGATION 4: USING LOGICAL STRUCTURES IN PYTHON=
Now that we have added some powerful logical structures to our python scripts, we can make our scripts much more adaptable. Just like we have done in bash, our Our python scripts can now responde respond to different situations by executing different code, or repeating the same code several times (e.g. keep asking the user for input until they give us something useful). In this investigation you will write a python script that will prompt a user for a directory to archive, ask them if they want the archive compressed, and (only if they do want compression) what type of compression to apply.
<ol>
<li>Add a third prompt (immediately after the other two prompts and before the tar command), asking the user if they want the archive to be compressed. You'll need an if statement to run your tar command with gzip compression if they answered yes.</li>
<li>Test your script to make sure it works.</li>
<li>Add a fourth prompt asking the user what type of compression they want (present them with gzip, bzip2, and xz xzip as options). Note that this prompt should only be shown if the user opted for compression (if they don't want compression, there is no point asking what type they don't want).</li>
<li>Expand the if statement around your actual tar command to use whichever type of compression the user asked for.</li>
<li>Test your script to make sure it works.</li>
<li>Test your script again, but feed it nonsense data (e.g. answer 'very much so' instead of 'y' or 'n' when prompted about compression).</li>
<li>Add loops around your prompts for whether or not the user wants compression, and the compression type to make the script repeat each prompt until the user gives a response your script can actually use. Don't worry about doing anything with the archive name or path.</li>
<li>Now test your script again, with good data and with nonsense.</li>
<li>When you are confident your script works, you are ready to submit the lab.</li>
# Make certain that your '''c7host''' and '''centos3''' VMs are running.
# Switch to your <u>'''c7host'''</u> machine, open a shell terminalas a regular user, and use change to the directory: '''~/bin'''# Download the Lab 3 check script: <b><code><span style="color:#3366CC;font-size:1.2em;">sudo wget https://raw.githubusercontent.com/OPS245/labs/main/lab3-icheck.bash</span></code></b> to switch to a root session.# Change to the directory: <!--<br>For Andrew'''/root/bin'''# Issue the Linux commands sections use this script instead: <b><code><span style="color:#3366CC;font-size:1.2em;">wget httpshttp://ict.senecacollegelittlesvr.ca/~peter.callaghan/ops245/labs/lab3-check-andrew.bash</span></code></b>-->
# Give the '''lab3-check.bash''' file execute permissions (for the file owner).
# Run the shell script with <code>sudo</code>, and if any warningsappear, make fixes fix the issues noted by the script and re-run shell script rerun the check until you receive the "congratulationsCongratulations!" message.#Arrange proof of the following on the screen:<br><span style="color:green;font-size:1.5em;">&#x2713;</span> '''centos3''' VM:<blockquote><ul><li> Archived files '''created'''</li><li>Archive files '''restored'''</ul></blockquote><span style="color:green;font-size:1.5em;">&#x2713;</span> '''c7host''' Machine:<blockquote><ul><li> '''One repository added''' for yum</li><li>Run the '''lab3-check.bash''' script (must have all <b><code><span style="color:#66cc00;border:thin solid black;font-size:1.2em;">&nbsp;OK&nbsp;</span></code></b> messages)</li></ul></blockquote><span style="color:green;font-size:1.5em;">&#x2713;</span> '''Lab3Lab 3''' log-book logbook pages filled out.#Upload a screen of the proof from the previous step, along with the file generated by '''lab3-check.bash''', your log book, and your '''tarchiver.py ''' script to blackboard.
= Practice For Quizzes, Tests, Midterm &amp; Final Exam =

Navigation menu