A package for managing network switches
Project description
net-switch-tools
Python package for managing network switches
Usage
import manage_switches.All
# Connection info for logging in to switch
coninfo = {
'ip': 'IP_OF_SWITCH',
'device_type': 'TYPE_OF_DEVICE', # Refer to NetMiko docs for switch type
'username': 'SWITCH_USERNAME',
'password': 'SWITCH_PASSWORD'
}
# Get list of IPs in a CIDR network range
ip_list = generate_ip_list('192.168.1.0/24')
# Get hostname from IP address, or returns 'Hostname not found' if no hostname
hostname = get_hostname_by_ip('192.168.1.5')
# Ping list of IP addresses from switch
ping_from_switch(coninfo, ip_list)
# Run commands on switch. Writes output to D 'switch_{command_slug}/{switch_ip}
run_commands(coninfo, ['show arp', 'show mac-address'])
# Run functions concurrently. Supports functions that take IP address as an arg
multithread(get_hostname_by_ip, ip_list)
# Write results of command CSV, saves file as {prepend}-{variable_name}.csv
result = [
{'IP': '192.168.1.1', 'Hostname': 'router.local'},
{'IP': '192.168.1.2', 'Hostname': 'computer1'},
{'IP': '192.168.1.1', 'Hostname': 'computer2'},
{'IP': '192.168.1.1', 'Hostname': 'computer3'},
]
write_result(result, 'w+', 'switch_ip')
# Reformat MAC address to a1-a1-a1-a1-a1-a1 format
fixed_mac = reformat_mac('a1:a1:a1:a1:a1:a1')
Example
import sys
import manage_switches.All
from secret import IP_RANGE, USERNAME, PASSWORD, DEVICE_TYPE
def write_arp_tables(ip: str):
"""
Retrieves ARP table from switch. Writes remote device IP, remote device
MAC, remote device hostname, switch ip, and switch port the remote
device is connected too into a CSV file.
:param ip: IP address of switch as string
:return: Success string 'Success on {ip}'
"""
spinner = Halo(spinner='dots')
try:
coninfo = {
'ip': ip,
'device_type': DEVICE_TYPE,
'username': USERNAME,
'password': PASSWORD
}
commands = ['show arp']
run_commands(coninfo, commands)
arp_list = []
spinner.start(f'\nGetting ARP table from switch at {ip}')
with open(f'switch_show_arp/{ip}', 'r') as f:
raw_arp_table = f.read()
spinner.succeed()
spinner.stop()
spinner.start(f'\nFormatting ARP table and writing to file.')
fixed_arp_list = [x.strip() for x in raw_arp_table.split("\n")[6:-2]]
# You'll need to figure this out for your specific switch output
for item in fixed_arp_list:
item = item.replace(' ', ' ')
.replace(' ', ' ')
.replace(' ', ' ')
.replace(' ', ' ')
item = item.split(' ')
if len(item) > 3:
arp_list.append({
'IP': item[0],
'MAC': reformat_mac(item[1]),
'Hostname': get_hostname_by_ip(item[0]),
'Switch IP': ip,
'Switch Port': item[3]
})
write_result_csv(arp_list, 'a+', prepend=ip)
spinner.succeed(f'\nFile written to {ip}-srp_list.csv')
spinner.stop()
return f'Success on {ip}'
except (KeyboardInterrupt, SystemExit):
spinner.stop()
def populate_arp_table(ip: str):
"""
Logs into switch, pings every address in IP_RANGE to populate ARP table
:param ip: IP address of switch as string
:return: Success string 'Success on {ip}'
"""
try:
coninfo = {
'ip': ip,
'device_type': DEVICE_TYPE,
'username': USERNAME,
'password': PASSWORD
}
ip_list = generate_ip_list(IP_RANGE)
ping_from_switch(coninfo, ip_list)
return f'Success on {ip}'
except (KeyboardInterrupt, SystemExit):
sys.exit()
if __name__ == '__main__':
switch_ips = []
ip_list = generate_ip_list(IP_RANGE)
with open('SwitchAddresses.csv', 'r') as f:
for row in csv.reader(f):
switch_ips.append(row[0])
multithread(populate_arp_table, switch_ips)
multithread(write_arp_tables, switch_ips)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file net-switch-tools-0.0.3.tar.gz
.
File metadata
- Download URL: net-switch-tools-0.0.3.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83615e662fca7d377541e18ec966e5886c6f3cbedf8419c1304a85e9732202bc |
|
MD5 | fcc20e3e225056706aca694024d73e10 |
|
BLAKE2b-256 | 3a83078f4b04b0c495ad15ea75ec2aad2b4110c3524e0a93e549a2834b8987f5 |
Provenance
File details
Details for the file net_switch_tools-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: net_switch_tools-0.0.3-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86a9d7e8d9065d198339926e3c509fbaf8c41c3fb3707c83e93ec13d92b06574 |
|
MD5 | a1f3abb9f6a365918ff83044408dc11c |
|
BLAKE2b-256 | fc7a446c65754ce7cc4a8dfe481bc61f2c8138410083186de0e7f054e2e955f1 |