Skip to main content

apache2 configuration file parser and query tool

Project description

a2conf is utility and python module to work with apache2 config files.

For all examples we will use file example.conf which is available examples/example.conf. Use export PYTHONPATH=. to use module if it's not installed.

a2conf.py utility

Examples

Just smart grep

$ bin/a2conf -i examples/example.conf --cmd ServerName ServerAlias
ServerName example.com
ServerAlias www.example.com example.com 1.example.com 2.example.com
ServerName example.com
ServerAlias www.example.com 1.example.com 2.example.com secure.example.com

$ bin/a2conf -i examples/example.conf --cmd SSLCertificateFile
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem

Only arguments (one line, space-separated, non-unique):

$ bin/a2conf -i examples/example.conf --cmd ServerName ServerAlias --args
example.com www.example.com example.com 1.example.com 2.example.com example.com www.example.com 1.example.com 2.example.com secure.example.com

Unique arguments:

$ bin/a2conf -i examples/example.conf --cmd ServerName ServerAlias --uargs
secure.example.com 1.example.com www.example.com example.com 2.example.com

Filtering sections

# Only SSL hosts. Note: secure.example.com listed
$ bin/a2conf -i examples/example.conf --cmd ServerName ServerAlias --uargs --filter sslengine on
1.example.com example.com secure.example.com 2.example.com www.example.com

# Inverted filtering, hosts without SSLEngine on. Note: secure.example.com not listed
$ bin/a2conf -i examples/example.conf --cmd ServerName ServerAlias --uargs --filter sslengine on --neg
example.com 2.example.com 1.example.com www.example.com

Per-vhost info

$ bin/a2conf -i examples/example.conf  --cmd servername serveralias --uargs --vhost '{vhostargs} {args}'
*:80 example.com www.example.com example.com 1.example.com 2.example.com
*:443 example.com www.example.com 1.example.com 2.example.com secure.example.com

List ServerName and DocumentRoot for each virtualhost with SSL

$ bin/a2conf --vhost '{servername} {documentroot}' --filter SSLEngine on -i examples/example.conf
example.com /var/www/example

You can get list of all available tokens for --vhost option in verbose mode (-v option).

Node class

Properties and methods

raw - text line as-is, with all spaces, tabs and with comments

cmd - cmd ('ServerName') without args or None (if section)

section - section (e.g. 'VirtualHost')

args - one text line args to cmd or section. for vhost args could be '*:80', for ServerAlias: 'example.com example.org'

name - name of node. cmd if node has cmd, or section name (in brackets) if this is section. e.g. 'ServerName' or ''

Structure

For container sections (VirtualHost) attr content is not None. For usual lines (ServerName) content is None

content - list of child nodes or None

children(name=None, recursive=None) - return generator for all children nodes (e.g. for VirtualHost node). Generator is empty if no children. If name specified, generator will return only nodes with this name (e.g. 'servername' or ''). If recursive is On, generator will return nested nodes too (e.g. what is inside <IfModule> or <Directory> settings)

Examples

Just dump apache config

examples/ex1_dump.py just loads config and dumps its structure:

#!/usr/bin/env python3
import sys
import a2conf
root = a2conf.Node(name='#root')
root.read_file(sys.argv[1])
def recdump(node, prefix=""):
    if node.section:
        print(prefix, "SECTION", node.section, "ARGS", node.args, "CONTENT", len(node.content))
    elif node.cmd:
            print(prefix, repr(node.cmd), repr(node.args))
    for ch in node.children():
        recdump(ch, prefix+"  ")
recdump(root)

Output:

$ examples/ex1_dump.py examples/example.conf
   SECTION VirtualHost ARGS *:80 CONTENT 6
     'DocumentRoot' '/var/www/example'
     'ServerName' 'example.com'
     'ServerAlias' 'www.example.com example.com 1.example.com 2.example.com'
     'DirectoryIndex' 'index.html index.htm default.htm index.php'
     'Options' '-Indexes +FollowSymLinks'
   SECTION VirtualHost ARGS *:443 CONTENT 9
     'DocumentRoot' '/var/www/example'
     'ServerName' 'example.com'
     'ServerAlias' 'www.example.com 1.example.com 2.example.com secure.example.com'
     'DirectoryIndex' 'index.html index.htm default.htm index.php'
     'Options' '-Indexes +FollowSymLinks'
     'SSLEngine' 'On'
     'SSLCertificateFile' '/etc/letsencrypt/live/example.com/fullchain.pem'
     'SSLCertificateKeyFile' '/etc/letsencrypt/live/example.com/privkey.pem'
     'SSLCertificateChainFile' '/etc/letsencrypt/live/example.com/chain.pem'

Query

examples/ex2_query.py print all SSL sites from config:

#!/usr/bin/env python3
import sys
import a2conf
root = a2conf.Node(name='#root')
root.read_file(sys.argv[1])
for vhost in root.children('<VirtualHost>'):
    servername = next(vhost.children('servername')).args
    try:
        ssl_option = next(vhost.children('sslengine')).args
        if ssl_option.lower() == 'on':
            print("{} has SSL enabled".format(servername))
    except StopIteration:
        # No SSL Engine directive in this vhost
        continue

Output:

$ examples/ex2_query.py examples/example.conf
example.com has SSL enabled

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

a2conf-0.1.8.tar.gz (9.3 kB view details)

Uploaded Source

File details

Details for the file a2conf-0.1.8.tar.gz.

File metadata

  • Download URL: a2conf-0.1.8.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for a2conf-0.1.8.tar.gz
Algorithm Hash digest
SHA256 47d25b7cf220904e93199cc1acddf7d0e63dba57bdd1f8b712da37ce7b3261e9
MD5 fc4b16ff50274bf363cd3c36db6134a7
BLAKE2b-256 9be9d476ec8c63ac8cbe11f0d8285fac5147b702211ff651c76eb82536e89d09

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page