Python for System Administration
Currently i am learning Python. I have been trying out a lot of programming languages the last couple of years, but i think Python is something for itself, in a very positive way.
Python's syntax was the first thing that caught my attention. It is the most simple syntax i have ever experienced, and you can do a lot with very few and simple lines. That the blocks is based on indentation is also just plus from my side. It is not C-like, but if you are used to those languages, i still believe you will have a lot of fun learning Python.
The best way learning Python, for me, is simply just to write it. If you get the language into your hands, which happens fast with Python, the next step is a question about structuring your programming and thought you put into your lines.
I am passionate about Linux and open source systems, so i like to mix my Python learning with administrating these.
In a Linux environment it can be really hard to see why to use a programming language, because you have Bash (or another shell language) right? Bash is amazing and talks directly with your kernel, but i think this is a completely different thing. It is a extra layer on top of what you want to do in your system, where Bash also can fit in, but it is a layer where you can make a really well-structured script, and this will be easy to maintain and read - this is often also a part of what you want do.
An example
There is many ways and modules to program for your system, but lets simply use subprocess, which will communicate and send bash lines directly. Just to see the difference of structure. So we will make a similar Python and Bash script to show basic machine information.
Bash:
sysinfo.bash:
#!/usr/bin/env bash
function main() {
echo '\n Basic system Information \n'
echo '--- some text ---'
basic_info
echo '--- some text ---'
mem_space
echo '--- some text ---'
disk_space
echo ''
}
function disk_space() {
df -h
}
function mem_space() {
free -h
}
function basic_info() {
cat /etc/issue
uname -a
}
#initiate
main
Python:
sysinfo.py:
#!/usr/bin/env python
import subprocess
class SysInfo():
"""docstring for SysInfo"""
def __init__(self):
print '\n Basic system Information \n'
print '--- some text ---'
self.basic_info()
print '--- some text ---'
self.mem_space()
print '--- some text ---'
self.disk_space()
print ''
def disk_space(self):
subprocess.call('df -h', shell=True)
def mem_space(self):
subprocess.call('free -h', shell=True)
def basic_info(self):
subprocess.call('cat /etc/issue', shell=True)
subprocess.call('uname -a', shell=True)
si = SysInfo()
This is of course very simple, and is only outputting data, but i think it draws a small picture of what you want to do.
There is so much to explore, and Python got so much to offer, so i look forward to the rest of the trip of mastering Python (for system administration).
Interesting Python modules for system administration
IPython (Expanded Python shell)
Sys (Python core - http://www.python.org/doc/)
Os (Python core - http://www.python.org/doc/)
Subprocess (Python core - http://www.python.org/doc/)
Envoy (Python Subprocesses for Humans: Thanks to OrangeTux for mentioning)
Fabric (Streamlining server automation through SSH and locally - I really like this one)
Flask (Simple cool module for Python for web)
Circus (Socket and process management and monitoring from Mozilla)
Twisted (Network programming framework)
Psutil (Util for system and process information)
Salt (App, cloud, infrastructure and data center automation module)
Virtualenv
Python got this special powertool-thingy called Virtualenv i really like, and i will definitely use it the in the future. It is really simple actually. It is just a program to run isolated Python applications/enviroments, so you can test with eg. different versions of modules.
It works so you can install modules in the Python projects own bin-directory, and only use this. If you list your modules ($ yolk -l
) inside of a project, you can see what you have in your virtual Python enviroment, and if you deactive it and jump out, you can check again and it will show the modules from your basic Python installation and its modules.
Basic commands, and enough to get started:
Install Virtualenv
$ pip install virtualenv
Create environment
$ virtualenv dir-name/
This create your environment - can be used to create a new project, or use it on an exisiting. There is some different parameters to use aswell, to eg. inherit from the already installed Python modules. Read more in the docs.
Activate an environment
$ source bin/activate
In the environment folder, activate the virtualenv, but running the activate-file in bin/.
Deactive an enviroment
$ deactivate
Simply deactivate by running the deactivate command.
Resource
http://www.virtualenv.org/en/latest/
IDE
I looked for an editor, and i found PyCharm (http://www.jetbrains.com/pycharm/) extremely good. It got a full-working terminal integrated into it, and gives you a lot of tips and best-practice notices when you program, and lots of other stuff - check it out.