Skip to content

Commit

Permalink
Add cisco_ios support.
Browse files Browse the repository at this point in the history
  • Loading branch information
redeuxx committed Mar 13, 2024
1 parent cd5e1ca commit efdaeb9
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
17 changes: 14 additions & 3 deletions get_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import db


def get_config(device_username, device_password, device_ip, device_type):
def get_config(
device_username, device_password, device_ip, device_type, enable_password
):
"""
Get the running configuration of a device.
Expand All @@ -15,14 +17,19 @@ def get_config(device_username, device_password, device_ip, device_type):
device_password: The password to authenticate with.
device_ip: The IP address of the device.
device_type: The device parameters.
enable_password: The enable password to authenticate with.
Returns:
The configuration of the device.
"""

# Import module named device_type, pass device type to get_running_config, return to string
config = __import__(f"vendors.{device_type}", fromlist=[""]).get_running_config(
device_username, device_password, device_ip, device_type
device_username,
device_password,
device_ip,
device_type,
enable_password,
)

return config
Expand All @@ -41,7 +48,11 @@ def fetch_all_configs():

for device in db.list_all_ips_with_type():
config = get_config(
device.username, device.password, device.ip, device.device_type
device.username,
device.password,
device.ip,
device.device_type,
device.enable_password,
)

print(f"Fetching config for {device.ip} ...")
Expand Down
2 changes: 1 addition & 1 deletion get_hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_hostname(device_ip, device_type, username, password, enable_password):

# Import module named device_type, pass device type to get_running_config, return to string
hostname = __import__(f"vendors.{device_type}", fromlist=[""]).get_hostname(
my_secrets.USERNAME, my_secrets.PASSWORD, device_ip, device_type
username, password, device_ip, device_type, enable_password
)

# Return hostname
Expand Down
2 changes: 1 addition & 1 deletion vendors/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ["hp_procurve"]
__all__ = ["hp_procurve", "cisco_ios"]
89 changes: 89 additions & 0 deletions vendors/cisco_ios.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import netmiko
import get_config
import re


def get_running_config(
device_username, device_password, device_ip, device_type, enable_password
):
"""
Get the command to get the running config.
Args:
device_username: The username to authenticate with.
device_password: The password to authenticate with.
device_ip: The IP address of the device.
device_type: The device parameters.
enable_password: The enable password to authenticate with.
Returns:
String: The running configuration of the device.
String: The error message if the device could not be connected to.
"""

try:
device = {
"device_type": device_type,
"host": device_ip,
"username": device_username,
"password": device_password,
# wait a little longer for the device to respond
"fast_cli": False,
"global_delay_factor": 2,
"secret": enable_password,
}

with netmiko.ConnectHandler(**device) as connection:
# send enable command
output = connection.send_command("enable", expect_string="Password:")

if "Password:" in output:
connection.send_command_timing(device["secret"])

# Get the running configuration
config = connection.send_command("show running-config")

# Close the connection
connection.disconnect()

# Return the configuration
return config
except Exception as e:
return (
f"Error: Could not connect to {device_ip} with the following error: \n{e}"
)


def get_hostname(
device_username, device_password, device_ip, device_type, enable_password
):
"""
Get the hostname of a device.
Args:
device_username: The username to authenticate with.
device_password: The password to authenticate with.
device_ip: The IP address of the device.
device_type: The device parameters.
enable_password: The enable password to authenticate with.
Returns:
String: The hostname of the device.
"""
# Get the running configuration

device = {
"device_type": device_type,
"host": device_ip,
"username": device_username,
"password": device_password,
# wait a little longer for the device to respond
"fast_cli": False,
"global_delay_factor": 2,
"secret": enable_password,
}
# Get the hostname
with netmiko.ConnectHandler(**device) as connection:
prompt = connection.find_prompt()
prompt_index = prompt.find(">") # get index of > in prompt
return prompt[:prompt_index] # return hostname from prompt
6 changes: 5 additions & 1 deletion vendors/hp_procurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import re


def get_running_config(device_username, device_password, device_ip, device_type):
def get_running_config(
device_username, device_password, device_ip, device_type, enable_password
):
"""
Get the command to get the running config.
Expand All @@ -12,6 +14,7 @@ def get_running_config(device_username, device_password, device_ip, device_type)
device_password: The password to authenticate with.
device_ip: The IP address of the device.
device_type: The device parameters.
enable_password: The enable password to authenticate with.
Returns:
String: The running configuration of the device.
Expand All @@ -27,6 +30,7 @@ def get_running_config(device_username, device_password, device_ip, device_type)
# wait a little longer for the device to respond
"fast_cli": False,
"global_delay_factor": 2,
"secret": enable_password,
}

with netmiko.ConnectHandler(**device) as connection:
Expand Down

0 comments on commit efdaeb9

Please sign in to comment.