715
edits
Changes
→INVESTIGATION 4: Using Python Scripting to Generate System Information Reports
'''Answer Investigation 3 observations (all parts and questions) in your lab log book.'''
= INVESTIGATION 45: Using Python Scripting to Generate System Information Reports =
Before we can successfully automate parts of configuration using python, we need to learn how to use it. Initially we will perform simple tasks we have already learned how to do in bash. This way can learn how the language works as we go.
:'''Perform the Following Steps:'''
<ol>
<li>Install Check if python3 is installed on your Centos RHEL Host machine. Open a terminal and type:</li>
<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">
which python3</code><li>The output should of the previous command should show python3 is already installed. If it is not, install python3 on your RHEL Host machine.</li><code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">sudo yum dnf install python3
</code>
<li><ul><li>Create a new file in your '''~/bin''' directory called '''myreport.py'''</li>
<li>Add a line that will print out the heading '''System Report'''<br />
<code style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;font-weight:bold;">
print('System Report\n')
</code>
</li>
<li>Save your script and run it. Does it work?</li>
<li>You'll notice that the python script is currently sending its output to your terminal. Since we haven't covered how to write to a file yet, this is ok for now. Just use output redirection on the command line when you run the script to send the output to '''~/bin/pythonreport.txt'''.</li>
<li>Add Open your script in a text editor (like Vi) again, and add the following lines below the print statement:</li><pre style="color:#3366CC;font-family:courier;font-size:.9em;margin-left:20px;># Import the Operating System moduleimport os # Print a heading for the date command outputprint('Current Date:') # Call the date command using the os moduleos.system("date +'%A %B %d, %Y (%I:%M %p)'")</pre><li>Save your script and run it again. Observe the output. What do you think the os.system command did?</li><li>Based on the above example and output, add the extra commands for your python script to also output (with appropriate headings):
<ul>
<li>The hostname of the machine.</li>
<li>The kernel version.</li>
</ul>
</li>
<li>Run your script to make sure it works. Note that the output does not need to match investigation 3 4 exactly, but it should be very close.</li>
</ol>