/usr/lib/python3/dist-packages/dhcpcanon/clientscript.py is in dhcpcanon 0.7.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | # -*- coding: utf-8 -*-
# vim:ts=4:sw=4:expandtab 2
# Copyright 2016, 2017 juga (juga at riseup dot net), MIT license.
"""Class to Initialize and call external script."""
from __future__ import absolute_import, unicode_literals
import logging
import subprocess
import attr
from .constants import (ENV_OPTIONS_REQ, LEASEATTRS2ENVKEYS,
LEASEATTRS_SAMEAS_ENVKEYS, SCRIPT_ENV_KEYS,
SCRIPT_PATH, STATES2REASONS)
logger = logging.getLogger('dhcpcanon')
@attr.s
class ClientScript(object):
"""Simulates the behaviour of the isc-dhcp client-script or nm-dhcp-helper.
`client-script
<https://anonscm.debian.org/cgit/pkg-dhcp/isc-dhcp.git/tree/client/scripts/linux>`_
or `nm-dhcp-helper
<https://github.com/NetworkManager/NetworkManager/tree/master/src/dhcp>`_.
"""
scriptname = attr.ib(default=None)
env = attr.ib(default=attr.Factory(dict))
def __attrs_post_init__(self, scriptfile=None, env=None):
"""."""
logger.debug('Modifying ClientScript obj after creating it.')
self.scriptname = scriptfile or SCRIPT_PATH
if env is None:
self.env = dict.fromkeys(SCRIPT_ENV_KEYS, str(''))
else:
self.env = env
def script_init(self, lease, state, prefix='', medium=''):
"""Initialize environment to pass to the external script."""
if self.scriptname is not None:
logger.debug('Modifying ClientScript obj, setting env.')
if isinstance(state, int):
reason = STATES2REASONS[state]
else:
reason = state
self.env['reason'] = str(reason)
self.env['medium'] = str(medium)
# self.env['client'] = str('dhcpcanon')
# self.env['pid'] = str(os.getpid())
for k in LEASEATTRS_SAMEAS_ENVKEYS:
self.env[k] = str(lease.__getattribute__(k))
for k, v in LEASEATTRS2ENVKEYS.items():
self.env[k] = str(lease.__getattribute__(v))
self.env.update(ENV_OPTIONS_REQ)
else:
logger.debug('There is not script path.')
def script_go(self, scriptname=None, env=None):
"""Run the external script."""
scriptname = self.scriptname or scriptname
if scriptname is not None:
env = self.env or env
logger.debug('Calling script %s', scriptname)
logger.debug('with env %s', env)
sp = None
try:
sp = subprocess.check_output([scriptname],
stderr=subprocess.STDOUT, env=env)
except subprocess.CalledProcessError as e:
sp = e.output
logger.debug('sp err %s', sp)
return sp
return None
|