Skip to content
Snippets Groups Projects
test_postfix.py 1.12 KiB
Newer Older
Stéphane Diemer's avatar
Stéphane Diemer committed
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Check that postfix listen correctly the port 25.
Postfix is the service which handles email sendings.
Stéphane Diemer's avatar
Stéphane Diemer committed
'''
import os
import re
import subprocess
import sys


if not os.path.exists('/etc/postfix'):
    print('Postfix dir does not exists, test skipped.')
else:
    out = subprocess.check_output('netstat -pant | grep \':25\'', shell=True)
    out = out.decode('utf-8') if out else ''

    # netstat -pant output columns are:
    # Proto | Recv-Q | Send-Q | Local address | Foreign address | State | PID/Program name

    expected = r'tcp +\d +\d +127\.0\.0\.1:25 +0\.0\.0\.0:\* +LISTEN +[\d]+/master'
    found = False
    if out:
        for line in out.split('\n'):
            line = re.sub(r' +', ' ', line)
            if line:
                if not re.match(expected, line):
                    print('Unexpected result for postfix listening ports:\n%s' % line)
                    sys.exit(1)
                else:
                    found = True

    if not found:
        print('The port 25 is not listened by any process.')
        sys.exit(1)
    print('Postfix listening port: OK.')