Skip to main content

Parse and edit your ipsec configuration files

Project description

Parse and edit your ipsec configuration files (ipsec.conf)

Installation

To install ipsecparse, simply:

pip install ipsecparse

Examples

# Load the configuration from a string.

from ipsecparse import loads

conf = loads(open('/etc/ipsec.conf').read())

# The configuration is represented as a dictionnary
# (actually a subclass of OrderedDict)

# Each section of the configuration is an OrderedDict.

# Let's modify some settings:

conf['config', 'setup']['nat_traversal'] = 'yes'

conf['conn', 'myconn']['left'] = '192.168.0.10'

# Create a connection:

conf['conn', 'mynewconn'] = {
    'leftsubnet': '10.0.0.0/16',
    'right': '192.168.0.1'
}

# You can also use an OrderedDict if order matters to you:

from collections import OrderedDict

conf['conn', 'mynewconn'] = OrderedDict(
    lefsubnet = '10.0.0.0/16',
    right = '192.168.0.1'
)

# Delete a connection:

del conf['conn', 'mynewconn']

# Same thing with certification authorities. Create a CA:

conf['ca', 'myca'] = {
    'cacert': 'MyCert.pem',
    'crluri': 'http://crl.example.com/mycrl.crl',
    'auto': 'add'
}

# Delete it:

del conf['ca', 'myca']

# Add an include:

conf['include', '/etc/ipsec.d/ipsec.include'] = True

# Delete it:

del conf['include', '/etc/ipsec.d/ipsec.include']

# Display the new configuration as a string:

print(conf.dumps())

# with four spaces indents instead of the default tabulations:

print(conf.dumps(indent = '    '))

# Replace the old configuration file:

with open('/etc/ipsec.conf', 'w') as fd:
    fd.write(conf.dumps())

# Search for connections inside the configuration.
# Pass a callable to the `conn_filter` method.

for name, section in conf.conn_filter(
    lambda conn: conn.get('leftsubnet') == '10.0.0.0/16'
):
    section['auto'] = 'start'

# Or use the Key and Keys class
# (just to make queries a bit shorter)

from ipsecparse import Key, Keys

for name, section in conf.conn_filter(
    Key('leftsubnet') == '10.0.0.0/16'
):
    section['auto'] = 'start'

for name, section in conf.conn_filter(
    Keys('left', 'right').contains('192.168.0.1')
):
    del conf['conn', name]

GitHub repo: https://github.com/leforestier/ipsecparse

Project details


Download files

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

Source Distributions

ipsecparse-0.1.0.zip (7.1 kB view details)

Uploaded Source

ipsecparse-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file ipsecparse-0.1.0.zip.

File metadata

  • Download URL: ipsecparse-0.1.0.zip
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ipsecparse-0.1.0.zip
Algorithm Hash digest
SHA256 f2ce20c81c6a25e2f84b020bcd1e50bdce7c1b93486e1783c1584ead5383e88a
MD5 12dfbdc3579b8105dd82f347e1a3b0ec
BLAKE2b-256 1b12f88ac62f6da2dc0fafd29b569a8068df241d3b3f33e1a350853bd30e4e4a

See more details on using hashes here.

File details

Details for the file ipsecparse-0.1.0.tar.gz.

File metadata

  • Download URL: ipsecparse-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ipsecparse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9f6f88e705ca667ed9d904c40e7cef6f5c28659ce51484b260ae4fbdd0dec48b
MD5 61b3cbf01ff5a0f83a2d7e5d61e2b8f7
BLAKE2b-256 5ea63c5be33c482167532720bf04c9af6497dd5bef25b7ff2d0ec5a1dfd29ebb

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