:::: MENU ::::

Posts Categorized / Python

  • Apr 01 / 2018
  • 0
Linux, Python

Using procmail with custom python script

If you want to execute some special commands, log or make an API call when you’re receiving an email onto your server, you can easily set up this by using procmail. Procmail is designed to filter and sort emails but can make any call you want.

Here, as an example, we will set up a simple call to a python script that will read the content of the mail (headers and body) and put the information into a log file.

1. Install procmail

Depending on the OS you’re using, you should find a package pre-compiled in the common repositories.

For example, on a Debian-based:

or on a CentOS-based:

2. Build the python script you want to call to analyze the message

You now have to prepare your script you will execute when receiving an email, that will read and parse the content to log interesting information in a file.

Let’s create a script called procmail_script.py

3. Configure your user to pass the mail to your script

So that you can pass rules you want to execute when receiving an email, you need to create a file (hidden) called .procmailrc that will take place in the home directory of the user you want to use.

For example, for executing rules when receiving email to [email protected], you will have to put that file into the home dir like /home/mailuser/.procmailrc

This will perform multiple steps:

  1. Check that the mail recipient is [email protected] or [email protected]
  2. Put a copy of the email into the procmail-dir directory
  3. Pass the message to our python script procmail_script.py
  4. Discard the message if the script succeeds (remove from queue) or copy it to procmail-failed-dir if failed (so you can process it later)

4. Prepare an email and perform a testing locally

First, create a sample mail that you will use for testing in a file called procmail_test.txt:

Then, you can test it by executing procmail manually:

Now, open the file /opt/mailAnalysis.log and you should have something like:

  • May 30 / 2017
  • 0
Linux, Python

DNS queries from a file/list to CSV

It’s not easy to perform bulk DNS resolution when you have many DNS/IPs to control. Here is a simple script allowing you to perform DNS resolution over a list of DNS entries or IPs.

Here is a list of DNS (names and IPs) that we put in a file called listDNS.txt

Let’s copy that script that will do the job in a file called resolverDNS.sh

And now, execute it by passing file path as an arg, and see the output:

Resolution are done for every line, depending on if it’s an IP or a name (and remain empty if it can’t resolve).
Feel free to adjust the script according to your needs!

  • Jun 09 / 2016
  • 0
Linux, Python

openssl/pyOpenSSL – “SSL23_GET_SERVER_HELLO:tlsv1 alert internal error”openssl/pyOpenSSL – “SSL23_GET_SERVER_HELLO:tlsv1 alert internal error”

You’re getting this annoying error message again and again when trying to fetch certificate and/or establish a connection to your website using openssl:

This issue is well known in several openssl versions, and a bug has been addressed for Ubuntu repositories:

For now, there’s a simple workaround that works to quickly fix it!

For openssl

If you’re facing it while using openssl directly, you can fix it by specifying the servername on command-line:

For pyOpenSSL

If you’re having this issue while using pyOpenSSL (python wrapper for OpenSSL), it can also be fixed with a quick workaround by adding the option set_tlsext_host_name() to specify the server name in your “Connection” object.
You will get something like this:

Vous avez ce message ennuyeux encore et encore lorsque vous essayer de récupérer le certificat ou tout simplement en essayant d’établir une connexion à votre site web en utilisant openssl :

Ce problème est bien connu sur de nombreuses versions d’openssl, et un bug a même été remonté pour les dépôts Ubuntu :

Pour le moment, il y a un moyen simple de rapidement contourner le problème !

Pour openssl

Si vous rencontrez le problème lorsque vous utilisez openssl directement, vous pouvez corriger le souci en spécifiant l’option servername sur la ligne de commande :

Pour pyOpenSSL

Si vous avez le problème en utilisant pyOpenSSL (un wrapper python pour OpenSSL), il est également possible de contourner le problème en ajoutant l’option set_tlsext_host_name() pour spécifier le nom du serveur dans votre objet “Connection” :
Vous aurez alors quelque chose comme :

  • Mar 30 / 2016
  • 0
Linux, Python

Generate SHA-512 hash on command-line with PythonGénérer un hash SHA-512 en ligne de commande avec Python

Need to generate the hash for a password? No need to use an online generator, totally insecure for your passwords …
This simple command will ask you which string you want to hash and will return you the result after pressing “Enter” key!

Besoin de générer un hash pour un mot de passe ? Pas besoin d’utiliser un générateur en ligne, totalement insécurisé pour vos mots de passe …
Cette simple commande vous demandera pour quelle chaine vous souhaitez effectuer un hash et vous retournera le résultat dès que vous presserez “Entrée” !

  • Oct 09 / 2015
  • 0

AWS – Update massively metadata using boto (python) on multiple S3 objects

A simple script that allows you to update massively Content-Type for files on an S3 bucket.
This script is able to:

  • Browse recursively a bucket
  • Perform action only on files matching specific prefix
  • Auto-detect type of file depending on its extension

Obviously, you can add any extension you want to the function update_md to handle more if needed.

  • Sep 18 / 2015
  • 0

Python – Browse directory and push on S3 (with regex)Python – Parcourir un répertoire et envoyer vers S3 (avec regex)

A simple script that can browse a directory, and upload to S3 some files matching a regex.
The file will be uploaded by respecting the path you have in local.

Un simple script qui permet de parcourir un répertoire, et d’envoyer vers S3 certains fichiers correspondant à une expression régulière.
Le fichier sera envoyé en respectant le chemin que vous avez en local.

Question ? Contact