I have a touch of a love and hate relationship with bash. I spend tons of your time within the terminal, and bash is my default “programming language“. Sometimes I tell folks that find, grep and xargs run their infrastructure, and that they laugh and laugh until they realize I’m serious.
Picking up some Python may be a perfect choice for system administrators. it is also great for anyone who has got to affect anything during a terminal but doesn’t want to use bash, or has needs that are too complex for bash. Once a task goes beyond
it’s time to interrupt out the Python!
There are tons of advantages to using Python as your attend instruction language.
- Python has many nice libraries to assist out with just about anything. that has handling system operations, reading files, listing directories, writing for loops, checking for exit codes, and so on.
- Autocomplete with IDEs. Seriously. Who wants to possess to memorize anything?
- Robust testing suite if that’s your thing (and if it isn’t , you ought to consider making it your thing).
- The iPython console. It’s wonderful. It’s amazing. I LOVE IT.
- Python is out there on most systems, and if it isn’t you’ll catch on with Miniconda.
- Robust error checking with attempt to catch blocks.
- If you’re employed on different operating systems you’ll use Python libraries which will affect all that under the hood.
- Even if you’ve got no programming ability Python is a simple language to urge started with.
Let’s get Started
To get started, first you will need to either have Python installed or install it with Miniconda.
Check if you have iPython installed
If both of those are successful, you’re in business! If you’ve got Python, but not iPython, you’ll need to install it. you’ll install it as a system package, but i actually recommend that you simply just install it with Miniconda.
Install Miniconda
Grab the installer for your OS here. I suggest getting the Python3 installation.
Then it’s just an easy installation.
Follow the prompts and you will have Miniconda3 installed. Once you’ve got it installed you’ll be wanting to run an update, because this is often tech and in fact you would like to run an update. 😉
Troubleshooting
If you’ve got trouble installing any packages here are some tips.
- Run conda clean –all and check out again.
- Make sure you’re using the right channel.
- Run conda update -y –all
- Try to install as little as possible to your global conda space. Instead create environments for various tasks and projects, which we’ll get into next.
Create Environments with Conda
If you have ever used virtualenv, pipenv (is that a thing?), Rbenv, plenv, anyenv or any of the opposite various envs that have popped up over the years, this may sound very familiar to you. the thought is that different projects should have their own isolated software environments.
If you’re like me and like to have iPython readily availabe make sure you install it to any new environments!
Python Libraries for System Administration
Before we get into the examples let’s just list some handy packages along side their docs.
My attend package is that the os package. you’ll use it to list directories, check if files exist, check if symlinks exist, make directories, run system commands, get and set environmental variables, and more. It’s great!
My second package for running system commands that do not exist as handy python libraries is that the subprocess module.
The shutil has file operations that are not within the os library.
The pprint library prints out complex data structures with nice indentation.
The pytest library let’s you test your Python code, because let’s face it, nothing ever works correctly the primary (few) times.
How Do I Execute my Code?
Finally! Code!
When you’re using Python for system administration you’ll dive straight into the iPython console, or write scripts then execute them with python name-of-script.py.
If you favor to write down your scripts you’ve got numerous choices, and it’s truly a matter of private preference. i exploit PyCharm, which is paid, but Visual Studio Code and Atom are equally excellent free choices.
I find that it depends on what I’m performing on . Sometimes I just open up the iPython console and begin typing, and other times i want something more robust with tests and whatnot.
If you’re using either the iPython console or any of the editors I listed above, you’ll have autocomplete. Autocomplete is awesome! With iPython simply start typing your function and press tab to urge an inventory of potential functions you’ll want.
I cannot express how much I love autocomplete.
Get Help
You can go to any of the doc pages for any library, but if you know the name of either the library or the function you can bring it up in iPython.
You can bring up the help menu in most IDEs and text editors too, but that will be specific to your editor.
Examples
First you will need to import your packages
Here are some examples of common file and directory operations.
Move files and directories around
Not everything is going to be available through python libraries, such as installing system libraries, so run a few system commands!
Write to files!
Write some tests!
Tests mostly work by using a function called assert, which is essentially saying make sure this is true and if not die loudly.
Put this function in a file called test_my_code.py
and run as pytest test_my_code.py
.
Wrap Up:
That’s it for my main tips and tricks for using Python as your go-to bash replacement. subsequent time you would like to write down a loop in bash, consider breaking out the iPython console and seeing what you’ll come up with instead!