46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
import logging
|
|
from subprocess import run
|
|
|
|
_SUDO = '/usr/local/bin/sudo'
|
|
_PFCTL = '/sbin/pfctl'
|
|
|
|
|
|
def pfctl_cfg_read(anchor):
|
|
cmd = [_SUDO, _PFCTL, '-a', anchor, '-sr']
|
|
logging.info('Running %s', cmd)
|
|
|
|
res = run(cmd, capture_output=True, check=True)
|
|
|
|
logging.info('Result: %s', res)
|
|
return res.stdout.splitlines()
|
|
|
|
|
|
def pfctl_cfg_write(anchor, cfg):
|
|
cmd = [_SUDO, _PFCTL, '-a', anchor, '-f-']
|
|
logging.info('Running %s', cmd)
|
|
logging.info('Config %s', cfg)
|
|
|
|
res = run(cmd,
|
|
input=cfg,
|
|
check=True,
|
|
capture_output=True)
|
|
|
|
logging.info('Result: %s', res)
|
|
return res.stdout.splitlines()
|
|
|
|
|
|
def pfctl_table_op(anchor, **kwargs):
|
|
table = kwargs['table']
|
|
operation = kwargs['operation']
|
|
value = kwargs['value'] if 'value' in kwargs else None
|
|
cmd = [_SUDO, _PFCTL, '-a', anchor, '-t', table, '-T', operation, value]
|
|
|
|
logging.info('Running %s', cmd)
|
|
|
|
res = run([x for x in cmd if x is not None],
|
|
capture_output=True,
|
|
check=True)
|
|
|
|
logging.debug(res)
|
|
return res.stdout.splitlines()
|