:::: MENU ::::

Posts Categorized / Linux

  • Sep 27 / 2018
  • 0

Extract certificate and/or key from a PKCS12 file

The PKCS12 format is replacing the old PFX format from Microsoft. This format will allow storage of X.509 private keys and the associated public certificates in a single encrypted file.

So you can extract the key and the certificate in a single common PEM file, you can use this openssl command:

openssl pkcs12 -in myCertificate.pfx -out myCertificateAndMyKey.pem

If you want to extract the key and the certificate independently, you can also use the options nocerts/nokeys along with openssl, to extract only one part:

openssl pkcs12 -in myCertificate.pfx -nocerts -out myCertificate.key
openssl pkcs12 -in myCertificate.pfx -nokeys -out myCertificate.pem
  • Jul 25 / 2018
  • Comments Off on Automate apt update and keep current config files

Automate apt update and keep current config files

When doing update on a Debian/Ubuntu server, if a package is trying to modify a config file, it’s asking for a manual choice. If you’re trying to try to automate the process, you’d probably like to get rid of that ask and keep the current config file by default.

For that, you can use that script:

apt-get update
listUpgrades=`apt list --upgradable |grep upgradable |cut -d/ -f1`
execUpgrades="DEBIAN_FRONTEND=noninteractive apt-get --yes --assume-yes -o DPkg::options::=\"--force-confdef\" -o DPkg::options::=\"--force-confold\" --only-upgrade install "$listUpgrades
eval $execUpgrades

The multiple options will enforce the update to keep current config and not overwrite it with the package default one.

  • Jun 04 / 2018
  • 0

Installing Oracle JRE using command line (no repo)

There is no way to easily automate deployment of Oracle JRE since there is no official repository for most of the linux distros. But there’s still a workaround to automatically download the JRE and install it locally on a server by using command lines.

Here is an example of how you can download the JRE 1.8u172 from official website for a CentOS server. You’ll have to grab the URL of the package you want to download from the website http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

First, download the package from the website by accepting the license automatically:

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u172-b11/a58eab1ec242421181065cdc37240b08/jre-8u172-linux-x64.rpm"

Then, install the package you downloaded on the server:

sudo yum localinstall jre-8u172-linux-x64.rpm
  • May 25 / 2018
  • 0

Change the passphrase of your SSH key

Forgot your SSH key passphrase? There’s an easy way to renew it locally if you have access to it:

ssh-keygen -p -f ~/.ssh/id_rsa

And then, enter the new passphrase you want to use.

  • 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:

apt-get install procmail

or on a CentOS-based:

yum install procmail

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

import os
import time
import email
import sys
import logging.handlers
import base64

# Constants
LOG_FILE_PATH = os.path.expanduser('/opt/mailAnalysis.log')

# Set up a logger
my_logger = logging.getLogger('MyLogger')
handler = logging.handlers.RotatingFileHandler(LOG_FILE_PATH,  maxBytes=500000, backupCount=4,)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s",  "%Y-%m-%d %H:%M:%S")

# Main function
def main():
        # Get message
        full_msg = sys.stdin.read()
        msg = email.message_from_string(full_msg)

        # Prepare dict containing data
        data = {}

        # Fill dict 
        data['From'],e    = email.Header.decode_header(msg['From'])[0] if msg.has_key('From') else ''
        data['To'],e      = email.Header.decode_header(msg['To'])[0] if msg.has_key('To') else ''
        data['Subject'],e = email.Header.decode_header(msg['Subject'])[0] if msg.has_key('Subject') else ''
        data['Body']      = msg.get_payload()

        # Add information to log
        my_logger.info('From:    ' + data['From'])
        my_logger.info('To:      ' + data['To'])
        my_logger.info('Subject: ' + data['Subject'])
        my_logger.info('Body:    ' + data['Body'])
    except Exception,e:
        my_logger.error('----- ERROR ENCOUNTERED')
        my_logger.error('----- END OF ERROR')

# Main program
if __name__ == "__main__":
    start_time = time.time()
    my_logger.info('----- START: ' + str(time.time()))
    result = main()
    my_logger.info('----- END: ' + str(time.time()))
    my_logger.info('Elapsed Seconds: ' + str(time.time() - start_time))

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


* ^Subject:.*procmail.*
* ^[email protected](com|net)

  :0 fw
  | /usr/bin/python /home/mailuser/procmail_script.py

  :0 e

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:

From: [email protected]
To: [email protected]
Subject: This is a procmail testing

Hey there,
I hope this message will be parsed and logged properly as expected.
This is my first test for procmail deployment!

Then, you can test it by executing procmail manually:

procmail VERBOSE=on /home/mailuser/.procmailrc < /home/mailuser/procmail_test.txt

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

2018-03-31 08:08:45 - INFO - ----- START: 1522570125.06
2018-03-31 08:08:45 - INFO - From:    [email protected]
2018-03-31 08:08:45 - INFO - To:      [email protected]
2018-03-31 08:08:45 - INFO - Subject: This is a procmail testing
2018-03-31 08:08:45 - INFO - Body:    Hey there,
I hope this message will be parsed and logged properly as expected.
This is my first test for procmail deployment!
2018-03-31 08:08:45 - INFO - ----- END: 1522570125.12
2018-03-31 08:08:45 - INFO - Elapsed Seconds: 0.0624470710754
  • Feb 12 / 2018
  • 0

Execute cron on a specific day of the month (e.g. second monday)

How to execute a cron on a specific day of the week once in the month?

This could look simple as we could think that this line in cron would do the trick:

# Run on every second Tuesday of the month
15 3 8-14 * 2  /usr/bin/bash /opt/myscriptfortuesday.sh

But this would not work as the ‘2’ for checking the Tuesday will come as a OR condition, and the command would be executed from day 8 to day 14 and on every Tuesday of the month.

As a workaround for that, you can use that command:

# Run on every second Tuesday of the month
15 3 8-14 * * test $(date +%u) -eq 2 && /usr/bin/bash /opt/myscriptfortuesday.sh

Here is the explanation of this cron line:

15   = 15th minute
3    = 3am
8-14 = between day 8 and day 14 (second week)
*    = every month
*    = every day of the week
test $(date +%u) -eq 2 && /usr/bin/bash /opt/myscriptfortuesday.sh = the command to execute with a check on the date

Doing this check will allow to verify first that we are on the second tuesday before to execute the command. Don’t forget to add a backslash before the ‘%’ character to escape it.

Question ? Contact