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-greenfructose-0.0.3.tar.gz
.
File metadata
- Download URL: net-switch-tools-greenfructose-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 | 0019cfd8b4dc676402f0852712e0dfd2a9d42782f946693f1b4331e57f796d16 |
|
MD5 | 83ebb205f699e432ecb466d10e792ad1 |
|
BLAKE2b-256 | 09299d7007e0b38fd69ccfec484c7f745a91ed6802988de5c86d86de09e5a2ca |
Provenance
File details
Details for the file net_switch_tools_greenfructose-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: net_switch_tools_greenfructose-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.0 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 | 4c46fea9e733fb05f2b6fb11ff5a2232f0c4147ce2e2834da42ba6d02f19a0eb |
|
MD5 | a460d6d5305c5bf1b3c84437b0eccd1e |
|
BLAKE2b-256 | bb18c184bc8b1fcfc5098e277d453c9019d332a9982707a5b61b7d836bbba7b5 |