-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved documentation examples (#1816)
- Loading branch information
Showing
86 changed files
with
1,797 additions
and
445 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import sys | ||
from getpass import getpass | ||
from netmiko.snmp_autodetect import SNMPDetect | ||
from netmiko import ConnectHandler | ||
|
||
host = "cisco1.lasthop.io" | ||
device = {"host": host, "username": "pyclass", "password": getpass()} | ||
|
||
snmp_community = getpass("Enter SNMP community: ") | ||
my_snmp = SNMPDetect(host, snmp_version="v2c", community=snmp_community) | ||
device_type = my_snmp.autodetect() | ||
print(device_type) | ||
|
||
if device_type is None: | ||
sys.exit("SNMP failed!") | ||
|
||
# Update the device dictionary with the device_type and connect | ||
device["device_type"] = device_type | ||
with ConnectHandler(**device) as net_connect: | ||
print(net_connect.find_prompt()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
device = { | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": getpass(), | ||
} | ||
|
||
commands = ["logging buffered 100000"] | ||
with ConnectHandler(**device) as net_connect: | ||
output = net_connect.send_config_set(commands) | ||
output += net_connect.save_config() | ||
|
||
print() | ||
print(output) | ||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
device1 = { | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": getpass(), | ||
} | ||
|
||
# File in same directory as script that contains | ||
# | ||
# $ cat config_changes.txt | ||
# -------------- | ||
# logging buffered 100000 | ||
# no logging console | ||
|
||
cfg_file = "config_changes.txt" | ||
with ConnectHandler(**device1) as net_connect: | ||
output = net_connect.send_config_from_file(cfg_file) | ||
output += net_connect.save_config() | ||
|
||
print() | ||
print(output) | ||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
logging buffered 100000 | ||
no logging console |
21 changes: 11 additions & 10 deletions
21
...se3_multiple_devices/conn_multiple_dev.py → examples/conn_multiple_dev.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
#!/usr/bin/env python | ||
from netmiko import Netmiko | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
password = getpass() | ||
|
||
cisco1 = { | ||
"host": "cisco1.twb-tech.com", | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": password, | ||
"device_type": "cisco_ios", | ||
} | ||
|
||
cisco2 = { | ||
"host": "cisco2.twb-tech.com", | ||
"device_type": "cisco_ios", | ||
"host": "cisco2.lasthop.io", | ||
"username": "pyclass", | ||
"password": password, | ||
"device_type": "cisco_ios", | ||
} | ||
|
||
nxos1 = { | ||
"host": "nxos1.twb-tech.com", | ||
"device_type": "cisco_nxos", | ||
"host": "nxos1.lasthop.io", | ||
"username": "pyclass", | ||
"password": password, | ||
"device_type": "cisco_nxos", | ||
} | ||
|
||
srx1 = { | ||
"host": "srx1.twb-tech.com", | ||
"device_type": "juniper_junos", | ||
"host": "srx1.lasthop.io", | ||
"username": "pyclass", | ||
"password": password, | ||
"device_type": "juniper_junos", | ||
} | ||
|
||
for device in (cisco1, cisco2, nxos1, srx1): | ||
net_connect = Netmiko(**device) | ||
net_connect = ConnectHandler(**device) | ||
print(net_connect.find_prompt()) | ||
net_connect.disconnect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
|
||
key_file = "~/.ssh/test_rsa" | ||
cisco1 = { | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "testuser", | ||
"use_keys": True, | ||
"key_file": key_file, | ||
} | ||
|
||
with ConnectHandler(**cisco1) as net_connect: | ||
output = net_connect.send_command("show ip arp") | ||
|
||
print(f"\n{output}\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
|
||
cisco1 = { | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": getpass(), | ||
"ssh_config_file": "~/.ssh/ssh_config", | ||
} | ||
|
||
with ConnectHandler(**cisco1) as net_connect: | ||
output = net_connect.send_command("show users") | ||
|
||
print(f"\n{output}\n") |
6 changes: 3 additions & 3 deletions
6
..._cases/case2_using_dict/conn_with_dict.py → examples/conn_with_dict.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
#!/usr/bin/env python | ||
from netmiko import Netmiko | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
cisco1 = { | ||
"host": "cisco1.twb-tech.com", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": getpass(), | ||
"device_type": "cisco_ios", | ||
} | ||
|
||
net_connect = Netmiko(**cisco1) | ||
net_connect = ConnectHandler(**cisco1) | ||
print(net_connect.find_prompt()) | ||
net_connect.disconnect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
cisco1 = { | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": getpass(), | ||
} | ||
|
||
# Will automatically 'disconnect()' | ||
with ConnectHandler(**cisco1) as net_connect: | ||
print(net_connect.find_prompt()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
from datetime import datetime | ||
|
||
cisco1 = { | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": getpass(), | ||
} | ||
|
||
command = "copy flash:c880data-universalk9-mz.155-3.M8.bin flash:test1.bin" | ||
|
||
# Start clock | ||
start_time = datetime.now() | ||
|
||
net_connect = ConnectHandler(**cisco1) | ||
|
||
# Netmiko normally allows 100 seconds for send_command to complete | ||
# delay_factor=4 would allow 400 seconds. | ||
output = net_connect.send_command_timing( | ||
command, strip_prompt=False, strip_command=False, delay_factor=4 | ||
) | ||
if "Destination filename" in output: | ||
print("Starting copy...") | ||
output += net_connect.send_command("\n", delay_factor=4, expect_string=r"#") | ||
net_connect.disconnect() | ||
|
||
end_time = datetime.now() | ||
print(f"\n{output}\n") | ||
print("done") | ||
print(f"Execution time: {start_time - end_time}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
password = getpass() | ||
secret = getpass("Enter secret: ") | ||
|
||
cisco1 = { | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": password, | ||
"secret": secret, | ||
} | ||
|
||
net_connect = ConnectHandler(**cisco1) | ||
# Call 'enable()' method to elevate privileges | ||
net_connect.enable() | ||
print(net_connect.find_prompt()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
cisco1 = { | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": getpass(), | ||
# Multiple all of the delays by a factor of two | ||
"global_delay_factor": 2, | ||
} | ||
|
||
command = "show ip arp" | ||
net_connect = ConnectHandler(**cisco1) | ||
output = net_connect.send_command(command) | ||
net_connect.disconnect() | ||
|
||
print(f"\n{output}\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env python | ||
from netmiko import ConnectHandler | ||
|
||
cisco1 = { | ||
# Just pick an 'invalid' device_type | ||
"device_type": "invalid", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": "invalid", | ||
} | ||
|
||
net_connect = ConnectHandler(**cisco1) | ||
net_connect.disconnect() |
10 changes: 6 additions & 4 deletions
10
...cases/case11_logging/conn_with_logging.py → examples/netmiko_logging.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
#!/usr/bin/env python | ||
from netmiko import Netmiko | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
# Logging section ############## | ||
import logging | ||
|
||
logging.basicConfig(filename="test.log", level=logging.DEBUG) | ||
logger = logging.getLogger("netmiko") | ||
# Logging section ############## | ||
|
||
cisco1 = { | ||
"host": "cisco1.twb-tech.com", | ||
"device_type": "cisco_ios", | ||
"host": "cisco1.lasthop.io", | ||
"username": "pyclass", | ||
"password": getpass(), | ||
"device_type": "cisco_ios", | ||
} | ||
|
||
net_connect = Netmiko(**cisco1) | ||
net_connect = ConnectHandler(**cisco1) | ||
print(net_connect.find_prompt()) | ||
net_connect.disconnect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.