Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Tes3awy committed Oct 15, 2021
1 parent bbc93e9 commit 1b15ef6
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 39 deletions.
5 changes: 3 additions & 2 deletions netmiko/example0.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"password": "C1sco12345",
"secret": "", # Optional (Default empty, Enable secret)
"port": 22, # Optional (Default to 22 for SSH and 23 for telnet)
"verbose": True, # Optional (Default False)
"verbose": True, # Optional (Default False) (Works with SSH only)
"session_log": "ex0-session.txt", # Optional (No default value)
"fast_cli": False, # (Multiplies delay factor by 0.1)
"global_delay_factor": 2, # Optional (Default to 1. 1 == 100 seconds)
"conn_timeout": 12, # Time duration to keep trying to connect to the device (seconds)
"conn_timeout": 20, # Time duration to keep trying to connect to the device (seconds)
"auth_timeout": 20, # Time duration to keep trying to auth to the device (seconds)
}

# Create an SSH connection instance (Traditional Method)
Expand Down
2 changes: 1 addition & 1 deletion netmiko/example4-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# Create an Excel sheet within the file
worksheet = workbook.add_worksheet(name="Device List")

worksheet.autofilter("A1:K1")
worksheet.autofilter("A1:L1")
worksheet.freeze_panes(1, 1)

# Create Header cell for each entry
Expand Down
36 changes: 19 additions & 17 deletions netmiko/example4-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# Create an Excel sheet within the file
worksheet = workbook.add_worksheet(name="Device List")

worksheet.autofilter("A1:K1")
worksheet.autofilter("A1:L1")
worksheet.freeze_panes(1, 1)

# Create Header cell for each entry
Expand All @@ -52,8 +52,10 @@
for cell, value in header.items():
worksheet.write(cell, value)

# Starting values for row and column in the Excel workbook
row, col = (1, 0)
# Starting values for row only in the Excel workbook
# Column will be indicated by its letter (A, B, C, etc) not index
# Since we are using Column letters, we need to start at 2 because A1, B1, C1, etc is the header
row = 2

# Loop over devices
for device in devices:
Expand All @@ -66,27 +68,27 @@
# Loop over each value in facts variable and insert each value
# in the corresponding cell according to the header above
for fact in facts:
worksheet.write(row, col + 0, fact["hostname"])
worksheet.write(row, col + 1, device["ip"]) # use IP from device variable
worksheet.write(row, col + 2, fact["serial"][0])
worksheet.write(f"A{row}", fact["hostname"])
worksheet.write(f"B{row}", device["ip"]) # use IP from device variable
worksheet.write(f"C{row}", fact["serial"][0])
# Try/except block to handle IndexError
try:
worksheet.write(row, col + 3, fact["mac"][0])
worksheet.write(f"D{row}", fact["mac"][0])
except IndexError:
# if device is a CSR router, then it has no MAC Address
worksheet.write(row, col + 3, "N/A")
worksheet.write(row, col + 4, fact["hardware"][0])
worksheet.write(row, col + 5, fact["version"])
worksheet.write(row, col + 6, fact["rommon"])
worksheet.write(f"D{row}", "N/A")
worksheet.write(f"E{row}", fact["hardware"][0])
worksheet.write(f"F{row}", fact["version"])
worksheet.write(f"G{row}", fact["rommon"])
# Checking if `bin` is in fact["running_image"]
if "bin" in fact["running_image"]:
worksheet.write(row, col + 7, "Bundle")
worksheet.write(f"H{row}", "Bundle")
else:
worksheet.write(row, col + 7, "Install")
worksheet.write(row, col + 8, fact["reload_reason"])
worksheet.write(row, col + 9, fact["restarted"])
worksheet.write(row, col + 10, fact["uptime"])
worksheet.write(row, col + 11, fact["config_register"])
worksheet.write(f"H{row}", "Install")
worksheet.write(f"I{row}", fact["reload_reason"])
worksheet.write(f"J{row}", fact["restarted"])
worksheet.write(f"K{row}", fact["uptime"])
worksheet.write(f"L{row}", fact["config_register"])
# Jump to next row
row += 1

Expand Down
9 changes: 4 additions & 5 deletions netmiko/example4-3.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
# Create an empty list to hold all dicts
output = []

# Command to send
cmd = "show version"

# Name of exported excel file
excel_file = "Example4-3-Inventory-Details-pandas.xlsx"

Expand All @@ -41,13 +38,15 @@
for device in devices:
# Create a connection instance to each device
with ConnectHandler(**device) as net_connect:
facts = net_connect.send_command(command_string=cmd, use_textfsm=True)
facts = net_connect.send_command(
command_string="show version", use_textfsm=True
)
# Append the show command output to the `output` empty list
output.append(facts[0])

# Create a data frame from the ouput list
df = (
pd.DataFrame(output)
pd.DataFrame(data=output)
.reindex( # to reorder the columns
columns=[
"hostname",
Expand Down
2 changes: 1 addition & 1 deletion netmiko/example4.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# Create an Excel sheet within the file
worksheet = workbook.add_worksheet(name="Device List")

worksheet.autofilter("A1:K1")
worksheet.autofilter("A1:L1")
worksheet.freeze_panes(1, 1)

# Create Header cell for each entry
Expand Down
4 changes: 2 additions & 2 deletions netmiko/example5-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
worksheet = workbook.add_worksheet(name=f"{hostname} Neighbors")

# Worksheet customizations
worksheet.autofilter("A1:H1")
worksheet.autofilter("A1:G1")
worksheet.freeze_panes(1, 1)

# Create Header line
Expand All @@ -68,7 +68,7 @@

# Check if cdp is enabled on that Cisco device
if "% CDP is not enabled" not in cdp_neighbors_detail:
print(f"Found cdp neighbors for {hostname}")
print(f"Found {len(cdp_neighbors_detail)} cdp neighbors for {hostname}")
# Loop over dicts in cdp neighbors detail list
for neighbor in cdp_neighbors_detail:
worksheet.write(row, col + 0, neighbor["local_port"])
Expand Down
4 changes: 0 additions & 4 deletions netmiko/example6-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

# Create an Excel file
with xlsxwriter.Workbook(filename="Example6-1-IP-Interface-Brief.xlsx") as workbook:
id = 0 # ID to differentiate devices of same hostname
# Iterate over devices
for device in devices:
# Create a connection instance to each device
Expand All @@ -43,9 +42,6 @@
delay_factor=2,
)

# Increment ID
id += 1

# Add worksheet with hostname of the device-ID
worksheet = workbook.add_worksheet(name=f"{hostname}-{id}")

Expand Down
13 changes: 8 additions & 5 deletions netmiko/example6.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
devices = []

# Read devices from device_list.csv CSV file
with open(file="device_list.csv", mode="r") as csvfile: # notice mode is r: read
with open(
file="device_list.csv", mode="rt", newline=""
) as csvfile: # notice mode is rt: read text
next(csvfile) # Use next() function to skip the header line in the csv file
# Each value is seperated based on the delimiter (comma (,))
device_list = csv.reader(csvfile, dialect="excel", delimiter=",")
Expand All @@ -57,14 +59,15 @@
"username": device[2],
"password": device[3],
"port": device[4],
"fast_cli": False,
}
)

# Starting values for row and column in the Excel sheets
# sheet 1
row1, col1 = 1, 0
row1, col1 = (1, 0)
# sheet 2
row2, col2 = 1, 0
row2, col2 = (1, 0)

# Loop over devices
for device in devices:
Expand All @@ -75,10 +78,10 @@
command_string="show version", use_textfsm=True
)[0]["hostname"]
inventory = net_connect.send_command(
command_string="show inventory", use_textfsm=True, delay_factor=3
command_string="show inventory", use_textfsm=True
)
intf_brief = net_connect.send_command(
command_string="show ip interface brief", use_textfsm=True, delay_factor=3
command_string="show ip interface brief", use_textfsm=True
)

# Pick only "Chassis" serial number and save in sheet 1
Expand Down
4 changes: 2 additions & 2 deletions netmiko/example9.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"ip": "sbx-nxos-mgmt.cisco.com",
"username": "admin",
"password": "Admin_1234!",
"secret": "", # Optional (Enable secret)
"secret": "", # Optional (Enable Password)
"port": 22, # Optional (Default to 22 for SSH)
"verbose": True, # Optional (Default False)
"session_log": "ex9-nxos-session.txt", # Optional (No default value)
"fast_cli": False,
"fast_cli": False, # Recommended (Default True)
"global_delay_factor": 2, # Optional (Default to 1)
}

Expand Down

0 comments on commit 1b15ef6

Please sign in to comment.