debinterface¶
Submodules¶
debinterface.adapter¶
The NetworkAdapter class represents an interface and its configuration from the /etc/network/interfaces. It tries to validate data before writting, but it is by no means fool proof. It has setter for many common options, but it is impossible to have setter for every options on earth !
-
class
debinterface.adapter.
NetworkAdapter
(options, interfaces_path=None)[source]¶ Bases:
object
A representation a network adapter.
-
attributes
¶
-
validateAll
()[source]¶ Not thorough validations... and quick coded.
Raises: ValueError
– if there is a validation error
-
validateOne
(opt, validations, val)[source]¶ Not thorough validations... and quick coded.
Parameters: - opt (str) – key name of the option
- validations (dict) – contains the validations to checks
- val (any) – the option value
Raises: ValueError
– if there is a validation error
-
static
validateIP
(ip)[source]¶ Validate an IP Address. Works for subnet masks too. Tries ipv4 then ipv6
- Args:
- ip (str): the IP as a string
- Raises:
- socket.error on invalid IP
-
setName
(name)[source]¶ Set the name option of an interface.
Parameters: name (str) – the name of the interface Raises: ValueError
– if there is a validation error
-
setAddrFam
(address_family)[source]¶ Set the address family option of an interface.
Parameters: address_family (str) – one of ‘inet’, ‘inet6’, ‘ipx’ Raises: ValueError
– if there is a validation error
-
setAddressSource
(address_source)[source]¶ Set the address source for an interface.
Valid values are : dhcp, static, loopback, manual, bootp, ppp, wvdial, dynamic, ipv4ll, v4tunnel
- Args:
- address_source (string): address source for an interface
- Raises:
- ValueError: if there is a validation error
-
setAddress
(ip_address)[source]¶ Set the ipaddress of an interface.
Parameters: ip_address (str) – the IP as a string Raises: ValueError
– if there is a validation error
-
setNetmask
(netmask)[source]¶ Set the netmask of an interface.
Parameters: netmask (str) – the netmask IP as a string Raises: ValueError
– if there is a validation error
-
setGateway
(gateway)[source]¶ Set the default gateway of an interface.
Parameters: gateway (str) – the gateway IP as a string Raises: ValueError
– if there is a validation error
-
setBroadcast
(broadcast)[source]¶ Set the broadcast address of an interface.
Parameters: broadcast (str) – the broadcast IP as a string Raises: ValueError
– if there is a validation error
-
setNetwork
(network)[source]¶ Set the network identifier of an interface.
Parameters: network (str) – the IP as a string Raises: ValueError
– if there is a validation error
-
setAuto
(auto)[source]¶ Set the option to autostart the interface.
Parameters: auto (bool) – interface will be set as auto if True Raises: ValueError
– if there is a validation error
-
setHotplug
(hotplug)[source]¶ Set the option to allow hotplug on the interface. Beware, option is really called allow-hotplug, that’s a small historic cruft...
- Args:
- hotplug (bool): interface hotplug will be set if True
- Raises:
- ValueError: if there is a validation error
-
setHostapd
(hostapd)[source]¶ Set the wifi conf file on the interface.
Raises: ValueError
– if there is a validation error
-
setNameservers
(nameserver)[source]¶ Set the ipaddress of an interface.
Raises: ValueError
– if there is a validation error
-
setDnsNameservers
(nameservers)[source]¶ Set the dns nameservers on the interface.
Parameters: nameservers (str, list) – the IP as a string Raises: ValueError
– if there is a validation error
-
setDnsSearch
(searchUri)[source]¶ Set the dns default search URI.
Parameters: searchURI (str, list) – The default search domain Raises: ValueError
– if there is a validation error
-
setBropts
(opts)[source]¶ Set the bridge options of an interface.
Parameters: opts (dict) – a dictionary mapping option names and values. In the interfaces file, options will have a bridge prefix. Raises: ValueError
– if there is a validation error
-
setWpaConf
(conf_path)[source]¶ Set the wpa supplicant configuration path for supplicant config for wireless interfaces.
Parameters: conf_path (str) – Path at which the supplicant config is located.
-
replaceBropt
(key, value)[source]¶ Set a discrete bridge option key with value
Parameters: - key (str) – the option key in the bridge option
- value (any) – the value
-
appendBropts
(key, value)[source]¶ Set a discrete bridge option key with value
Parameters: - key (str) – the option key in the bridge option
- value (any) – the value
-
setUp
(up)[source]¶ Set and add to the up commands for an interface.
Parameters: up (list) – list of shell commands
-
appendUp
(cmd)[source]¶ Append a shell command to run when the interface is up.
Parameters: cmd (str) – a shell command
-
setDown
(down)[source]¶ Set and add to the down commands for an interface.
Parameters: down (list) – list of shell commands
-
appendDown
(cmd)[source]¶ Append a shell command to run when the interface is down.
Parameters: cmd (str) – a shell command
-
setPreUp
(pre)[source]¶ Set and add to the pre-up commands for an interface.
Parameters: pre (list) – list of shell commands
-
appendPreUp
(cmd)[source]¶ Append a shell command to run when the interface is pre-up.
Parameters: cmd (str) – a shell command
-
setPreDown
(pre)[source]¶ Set and add to the pre-down commands for an interface.
Parameters: pre (list) – list of shell commands
-
appendPreDown
(cmd)[source]¶ Append a shell command to run when the interface is pre-down.
Parameters: cmd (str) – a shell command
-
setPostUp
(post)[source]¶ Set and add to the post-up commands for an interface.
Parameters: post (list) – list of shell commands
-
appendPostUp
(cmd)[source]¶ Append a shell command to run when the interface is post-up.
Parameters: cmd (str) – a shell command
-
setPostDown
(post)[source]¶ Set and add to the post-down commands for an interface.
Parameters: post (list) – list of shell commands
-
appendPostDown
(cmd)[source]¶ Append a shell command to run when the interface is post-down.
Parameters: cmd (str) – a shell command
-
setUnknown
(key, val)[source]¶ Stores uncommon options as there are with no special handling It’s impossible to know about all available options WARNING: duplicated directives are overwriten. TODO better
- Args:
- key (str): the option name val (any): the option value
-
export
(options_list=None)[source]¶ Return a copy of the ifAttributes data structure as dict. You may optionally pass a list of options to return
- Args:
- options_list (list, optional): a list of options you want
- Returns:
- dict: the ifAttributes data structure, optionally filtered
-
set_options
(options)[source]¶ Set options, either only the name if options is a str, or all given options if options is a dict
- Args:
- options (str/dict): historical code... only set
- the name if options is a str, or all given options if options is a dict
- Raises:
- ValueError: if validation error socket.error: if validation error of an IP Exception: if anything weird happens
-
debinterface.adapterValidation¶
The NetworkAdapterValidation tries to validate data before writting, but it is by no means fool proof as it is impossible to have checks for everything as any package can add its keys.
-
class
debinterface.adapterValidation.
NetworkAdapterValidation
[source]¶ Bases:
object
Class to validate an adapter. It validates some options for: - presence - type - authorized values
-
validate_all
(if_attributes)[source]¶ Not thorough validations... and quick coded.
Parameters: if_attributes (dict) – the dict representation of the interface Raises: ValueError
– if there is a validation error
-
validate_one
(opt, validations, val)[source]¶ Not thorough validations... and quick coded.
Parameters: - opt (str) – key name of the option
- validations (dict) – contains the validations to checks
- val (any) – the option value
Raises: ValueError
– if there is a validation error
-
debinterface.dnsmasqRange¶
-
class
debinterface.dnsmasqRange.
DnsmasqRange
(path, backup_path=None, leases_path='/var/tmp/dnsmasq.leases')[source]¶ Bases:
object
Basic dnsmasq conf of the more file which holds the ip ranges per interface. Made for handling very basic dhcp-range options
-
backup_path
= None¶
-
config
¶
-
update_range
(interface, start, end, lease_time)[source]¶ Update existing range based on the interface name If does not exist will be created
Parameters: - interface (str) – interface name
- start (str) – start ip of range
- end (str) – end ip of range
- lease_time (str) – lease_time
Returns: - True if configuration was updated or created,
False otherwise
Return type: bool
-
debinterface.hostapd¶
-
class
debinterface.hostapd.
Hostapd
(path, backup_path=None)[source]¶ Bases:
object
basic hostapd conf file handling
-
backup_path
= None¶
-
config
¶
-
validate
()[source]¶ Not sure which ones are really necessary for everyone, here are the ones I require. I created 4 groups of keys : basic must always be there, wireless if you want to be an AP, auth if you want to add some security, bridge for, well, bridging Not foul proof !
Returns: True if everything went ok
Return type: bool
Raises: KeyError
– missing keyValueError
– invalid data
-
debinterface.interfaces¶
-
class
debinterface.interfaces.
Interfaces
(update_adapters=True, interfaces_path='/etc/network/interfaces', backup_path=None, header_comment=None)[source]¶ Bases:
object
By default read interface file on init
Parameters: - update_adapters (bool, optional) – load adapters from interface file. Default True
- interfaces_path (str, optional) – default to /etc/network/interfaces
- backup_path (str, optional) – default to /etc/network/interfaces.bak
- header_comment (str, optional) – default to none, otherwise sets comments at the top of the interfaces file.
-
adapters
¶
-
interfaces_path
¶ Returns the path of the main interface file Others may come from the source directive. Adapters register the path
-
backup_path
¶
-
header_comment
¶
-
getAdapter
(name)[source]¶ Find adapter by interface name
Parameters: name (str) – the name of the interface Returns: the new adapter or None if not found Return type: NetworkAdapter
-
addAdapter
(options, index=None, interfaces_path=None)[source]¶ Insert a NetworkAdapter before the given index or at the end of the list. Options should be a string (name) or a dict
Parameters: - options (string or dict) – options to build a network adaptator
- index (integer, optional) – index to insert the NetworkAdapter
- interfaces_path (str, optional) – path to the source file of the adadpter.
Returns: the new adapter
Return type:
-
removeAdapter
(index)[source]¶ Remove the adapter at the given index. TODO: handle file deletion of sourced paths
- Args:
- index (int): the position of the adapter
-
removeAdapterByName
(name)[source]¶ Remove the adapter with the given name.
Parameters: name (str) – the name of the interface
-
static
upAdapter
(if_name)[source]¶ Uses ifup
Parameters: if_name (str) – the name of the interface Returns: True/False, command output. Return type: bool, str
debinterface.interfacesReader¶
-
class
debinterface.interfacesReader.
InterfacesReader
(interfaces_path)[source]¶ Bases:
object
Short lived class to read interfaces file
-
adapters
¶
-
header_comments
¶
-
parse_interfaces
(read_comments=False)[source]¶ Read /etc/network/interfaces (or specified file). Save adapters Return an array of networkAdapter instances.
-
debinterface.interfacesWriter¶
-
class
debinterface.interfacesWriter.
InterfacesWriter
(adapters, interfaces_path, backup_path=None, header_comment=None)[source]¶ Bases:
object
Short lived class to write interfaces file
if backup_path is None => no backup
-
adapters
¶
-
_check_interfaces
(interfaces_path)[source]¶ Uses ifup to check interfaces file. If it is not in the default place, each interface must be checked one by one.
Parameters: interfaces_path (string) – the path to interfaces file Raises: ValueError
– if invalid network interfaces
-
_write_addrFam
(interfaces, adapter, ifAttributes)[source]¶ Construct and write the iface declaration. The addrFam clause needs a little more processing.
-
_write_callbacks
(interfaces, adapter, ifAttributes)[source]¶ Write the up, down, pre-up, pre-down, post-up, and post-down clauses.
-
_write_plugins
(interfaces, adapter, ifAttributes)[source]¶ Write plugins options, currently hostapd.
-
debinterface.toolutils¶
-
debinterface.toolutils.
safe_subprocess
(command_array)[source]¶ Executes shell command. Do not raise
Parameters: command_array (list) – ideally an array of string elements, but may be a string. Will be converted to an array of strings Returns: True/False (command succeeded), command output Return type: bool, str
Module contents¶
Imports for easier use
-
class
debinterface.
NetworkAdapter
(options, interfaces_path=None)[source]¶ Bases:
object
A representation a network adapter.
-
appendBropts
(key, value)[source]¶ Set a discrete bridge option key with value
Parameters: - key (str) – the option key in the bridge option
- value (any) – the value
-
appendDown
(cmd)[source]¶ Append a shell command to run when the interface is down.
Parameters: cmd (str) – a shell command
-
appendPostDown
(cmd)[source]¶ Append a shell command to run when the interface is post-down.
Parameters: cmd (str) – a shell command
-
appendPostUp
(cmd)[source]¶ Append a shell command to run when the interface is post-up.
Parameters: cmd (str) – a shell command
-
appendPreDown
(cmd)[source]¶ Append a shell command to run when the interface is pre-down.
Parameters: cmd (str) – a shell command
-
appendPreUp
(cmd)[source]¶ Append a shell command to run when the interface is pre-up.
Parameters: cmd (str) – a shell command
-
appendUp
(cmd)[source]¶ Append a shell command to run when the interface is up.
Parameters: cmd (str) – a shell command
-
attributes
¶
-
export
(options_list=None)[source]¶ Return a copy of the ifAttributes data structure as dict. You may optionally pass a list of options to return
- Args:
- options_list (list, optional): a list of options you want
- Returns:
- dict: the ifAttributes data structure, optionally filtered
-
replaceBropt
(key, value)[source]¶ Set a discrete bridge option key with value
Parameters: - key (str) – the option key in the bridge option
- value (any) – the value
-
setAddrFam
(address_family)[source]¶ Set the address family option of an interface.
Parameters: address_family (str) – one of ‘inet’, ‘inet6’, ‘ipx’ Raises: ValueError
– if there is a validation error
-
setAddress
(ip_address)[source]¶ Set the ipaddress of an interface.
Parameters: ip_address (str) – the IP as a string Raises: ValueError
– if there is a validation error
-
setAddressSource
(address_source)[source]¶ Set the address source for an interface.
Valid values are : dhcp, static, loopback, manual, bootp, ppp, wvdial, dynamic, ipv4ll, v4tunnel
- Args:
- address_source (string): address source for an interface
- Raises:
- ValueError: if there is a validation error
-
setAuto
(auto)[source]¶ Set the option to autostart the interface.
Parameters: auto (bool) – interface will be set as auto if True Raises: ValueError
– if there is a validation error
-
setBroadcast
(broadcast)[source]¶ Set the broadcast address of an interface.
Parameters: broadcast (str) – the broadcast IP as a string Raises: ValueError
– if there is a validation error
-
setBropts
(opts)[source]¶ Set the bridge options of an interface.
Parameters: opts (dict) – a dictionary mapping option names and values. In the interfaces file, options will have a bridge prefix. Raises: ValueError
– if there is a validation error
-
setDnsNameservers
(nameservers)[source]¶ Set the dns nameservers on the interface.
Parameters: nameservers (str, list) – the IP as a string Raises: ValueError
– if there is a validation error
-
setDnsSearch
(searchUri)[source]¶ Set the dns default search URI.
Parameters: searchURI (str, list) – The default search domain Raises: ValueError
– if there is a validation error
-
setDown
(down)[source]¶ Set and add to the down commands for an interface.
Parameters: down (list) – list of shell commands
-
setGateway
(gateway)[source]¶ Set the default gateway of an interface.
Parameters: gateway (str) – the gateway IP as a string Raises: ValueError
– if there is a validation error
-
setHostapd
(hostapd)[source]¶ Set the wifi conf file on the interface.
Raises: ValueError
– if there is a validation error
-
setHotplug
(hotplug)[source]¶ Set the option to allow hotplug on the interface. Beware, option is really called allow-hotplug, that’s a small historic cruft...
- Args:
- hotplug (bool): interface hotplug will be set if True
- Raises:
- ValueError: if there is a validation error
-
setName
(name)[source]¶ Set the name option of an interface.
Parameters: name (str) – the name of the interface Raises: ValueError
– if there is a validation error
-
setNameservers
(nameserver)[source]¶ Set the ipaddress of an interface.
Raises: ValueError
– if there is a validation error
-
setNetmask
(netmask)[source]¶ Set the netmask of an interface.
Parameters: netmask (str) – the netmask IP as a string Raises: ValueError
– if there is a validation error
-
setNetwork
(network)[source]¶ Set the network identifier of an interface.
Parameters: network (str) – the IP as a string Raises: ValueError
– if there is a validation error
-
setPostDown
(post)[source]¶ Set and add to the post-down commands for an interface.
Parameters: post (list) – list of shell commands
-
setPostUp
(post)[source]¶ Set and add to the post-up commands for an interface.
Parameters: post (list) – list of shell commands
-
setPreDown
(pre)[source]¶ Set and add to the pre-down commands for an interface.
Parameters: pre (list) – list of shell commands
-
setPreUp
(pre)[source]¶ Set and add to the pre-up commands for an interface.
Parameters: pre (list) – list of shell commands
-
setUnknown
(key, val)[source]¶ Stores uncommon options as there are with no special handling It’s impossible to know about all available options WARNING: duplicated directives are overwriten. TODO better
- Args:
- key (str): the option name val (any): the option value
-
setUp
(up)[source]¶ Set and add to the up commands for an interface.
Parameters: up (list) – list of shell commands
-
setWpaConf
(conf_path)[source]¶ Set the wpa supplicant configuration path for supplicant config for wireless interfaces.
Parameters: conf_path (str) – Path at which the supplicant config is located.
-
set_options
(options)[source]¶ Set options, either only the name if options is a str, or all given options if options is a dict
- Args:
- options (str/dict): historical code... only set
- the name if options is a str, or all given options if options is a dict
- Raises:
- ValueError: if validation error socket.error: if validation error of an IP Exception: if anything weird happens
-
validateAll
()[source]¶ Not thorough validations... and quick coded.
Raises: ValueError
– if there is a validation error
-
-
class
debinterface.
NetworkAdapterValidation
[source]¶ Bases:
object
Class to validate an adapter. It validates some options for: - presence - type - authorized values
-
validate_all
(if_attributes)[source]¶ Not thorough validations... and quick coded.
Parameters: if_attributes (dict) – the dict representation of the interface Raises: ValueError
– if there is a validation error
-
static
validate_broadcast_ip
(ip, opt)[source]¶ Validate an IP Address. Works for subnet masks too.
Parameters: - ip (str) – the IP as a string
- opt (str) – the opt name
Raises: ValueError
– on invalid IP
-
-
class
debinterface.
DnsmasqRange
(path, backup_path=None, leases_path='/var/tmp/dnsmasq.leases')[source]¶ Bases:
object
Basic dnsmasq conf of the more file which holds the ip ranges per interface. Made for handling very basic dhcp-range options
-
backup_path
= None¶
-
config
¶
-
rm_itf_range
(if_name)[source]¶ Rm range info for the given interface
Parameters: if_name (str) – interface name Returns: True if configuration was updated, False otherwise Return type: bool
-
update_range
(interface, start, end, lease_time)[source]¶ Update existing range based on the interface name If does not exist will be created
Parameters: - interface (str) – interface name
- start (str) – start ip of range
- end (str) – end ip of range
- lease_time (str) – lease_time
Returns: - True if configuration was updated or created,
False otherwise
Return type: bool
-
-
class
debinterface.
Hostapd
(path, backup_path=None)[source]¶ Bases:
object
basic hostapd conf file handling
-
backup_path
= None¶
-
config
¶
-
validate
()[source]¶ Not sure which ones are really necessary for everyone, here are the ones I require. I created 4 groups of keys : basic must always be there, wireless if you want to be an AP, auth if you want to add some security, bridge for, well, bridging Not foul proof !
Returns: True if everything went ok
Return type: bool
Raises: KeyError
– missing keyValueError
– invalid data
-
-
class
debinterface.
Interfaces
(update_adapters=True, interfaces_path='/etc/network/interfaces', backup_path=None, header_comment=None)[source]¶ Bases:
object
By default read interface file on init
Parameters: - update_adapters (bool, optional) – load adapters from interface file. Default True
- interfaces_path (str, optional) – default to /etc/network/interfaces
- backup_path (str, optional) – default to /etc/network/interfaces.bak
- header_comment (str, optional) – default to none, otherwise sets comments at the top of the interfaces file.
-
_set_paths
(interfaces_path, backup_path)[source]¶ Either use user input or defaults
Parameters: - interfaces_path (str) – path to interfaces file
- backup_path (str) – default to interfaces_path + .bak
-
adapters
¶
-
addAdapter
(options, index=None, interfaces_path=None)[source]¶ Insert a NetworkAdapter before the given index or at the end of the list. Options should be a string (name) or a dict
Parameters: - options (string or dict) – options to build a network adaptator
- index (integer, optional) – index to insert the NetworkAdapter
- interfaces_path (str, optional) – path to the source file of the adadpter.
Returns: the new adapter
Return type:
-
backup_path
¶
-
static
downAdapter
(if_name)[source]¶ Uses ifdown
Parameters: if_name (str) – the name of the interface Returns: True/False, command output. Return type: bool, str
-
getAdapter
(name)[source]¶ Find adapter by interface name
Parameters: name (str) – the name of the interface Returns: the new adapter or None if not found Return type: NetworkAdapter
-
header_comment
¶
-
interfaces_path
¶ Returns the path of the main interface file Others may come from the source directive. Adapters register the path
-
removeAdapter
(index)[source]¶ Remove the adapter at the given index. TODO: handle file deletion of sourced paths
- Args:
- index (int): the position of the adapter
-
removeAdapterByName
(name)[source]¶ Remove the adapter with the given name.
Parameters: name (str) – the name of the interface
-
class
debinterface.
InterfacesReader
(interfaces_path)[source]¶ Bases:
object
Short lived class to read interfaces file
-
_read_file
(current_path)[source]¶ Open up the given interfaces file. Read only. If a source directive is found, it will call itself recursively
-
_read_sourced_path
(line)[source]¶ Identify source path/to/file. If so will return the path to open it. Relative paths will be expanded from root path
- Returns:
- str: absolute path to the sourced file
-
adapters
¶
-
header_comments
¶
-
-
class
debinterface.
InterfacesWriter
(adapters, interfaces_path, backup_path=None, header_comment=None)[source]¶ Bases:
object
Short lived class to write interfaces file
if backup_path is None => no backup
-
_backup_interfaces
(adapters_paths)[source]¶ Backup interfaces file is the file exists
Returns: True/False, command output Raises: IOError
– if the copy fails and the source file exists
-
_check_interfaces
(interfaces_path)[source]¶ Uses ifup to check interfaces file. If it is not in the default place, each interface must be checked one by one.
Parameters: interfaces_path (string) – the path to interfaces file Raises: ValueError
– if invalid network interfaces
-
_restore_interfaces
(adapters_paths)[source]¶ Restore interfaces file is the file exists
Returns: True/False, command output Raises: IOError
– if the copy fails and the source file exists
-
_write_addrFam
(interfaces, adapter, ifAttributes)[source]¶ Construct and write the iface declaration. The addrFam clause needs a little more processing.
-
_write_callbacks
(interfaces, adapter, ifAttributes)[source]¶ Write the up, down, pre-up, pre-down, post-up, and post-down clauses.
-
_write_plugins
(interfaces, adapter, ifAttributes)[source]¶ Write plugins options, currently hostapd.
-
adapters
¶
-