Netwalk package
Module contents
Main file for library
- class netwalk.Device(mgmt_address, **kwargs)
Bases:
object
Device type
- add_interface(intobject: netwalk.interface.Interface)
Add interface to device
- Parameters
intobject (netwalk.Interface) – Interface to add
- discovery_status: Optional[Union[str, datetime.datetime]]
- facts: dict
- hostname: str
- mgmt_address: Union[ipaddress.ip_address, str]
- promote_to_switch()
- class netwalk.Fabric
Bases:
object
Defines a fabric, i.e. a graph of connected Devices and their global mac address table
- _find_links()
Join switches by CDP neighborship
- _recalculate_macs()
Refresh count macs per interface. Tries to guess where mac addresses are by assigning them to the interface with the lowest total mac count
- add_device(switch: netwalk.device.Switch, credentials, napalm_optional_args=None, **kwargs)
Try to connect to, and if successful add to fabric, a new Device object
- Parameters
host (str) – IP or hostname of device to connect to
credentials (list(tuple(str,str))) – List of (username, password) tuples to try
napalm_optional_args (list(dict)) – Optional_args to pass to NAPALM, as many as you want
- discovery_status: Dict[str, Any]
Dictionary of {hostname: status} where status can be “Queued”, “Failed” or a datetime of when the discovery was completed. It is set and used by init_from_seed_device()
- find_paths(start_sw, end_sw)
Return a list of all interfaces from ‘start’ Switch to ‘end’ Switch
- Parameters
start_sw (netwalk.Switch) – Switch to begin search from
end_sw (netwalk.Switch) – List of target switch or switches
- init_from_seed_device(seed_hosts: str, credentials: list, napalm_optional_args=None, parallel_threads=1, neigh_validator_callback=None)
Initialise entire fabric from a seed device.
- Parameters
seed_hosts (str) – List of IP or hostname of seed devices
credentials (list) – List of (username, password) tuples to try
napalm_optional_args (list(dict(str, str)), optional) – Optional_args to pass to NAPALM for telnet
neigh_validator_callback (function) – Function accepting a Device object. Return True if device should be actively discovered
- logger: logging.Logger
- mac_table: Dict[netaddr.eui.EUI, dict]
Calculated global mac address table across all switches in the fabric. Generated by _recalculate_macs(). Dictionary of {netaddr.EUI (mac address object): attribute_dictionary}. Contains pointer to Interface object where the mac is.
- refresh_global_information()
Update global information such as mac address position and cdp neighbor adjacency
- switches: Dict[str, netwalk.device.Switch]
A dictionary of {hostname: Switch}
- class netwalk.Interface(**kwargs)
Bases:
object
Define an interface Can be initialised with any of the values or by passing an array containing each line of the interface configuration.
Converted to str it outputs the corresponding show running configuration. All unparsed lines go to “unparsed_lines” and are returned when converted to str
- _allowed_vlan_to_list(vlanlist: str) set
Expands vlan ranges
- Parameters
vlanlist (str) – String of vlans from config, i.e. 1,2,3-5
- Returns
Set of vlans
- Return type
set
- _calculate_sort_order() None
Generate unique sorting number from port id to sort interfaces meaningfully
- abort: Optional[str]
data from show interface
- add_child_interface(child_interface) None
Method to add interface to child interfaces and assign it a parent
- add_neighbor(neigh_int: netwalk.interface.Interface) None
Method to add bidirectional neighborship to an interface
- address: dict
- allowed_vlan: set
- bandwidth: Optional[str]
data from show interface
- bia: Optional[str]
data from show interface
- bpduguard: bool
- channel_group: Optional[int]
- channel_protocol: Optional[str]
- child_interfaces: List[netwalk.interface.Interface]
- config: List[str]
- counters: Optional[dict]
- crc: Optional[str]
data from show interface
- delay: Optional[str]
data from show interface
- description: Optional[str]
- duplex: Optional[str]
data from show interface
- encapsulation: Optional[str]
- generate_config(full=False) str
Generate show run from self data
- hardware_type: Optional[str]
data from show interface
- input_errors: Optional[str]
data from show interface
- input_packets: Optional[str]
data from show interface
- input_rate: Optional[str]
data from show interface
- is_enabled: bool
- is_up: bool
- last_clearing: Optional[datetime.datetime]
data from show interface
- last_in: Optional[datetime.datetime]
data from show interface
- last_out: Optional[datetime.datetime]
data from show interface
- last_out_hang: Optional[datetime.datetime]
data from show interface
- logger: logging.Logger
- mac_address: Optional[netaddr.eui.EUI]
- mac_count: int = 0
Total number of mac addresses behind this interface
- media_type: Optional[str]
data from show interface
- mode: str
- mtu: Optional[int]
data from show interface
- name: str
- native_vlan: int
- neighbors: List[Any]
List of neighbors connected to the interface. List because CDP might show more than one. If the neighbour is another Switch, it’s turned into the other end’s Interface object otherwise remains a dict containing all the parsed data.
- output_errors: Optional[str]
data from show interface
- output_packets: Optional[str]
data from show interface
- output_rate: Optional[str]
data from show interface
- parent_interface: Optional[netwalk.interface.Interface]
- parse_config(second_pass=False)
Parse configuration from show run
- protocol_status: Optional[str]
- queue_strategy: Optional[str]
data from show interface
- routed_port: bool
- sort_order: Optional[int]
- speed: Optional[str]
data from show interface
- type_edge: bool
- unparsed_lines: List[str]
- voice_vlan: Optional[int]
- vrf: str
- class netwalk.Switch(mgmt_address, **kwargs)
Bases:
netwalk.device.Device
Switch object to hold data Initialize with name and hostname, call retrieve_data() to connect to device and retrieve automaticlly or pass config as string to parse locally
- INTERFACE_FILTER = '^interface ([Pp]ort-channel|\\w*Ethernet|\\w*GigE|Vlan|Loopback).'
- INTERFACE_TYPES = '([Pp]ort-channel|\\w*Ethernet|\\w*GigE|Vlan|Loopback).'
- _cisco_time_to_dt(time: str) datetime.datetime
Converts time from now to absolute, starting when Switch object was initialised
- Parameters
time – Cisco diff time (e.g. ‘00:00:01’ or ‘5w4d’)
type – str
- Returns
Absolute time
- Return type
datetime.datetime
- _get_switch_data(whitelist: Optional[List[str]] = None, blacklist: Optional[List[str]] = None)
Get data from switch. If no argument is passed, scan all modules
- Parameters
whitelist (list(str)) – List of modules to scan, defaults to None
blacklist (list(str)) – List of modules to exclude from scan, defaults to None
Either whitelist or blacklist can be passed. If both are passed, whitelist takes precedence over blacklist.
Valid values are: - ‘mac_address’ - ‘interface_status’ - ‘cdp_neighbors’ - ‘lldp_neighbors’ - ‘vtp’ - ‘vlans’ - ‘l3_int’ - ‘local_admins’ - ‘inventory’
Running config is ALWAYS returned
- _parse_cdp_neighbors()
Ask for and parse CDP neighbors
- _parse_config()
Parse show run
- _parse_inventory()
- _parse_lldp_neighbors()
Ask for and parse LLDP neighbors
- _parse_show_interface()
Parse output of show inteface with greater data collection than napalm
- arp_table: Dict[ipaddress.IPv4Interface, dict]
- config: Optional[str]
Pass at init time to parse config automatically
- connect(username: str, password: str, napalm_optional_args: Optional[dict] = None) None
Connect to device
- Parameters
username (str) – username
password (str) – password
napalm_optional_args (dict, optional) – Check Napalm’s documentation about optional-args, defaults to None
- get_active_vlans()
Get active vlans from switch. Only lists vlans configured on ports
- Returns
[description]
- Return type
[type]
- hostname: str
- interfaces: Dict[str, netwalk.interface.Interface]
Dict of {name: Interface}
- interfaces_ip: dict
- inventory: List[Dict[str, Dict[str, str]]]
Time of object initialization. All timers will be calculated from it
- local_admins: Optional[Dict[str, dict]]
- logger: logging.Logger
- mac_table: dict
- napalm_optional_args: dict
- retrieve_data(username: str, password: str, napalm_optional_args: Optional[dict] = None, scan_options: Optional[dict] = None)
One-stop function to get data from switch.
- Parameters
username (str) – username
password (str) – password
napalm_optional_args (dict) – Refer to Napalm’s documentation
scan_options (dict(str, list(str))) – Valid keys are ‘whitelist’ and ‘blacklist’. Value must be a list of options to pass to _get_switch_data
- timeout: int
- vlans: Optional[Dict[int, dict]]
- vlans_set: set
- vtp: Optional[str]
Classes
netwalk.fabric module
- netwalk.fabric
alias of <module ‘netwalk.fabric’ from ‘/home/docs/checkouts/readthedocs.org/user_builds/netwalk/envs/latest/lib/python3.8/site-packages/netwalk-1.6.1-py3.8.egg/netwalk/fabric.py’>
netwalk.interface module
- netwalk.interface
alias of <module ‘netwalk.interface’ from ‘/home/docs/checkouts/readthedocs.org/user_builds/netwalk/envs/latest/lib/python3.8/site-packages/netwalk-1.6.1-py3.8.egg/netwalk/interface.py’>
netwalk.device module
- netwalk.device
alias of <module ‘netwalk.device’ from ‘/home/docs/checkouts/readthedocs.org/user_builds/netwalk/envs/latest/lib/python3.8/site-packages/netwalk-1.6.1-py3.8.egg/netwalk/device.py’>