The os module extends the reach of our script from the
interpreter to the operating system.
Not to sound like a broken record, but as with the sys,
details on the module can be found directly on the python.org site.
Each function, including some of those we have already talked through,
are broken down with some of the more important detail on how to implement.
Some of the functions come with multiple parameters and
nesting abilities to extend the function.
As mentioned,
this is part of the standard library and does not require you to download,
but it does have to be imported.
So, what is os?
Well, it is a set of functions that allow you to interact with the,
what else, operating system running the script.
Taking a look at a few of these now.
The open, close,
and remove functions allow you to interact with files on the server,
something as standard as maintaining a log for the script
running or diving deep into the security world and open a
file to check for malware,
reverse engineering a virus or importing a definition to
crack a password or compare a YARA file.
The getcwd function returns the current working directory
so you are able to jump around in case you want to access
files in other directories.
Getpid will return the process ID of the current running script.
When we start to talk about the psutil library in the next clip,
we will get into how to pull back the PID of other processes which may be
useful in understanding what is running on your machine.
Finally, in this section, we will look at system,
which will allow you to run command line functions.
Having communication problems to a server,
want to ping or trace the route, it is possible.
You can kick off executable or perform any of the standard
uses of the command line you can think of.
A very powerful tool here.
One item of note,
the subprocess module intends to replace the
os.system as it spawns new processes.
I still use the os.system today, so I will demo it,
but you'll want to learn about the subprocess module we
covered in the last part of this course.
Okay,
let's run through a short demo here of some of the functions we just
covered and see how we can combine them with the methods and functions
from the sys module we covered last section.
The first couple here, getcwd, or the get current working directory,
should show me the directory this script is being ran out of.
The getpid, or the get process ID,
will return the process ID of the executing of the script.
And the os.name will uncover the name of the operating
system I'm currently running on, which I will say is kind of limited,
so there's only so many that are registered on there and will be returned.
So I'm going to throw in there the sys.platform to show the
difference as the platform can go a little bit deeper.
All right, so I'm in here now.
I'm going to run this.
Okay, so here is my current working directory.
So I made mention that I'm working out of a folder on
the desktop called Pluralsight before.
So this script is being ran out of that working directory. Here is the
PID, or the process ID, of the execution of this script.
Next up we have our operating system, so nt as you see
there, and then I call the platform, which can dive a little
deeper, and that's showing win32.
Okay,
next we're going to talk about being able to use the system function to call
out commands. Here I am running a ping just of the localhost,
the 127.0.0.1.
So when I run that, you'll see that the ping is going through.
Coming back into the code, I can also run a ping of an IP that I
add to the arguments of the script.
So if you have a server acting up or needing to be audited or
checked, using the argument saves you from having to hard code and
create different scripts for different servers,
not have to have a specific server in the script.
So I'm going to run this against the 127.0.0.1, and as you can see just by the
arguments alone, it's going to pull that and start pinging it. All right, in
the last demo, we talked about opening a file and writing to it. In this case
I'm reading the log.txt file and I'm going to display it on the screen. So I'm
going to go through that, and you can see that this is all the contents of
that log.txt file.
But how do I delete a file?
That's where the os.remove comes into play.
So when I call this remove,
it's actually going to look for that temp.txt file and
it's going to go through and delete it.
I can add directories in here if I want to delete a file in a
different directory, I just need to make sure that the script has
permission to access that file and to delete it.
But once I run this, you'll see that that temp.txt file has disappeared.
And then finally, a few functions we haven't really talked about too much
is the walk, the path, and the join. As I enable this, what you will see
is when you execute it, all the files and file structure of that
Pluralsight folder I'm running the script out of, this includes all the
buried YARA rule directories.
So as I go through and scroll through here a little bit,
you're going to see all the different files and directories that are
sitting in here are now displayed because I was able to walk through them
all and basically print the path on the screen.
All right, that's enough for now.
Let's jump into the regular expressions library next.