I sometimes heard claims such as “shell scripting is no more necessary since now we have Python“, or “I don’t like Python, I can do everything with shell scripts“.

Both of the above claims are too simplistic: skilled professionals that know both of them for true know also that they both have pros and cons. So the right approach probably is to use the one that best fit the use case in terms of delivery time and maintainability.

For example:

  • if the use case is housekeeping of files and directories, or automating tasks that run a lot of shell commands, a shell script certainly wins over a Python script in terms of delivery time
  • when it comes to data search and manipulation, in simplest use cases may be still convenient to use shell scripting, but when complex formats such as JSON, XML or YAML are involved, it is certainly more effective to develop using Python
  • the same applies when developing scripts that connect to REST API: it is straightforward that Python is the best choice

So, to keep it short, professionals should know both shell scripting as well as Python, so to pick the right one for the particular use case they are working onto.

The Python Smtplib Tutorial - howto send HTML EMAIL with Python is explains how to send HTML emails using SMTP, including auth, SMTP with TLS and SMTPS.

After introducing Python smtplib, the post provides an hands-on example showing how to add HTML email notification to an existing scirpt. The provided code supports every kind of SMTP connection - SMTP, SMTP with startTLS and SMTPS.

This post completes the trilogy we started with the JINJA2 With Python Tutorial JINJA Ansible HowTo and  Python Argparse Tutorial - Argparse HowTo posts.

Read more >

The Python Argparse Tutorial - Argparse HowTo post provides a practical example of how to parse command line parameters and options using the argparse module.

Knowing how to add command line paramenters and options to Python scripts is a must-have skill, since it enables them to better adapt to customers' needs, allowing them to enable or disable specific script features, or providing information on the operational environment structure, such as specific directory paths different from the default ones.

As a professional, you are supposed to always add command line arguments support to your scripts, so to make them always fit to the user-specific scenarios.

Read more >

JINJA2 is a powerful yet easy to use templating engine that enables easily generating documents on the fly blending one or more objects with a template.

Since the syntax is heavily inspired by Django and Python, JINJA2 is not only commonly used inside Python scripts: it is very often exploited by Ansible, for example when generating configuration files on the fly.

In "JINJA2 With Python Tutorial JINJA Ansible Howto" post we will learn all the must know about JINJA2, and experiment with it by writing a real-life JINJA2 template file, rendering it by merging it with a JSON document using a custom Python script and an Ansible playbook.

Read more >

Interacting with Git using Python is a very common use case in the DevOps field: very often it is necessary to checkout application’s or scripts along with their configuration or even just checkout versioned configurations. Although more rare, it is sometimes necessary to update the checked out contents and push the committed version back to the “origin” remote repository. In the "Git With Python HowTo GitPython Tutorial And PyGit2 Tutorial" post we play with the two most commonly used Python libraries used to interconnect to Git: gitpython and pygit2.

Python enables you to easily serialize objects as either JSON or YAML: very often it is very convenient to leverage on these features exploiting them to enhance your own object. YAML serialization comes almost for free if you derive your classes from the YAML object, whereas automatic instantiation of objects from a YAML document requires a little bit of work and leverages on YAML tags. As for JSON serialization, it is bloody useful and enables you to quickly and easily serialize the contents of your objects into JSON documents that can be exploited for example to interact with or to develop a REST API.

This post, Python Serialization as JSON or YAML exploiting YAML TAGS, shows you how to develop a Python package that provides such kinds of objects. As usual we take particularly care of the code style, using a very clean and portable design and adhering best practices.

The operating environment used in this post is Red Hat Enterprise Linux 9 or Rocky Linux 9 - using a different environment may lead to having to adapt or even change things.

Read more >

AWK is a powerful pattern scanning and processing language developed by Alfred Aho, Peter Weinberger and Brian Kernighan at Bell Labs - the name of this tool is indeed derived by concatenating the letter of their surnames to one another. It is one of that tools that every Linux professionals (not only the more seasoned ones) must be skilled on, since it is broadly used in a lot of shell scripts that very often are inherited from predecessors and that must be maintained: the sad truth is that very often is not worth the effort to rewrite them using other more modern languages, so knowing how to deal with it can really ease your life. And anyway, ... sometimes it requires much less time to code an AWK one liner than a Python script, so knowing how and when to use AWK is certainly a valuable skill still nowadays.
The aim of "The Ultimate AWK Tutorial For Professionals" is not to provide a complete explain about how to code with AWK - there are more modern and handy languages such as Python nowadays: I just want to provide a very quick yet comprehensive walkthrough on it focusing on how to write AWK one-liners that are often embedded in shell scripts or that you can use to sort out common system administration tasks. That's why I'm also showing some real-life use cases with AWK one-liners that can very quickly and easily sort things out.

Read more >