def get_interfaces_details(): ''' Get details about all the interfaces on the minion :return: information about all interfaces omitting loopback :rtype: dictionary CLI Example: .. code-block:: bash salt '*' ip.get_interfaces_details ''' _interfaces = [ interface for interface in pyiface.getIfaces() if interface.flags & IFF_LOOPBACK == 0 ] if __grains__['lsb_distrib_id'] == 'nilrt': return {'interfaces': list(map(_get_interface_info, _interfaces))} # filter just the services _interfaces = [ interface for interface in _interfaces if _interface_to_service(interface.name) is not None ] return {'interfaces': list(map(_get_info, _interfaces))}
def set_dhcp_linklocal_all(interface): ''' Configure specified adapter to use DHCP with linklocal fallback Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot. :param str interface: interface label :return: True if the settings were applied, otherwise an exception will be thrown. :rtype: bool CLI Example: .. code-block:: bash salt '*' ip.set_dhcp_linklocal_all interface-label ''' if __grains__['lsb_distrib_id'] == 'nilrt': initial_mode = _get_adapter_mode_info(interface) _save_config(interface, 'Mode', 'TCPIP') _save_config(interface, 'dhcpenabled', '1') _save_config(interface, 'linklocalenabled', '1') if initial_mode == 'ethercat': __salt__['system.set_reboot_required_witnessed']() else: _restart(interface) return True if interface in [x.name for x in pyiface.getIfaces()]: return _change_dhcp_config(interface) raise salt.exceptions.CommandExecutionError('Invalid interface name: {0}'.format(interface))
def _change_state(interface, new_state): ''' Enable or disable an interface Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot. :param interface: interface label :param new_state: up or down :return: True if the service was enabled, otherwise an exception will be thrown. :rtype: bool ''' if __grains__['lsb_distrib_id'] == 'nilrt': return _change_state_legacy(interface, new_state) service = _interface_to_service(interface) if not service: if interface in map(lambda x: x.name, pyiface.getIfaces()): ret = _change_connman_backlist(interface, add=new_state == 'down') return ret and __salt__['cmd.run_all']( '/etc/init.d/connman restart')['retcode'] == 0 raise salt.exceptions.CommandExecutionError( 'Invalid interface name: {0}'.format(interface)) connected = _connected(service) if (not connected and new_state == 'up') or (connected and new_state == 'down'): service = pyconnman.ConnService(os.path.join(SERVICE_PATH, service)) try: state = service.connect( ) if new_state == 'up' else service.disconnect() _change_connman_backlist(interface, add=new_state == 'down') return state is None except Exception: raise salt.exceptions.CommandExecutionError( 'Couldn\'t {0} service: {1}\n'.format( 'enable' if new_state == 'up' else 'disable', service)) return True
def set_static_all(interface, address, netmask, gateway, nameservers): ''' Configure specified adapter to use ipv4 manual settings :param str interface: interface label :param str address: ipv4 address :param str netmask: ipv4 netmask :param str gateway: ipv4 gateway :param str nameservers: list of nameservers servers separated by spaces :return: True if the settings were applied, otherwise an exception will be thrown. :rtype: bool CLI Example: .. code-block:: bash salt '*' ip.set_static_all interface-label address netmask gateway nameservers ''' service = _interface_to_service(interface) if not service: raise salt.exceptions.CommandExecutionError( 'Invalid interface name: {0}'.format(interface)) validate, msg = _validate_ipv4([address, netmask, gateway]) if not validate: raise salt.exceptions.CommandExecutionError(msg) if nameservers: validate, msg = _space_delimited_list(nameservers) if not validate: raise salt.exceptions.CommandExecutionError(msg) if not isinstance(nameservers, list): nameservers = nameservers.split(' ') service = _interface_to_service(interface) if not service: if interface in pyiface.getIfaces(): return _configure_static_interface( interface, **{ 'ip': address, 'dns': ','.join(nameservers), 'netmask': netmask, 'gateway': gateway }) raise salt.exceptions.CommandExecutionError( 'Invalid interface name: {0}'.format(interface)) service = pyconnman.ConnService(_add_path(service)) ipv4 = service.get_property('IPv4.Configuration') ipv4['Method'] = dbus.String('manual', variant_level=1) ipv4['Address'] = dbus.String('{0}'.format(address), variant_level=1) ipv4['Netmask'] = dbus.String('{0}'.format(netmask), variant_level=1) ipv4['Gateway'] = dbus.String('{0}'.format(gateway), variant_level=1) try: service.set_property('IPv4.Configuration', ipv4) if nameservers: service.set_property( 'Nameservers.Configuration', [dbus.String('{0}'.format(d)) for d in nameservers]) except Exception as exc: raise salt.exceptions.CommandExecutionError( 'Couldn\'t set manual settings for service: {0}\nError: {1}\n'. format(service, exc)) return True
def set_static_all(interface, address, netmask, gateway, nameservers=None): ''' Configure specified adapter to use ipv4 manual settings Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot. :param str interface: interface label :param str address: ipv4 address :param str netmask: ipv4 netmask :param str gateway: ipv4 gateway :param str nameservers: list of nameservers servers separated by spaces (Optional) :return: True if the settings were applied, otherwise an exception will be thrown. :rtype: bool CLI Example: .. code-block:: bash salt '*' ip.set_static_all interface-label address netmask gateway nameservers ''' validate, msg = _validate_ipv4([address, netmask, gateway]) if not validate: raise salt.exceptions.CommandExecutionError(msg) if nameservers: validate, msg = _space_delimited_list(nameservers) if not validate: raise salt.exceptions.CommandExecutionError(msg) if not isinstance(nameservers, list): nameservers = nameservers.split(' ') if __grains__['lsb_distrib_id'] == 'nilrt': initial_mode = _get_adapter_mode_info(interface) _save_config(interface, 'Mode', 'TCPIP') _save_config(interface, 'dhcpenabled', '0') _save_config(interface, 'linklocalenabled', '0') _save_config(interface, 'IP_Address', address) _save_config(interface, 'Subnet_Mask', netmask) _save_config(interface, 'Gateway', gateway) if nameservers: _save_config(interface, 'DNS_Address', nameservers[0]) if initial_mode == 'ethercat': __salt__['system.set_reboot_required_witnessed']() else: _restart(interface) return True if interface in map(lambda x: x.name, pyiface.getIfaces()): ret_value = _configure_static_interface( interface, **{ 'ip': address, 'dns': ','.join(nameservers) if nameservers else '', 'netmask': netmask, 'gateway': gateway }) service = _interface_to_service(interface) if service: ret_value = ret_value and __salt__['cmd.run_all']( '/etc/init.d/connman restart')['retcode'] == 0 return ret_value raise salt.exceptions.CommandExecutionError( 'Invalid interface name: {0}'.format(interface))
def __interfaces(): ''' Return the list of all interfaces without loopback ''' return [ interface for interface in pyiface.getIfaces() if interface.flags & IFF_LOOPBACK == 0 ]
def set_static_all(interface, address, netmask, gateway, nameservers=None): """ Configure specified adapter to use ipv4 manual settings Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot. :param str interface: interface label :param str address: ipv4 address :param str netmask: ipv4 netmask :param str gateway: ipv4 gateway :param str nameservers: list of nameservers servers separated by spaces (Optional) :return: True if the settings were applied, otherwise an exception will be thrown. :rtype: bool CLI Example: .. code-block:: bash salt '*' ip.set_static_all interface-label address netmask gateway nameservers """ validate, msg = _validate_ipv4([address, netmask, gateway]) if not validate: raise salt.exceptions.CommandExecutionError(msg) if nameservers: validate, msg = _space_delimited_list(nameservers) if not validate: raise salt.exceptions.CommandExecutionError(msg) if not isinstance(nameservers, list): nameservers = nameservers.split(" ") if __grains__["lsb_distrib_id"] == "nilrt": initial_mode = _get_adapter_mode_info(interface) _save_config(interface, "Mode", "TCPIP") _save_config(interface, "dhcpenabled", "0") _save_config(interface, "linklocalenabled", "0") _save_config(interface, "IP_Address", address) _save_config(interface, "Subnet_Mask", netmask) _save_config(interface, "Gateway", gateway) if nameservers: _save_config(interface, "DNS_Address", nameservers[0]) if initial_mode == "ethercat": __salt__["system.set_reboot_required_witnessed"]() else: _restart(interface) return True if interface in [x.name for x in pyiface.getIfaces()]: return _configure_static_interface( interface, **{ "ip": address, "dns": ",".join(nameservers) if nameservers else "''", "netmask": netmask, "gateway": gateway, } ) raise salt.exceptions.CommandExecutionError( "Invalid interface name: {}".format(interface) )
def _change_state(interface, new_state): ''' Enable or disable an interface Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot. :param interface: interface label :param new_state: up or down :return: True if the service was enabled, otherwise an exception will be thrown. :rtype: bool ''' if __grains__['lsb_distrib_id'] == 'nilrt': return _change_state_legacy(interface, new_state) if interface in [x.name for x in pyiface.getIfaces()]: return _change_dhcp_config(interface) if new_state == 'up' else _change_dhcp_config(interface, False) raise salt.exceptions.CommandExecutionError('Invalid interface name: {0}'.format(interface))
def set_dhcp_linklocal_all(interface): ''' Configure specified adapter to use DHCP with linklocal fallback Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot. :param str interface: interface label :return: True if the settings were applied, otherwise an exception will be thrown. :rtype: bool CLI Example: .. code-block:: bash salt '*' ip.set_dhcp_linklocal_all interface-label ''' if __grains__['lsb_distrib_id'] == 'nilrt': initial_mode = _get_adapter_mode_info(interface) _save_config(interface, 'Mode', 'TCPIP') _save_config(interface, 'dhcpenabled', '1') _save_config(interface, 'linklocalenabled', '1') if initial_mode == 'ethercat': __salt__['system.set_reboot_required_witnessed']() else: _restart(interface) return True service = _interface_to_service(interface) if not service: if interface in map(lambda x: x.name, pyiface.getIfaces()): return _enable_dhcp(interface) raise salt.exceptions.CommandExecutionError( 'Invalid interface name: {0}'.format(interface)) service = pyconnman.ConnService(os.path.join(SERVICE_PATH, service)) ipv4 = service.get_property('IPv4.Configuration') ipv4['Method'] = dbus.String('dhcp', variant_level=1) ipv4['Address'] = dbus.String('', variant_level=1) ipv4['Netmask'] = dbus.String('', variant_level=1) ipv4['Gateway'] = dbus.String('', variant_level=1) try: service.set_property('IPv4.Configuration', ipv4) service.set_property('Nameservers.Configuration', ['']) # reset nameservers list except Exception as exc: exc_msg = 'Couldn\'t set dhcp linklocal for service: {0}\nError: {1}\n'.format( service, exc) raise salt.exceptions.CommandExecutionError(exc_msg) return True
def get_interfaces_details(): """ Get details about all the interfaces on the minion :return: information about all interfaces omitting loopback :rtype: dictionary CLI Example: .. code-block:: bash salt '*' ip.get_interfaces_details """ _interfaces = [ interface for interface in pyiface.getIfaces() if interface.flags & IFF_LOOPBACK == 0 ] if __grains__["lsb_distrib_id"] == "nilrt": return {"interfaces": list(map(_get_interface_info, _interfaces))} return {"interfaces": list(map(_get_info, _interfaces))}
def get_interfaces_details(): ''' Get details about all the interfaces on the minion :return: information about all connmans interfaces :rtype: dictionary CLI Example: .. code-block:: bash salt '*' ip.get_interfaces_details ''' interfaces = [] for interface in pyiface.getIfaces(): if interface.flags & IFF_LOOPBACK == 0: interfaces.append(_get_info(interface)) interfaceList = {'interfaces': interfaces} return interfaceList
def set_static_all(interface, address, netmask, gateway, nameservers): """ Configure specified adapter to use ipv4 manual settings Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot. :param str interface: interface label :param str address: ipv4 address :param str netmask: ipv4 netmask :param str gateway: ipv4 gateway :param str nameservers: list of nameservers servers separated by spaces :return: True if the settings were applied, otherwise an exception will be thrown. :rtype: bool CLI Example: .. code-block:: bash salt '*' ip.set_static_all interface-label address netmask gateway nameservers """ validate, msg = _validate_ipv4([address, netmask, gateway]) if not validate: raise salt.exceptions.CommandExecutionError(msg) validate, msg = _space_delimited_list(nameservers) if not validate: raise salt.exceptions.CommandExecutionError(msg) if not isinstance(nameservers, list): nameservers = nameservers.split(" ") if __grains__["lsb_distrib_id"] == "nilrt": initial_mode = _get_adapter_mode_info(interface) _save_config(interface, "Mode", "TCPIP") _save_config(interface, "dhcpenabled", "0") _save_config(interface, "linklocalenabled", "0") _save_config(interface, "IP_Address", address) _save_config(interface, "Subnet_Mask", netmask) _save_config(interface, "Gateway", gateway) if nameservers: _save_config(interface, "DNS_Address", nameservers[0]) if initial_mode == "ethercat": __salt__["system.set_reboot_required_witnessed"]() else: _restart(interface) return True service = _interface_to_service(interface) if not service: if interface in pyiface.getIfaces(): return _configure_static_interface( interface, **{ "ip": address, "dns": ",".join(nameservers), "netmask": netmask, "gateway": gateway, } ) raise salt.exceptions.CommandExecutionError( "Invalid interface name: {0}".format(interface) ) service = pyconnman.ConnService(os.path.join(SERVICE_PATH, service)) ipv4 = service.get_property("IPv4.Configuration") ipv4["Method"] = dbus.String("manual", variant_level=1) ipv4["Address"] = dbus.String("{0}".format(address), variant_level=1) ipv4["Netmask"] = dbus.String("{0}".format(netmask), variant_level=1) ipv4["Gateway"] = dbus.String("{0}".format(gateway), variant_level=1) try: service.set_property("IPv4.Configuration", ipv4) service.set_property( "Nameservers.Configuration", [dbus.String("{0}".format(d)) for d in nameservers], ) except Exception as exc: # pylint: disable=broad-except exc_msg = "Couldn't set manual settings for service: {0}\nError: {1}\n".format( service, exc ) raise salt.exceptions.CommandExecutionError(exc_msg) return True
def getInterfaces(): #Get all available network interfaces allIfaces = pyiface.getIfaces() for iface in allIfaces: print iface
def set_static_all(interface, address, netmask, gateway, nameservers): ''' Configure specified adapter to use ipv4 manual settings Change adapter mode to TCP/IP. If previous adapter mode was EtherCAT, the target will need reboot. :param str interface: interface label :param str address: ipv4 address :param str netmask: ipv4 netmask :param str gateway: ipv4 gateway :param str nameservers: list of nameservers servers separated by spaces :return: True if the settings were applied, otherwise an exception will be thrown. :rtype: bool CLI Example: .. code-block:: bash salt '*' ip.set_static_all interface-label address netmask gateway nameservers ''' validate, msg = _validate_ipv4([address, netmask, gateway]) if not validate: raise salt.exceptions.CommandExecutionError(msg) validate, msg = _space_delimited_list(nameservers) if not validate: raise salt.exceptions.CommandExecutionError(msg) if not isinstance(nameservers, list): nameservers = nameservers.split(' ') if __grains__['lsb_distrib_id'] == 'nilrt': initial_mode = _get_adapter_mode_info(interface) _save_config(interface, 'Mode', 'TCPIP') _save_config(interface, 'dhcpenabled', '0') _save_config(interface, 'linklocalenabled', '0') _save_config(interface, 'IP_Address', address) _save_config(interface, 'Subnet_Mask', netmask) _save_config(interface, 'Gateway', gateway) if nameservers: _save_config(interface, 'DNS_Address', nameservers[0]) if initial_mode == 'ethercat': __salt__['system.set_reboot_required_witnessed']() else: _restart(interface) return True service = _interface_to_service(interface) if not service: if interface in pyiface.getIfaces(): return _configure_static_interface( interface, **{ 'ip': address, 'dns': ','.join(nameservers), 'netmask': netmask, 'gateway': gateway }) raise salt.exceptions.CommandExecutionError( 'Invalid interface name: {0}'.format(interface)) service = pyconnman.ConnService(os.path.join(SERVICE_PATH, service)) ipv4 = service.get_property('IPv4.Configuration') ipv4['Method'] = dbus.String('manual', variant_level=1) ipv4['Address'] = dbus.String('{0}'.format(address), variant_level=1) ipv4['Netmask'] = dbus.String('{0}'.format(netmask), variant_level=1) ipv4['Gateway'] = dbus.String('{0}'.format(gateway), variant_level=1) try: service.set_property('IPv4.Configuration', ipv4) service.set_property( 'Nameservers.Configuration', [dbus.String('{0}'.format(d)) for d in nameservers]) except Exception as exc: exc_msg = 'Couldn\'t set manual settings for service: {0}\nError: {1}\n'.format( service, exc) raise salt.exceptions.CommandExecutionError(exc_msg) return True
#!/usr/bin/env python from pyiface import getIfaces, Interface, IFF_UP print 'All your interfaces' allIfaces = getIfaces() for iface in allIfaces: print iface iff = Interface(name='eth0') #iff.flags = iff.flags & IFF_UP print iff #iff.flags = iff.flags | IFF_UP | IFF_RUNNING #iff.addr = (socket.AF_INET, sys.argv[1]) #print iff #iff.netmask = (socket.AF_INET, sys.argv[2]) #iff.flags = iff.flags | IFF_UP #print iff #iff.flags = iff.flags & ~IFF_UP #print iff
def get_macvlans(): a = pyiface.getIfaces() return [i for i in a if "mac" in i.name]