Tiffany's PYAOSCX looks like the way to go. But, if you're interested, I got netmiko to work for me.
#!/usr/bin/env python3
'''This module will house the function which can be called
to build a netmiko handler, and return it.
Brannen Taylor
9/8/22'''
#STEP 5
# Moving build_netmiko into main script of gg_lt_aruba_netmiko_mac_lookup_v2 - Brannen 9/19
def build_netmiko (os:str=None, ip:str=None, user:str=None, password:str=None):
'''Builds a netmiko connector, called "net_connect" and returns it.
Does not validate IP address. Does not retrieve credentials.'''
import logging
from netmiko import ConnectHandler
#Build a dictionary to pass into netmiko connection handler.
if os == 'cisco_ios':
device = {
'device_type':os,
'host':ip,
'username':user,
'password':password,
'port':22
}
elif os == 'ARUBA' or os == 'aruba_osswitch':
device = {
'device_type':'arba_ossuwitch',
'host':ip,
'username':user,
'password':password,
'port':22
}
else:
logging.warning(f"OS not found. net_dict not built")
device = None
#STEP 4
#Establish an SSH connection to the device by passing in the device dictionary.
net_connect = ConnectHandler(**device)
return net_connect
def main():
'''Main Function'''
pass
# Main
if __name__ == '__main__':
main()
Truncated code to log into a CX switch:
"""Author: Brannen Taylor - 9/21/22.'''
import logging
import macaddress # requires pip install macaddress.
import json
import os
import re
from dotenv import load_dotenv
from netmiko import ConnectHandler
#######################################################################
#STEP 5
def build_netmiko (os:str=None, ip:str=None, user:str=None, password:str=None):
'''Builds a netmiko connector, called "net_connect" and returns it.
Does not validate IP address. Does not retrieve credentials
see: http://ktbyers.github.io/netmiko/PLATFORMS.html
Parameters:\n
os - string - a netmiko string description of the os of the network device.\n
ip - string - an IPv4 address of the network device\n
user - string - a string for the username to log into the network device.\n
password - string - the password to log into the network device.
Return:\n
Returns a netmiko "connect handler" object.'''
# brought in function from 'fa_build_netmiko_connecthandler.py'
# Build a dictionary, to pass to the connect handler via **device argument (= **kwargs)
if os == 'ARUBA' or os == 'aruba_osswitch':
device = {
'device_type':'aruba_osswitch', # aruba_osswitch is a Supported SSH device_type values
'host':ip,
'username':user,
'password':password,
'port':22,
}
else:
logging.warning(f"OS not found. net_dict not built")
device = None
#STEP 4
#Establish an SSH connection to the device by passing in the device dictionary.
try:
net_connect = ConnectHandler(**device)
except Exception as e:
logger.error("LOGGER-ERROR Tried to load netmiko ConnectHandler at line ~93 and failed.\n{e}")
net_connect = None
return net_connect
#STEP 6,12
def send_to_network(net_connect, network_command):
'''Send a a command via netmiko net_connect to a network device.
Return the device output
Parameters:\n
net_connect - netmiko.connecthandler object - an established netmiko connection\n
network_command - str - the command to issue to the network device.
Return:\n
device_output - str - a string of the network device's output as a result of the network_command.'''
device_output = net_connect.send_command(network_command)
LOCAL and logger.debug(f"LOGGER-DEBUG Device output: {network_command} {device_output}")
return device_output
------------------------------
Brannen Taylor
LendingTree
Manager, Network Operations
------------------------------