-
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.
Merge pull request #876 from ktbyers/develop
Netmiko 2.2.0 Release
- Loading branch information
Showing
85 changed files
with
2,011 additions
and
114 deletions.
There are no files selected for viewing
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
106 changes: 106 additions & 0 deletions
106
examples/configuration_changes/config_changes_to_device_list.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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#!/usr/bin/env python | ||
# Author: Peter Bruno | ||
# Purpose: Script commands to group of Cisco devices with | ||
# success/failure feedback. | ||
from __future__ import print_function, unicode_literals | ||
import sys | ||
from netmiko import ConnectHandler | ||
from getpass import getpass | ||
|
||
|
||
def usage(ext): | ||
# exit with description and command line example | ||
print("\nInput file should contain list of switch IP addresses.") | ||
print("Commands should be the commands you wish to run on your") | ||
print('network devices enclosed in "quotes".') | ||
print( | ||
"Results key: # = enable mode, * = successful command", | ||
"w = write mem, ! = command failure" | ||
) | ||
print("\nusage:") | ||
print( | ||
("\n%s <input file>" % sys.argv[0]), | ||
'"command1"', | ||
'"command2"', | ||
'"command3"', | ||
"wr", | ||
) | ||
sys.exit(ext) | ||
|
||
|
||
def get_cmd_line(): | ||
if len(sys.argv) < 2: | ||
usage(0) | ||
cmdlist = sys.argv[2:] | ||
try: | ||
with open(sys.argv[1], "r") as f: | ||
switchip = f.read().splitlines() | ||
f.close() | ||
except (IndexError, IOError): | ||
usage(0) | ||
return switchip, cmdlist | ||
|
||
|
||
def main(): | ||
inputfile, config_commands = get_cmd_line() | ||
|
||
print("Switch configuration updater. Please provide login information.\n") | ||
# Get username and password information. | ||
username = input("Username: ") | ||
password = getpass("Password: ") | ||
enasecret = getpass("Enable Secret: ") | ||
|
||
print("{}{:<20}{:<40}{:<20}".format( | ||
"\n", "IP Address", "Name", "Results"), end="") | ||
|
||
for switchip in inputfile: | ||
ciscosw = { | ||
"device_type": "cisco_ios", | ||
"ip": switchip.strip(), | ||
"username": username.strip(), | ||
"password": password.strip(), | ||
"secret": enasecret.strip(), | ||
} | ||
print() | ||
print("{:<20}".format(switchip.strip()), end="", flush=True) | ||
try: | ||
# Connect to switch and enter enable mode. | ||
net_connect = ConnectHandler(**ciscosw) | ||
except Exception: | ||
print("** Failed to connect.", end="", flush=True) | ||
continue | ||
|
||
prompt = net_connect.find_prompt() | ||
# Print out the prompt/hostname of the device | ||
print("{:<40}".format(prompt), end="", flush=True) | ||
try: | ||
# Ensure we are in enable mode and can make changes. | ||
if "#" not in prompt[-1]: | ||
net_connect.enable() | ||
print("#", end="", flush=True) | ||
except Exception: | ||
print("Unable to enter enable mode.", end="", flush=True) | ||
continue | ||
|
||
else: | ||
for cmd in config_commands: | ||
# Make requested configuration changes. | ||
try: | ||
if cmd in ("w", "wr"): | ||
output = net_connect.save_config() | ||
print("w", end="", flush=True) | ||
else: | ||
output = net_connect.send_config_set(cmd) | ||
if "Invalid input" in output: | ||
# Unsupported command in this IOS version. | ||
print("Invalid input: ", cmd, end="", flush=True) | ||
print("*", end="", flush=True) | ||
except Exception: | ||
# Command failed! Stop processing further commands. | ||
print("!") | ||
break | ||
net_connect.disconnect() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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 @@ | ||
#!/usr/bin/env python | ||
from netmiko import Netmiko | ||
from getpass import getpass | ||
|
||
key_file = "/home/gituser/.ssh/test_rsa" | ||
|
||
cisco1 = { | ||
'device_type': 'cisco_ios', | ||
'host': 'cisco1.twb-tech.com', | ||
'username': 'testuser', | ||
'use_keys': True, | ||
'key_file': key_file, | ||
'ssh_config_file': './ssh_config', | ||
} | ||
|
||
net_connect = Netmiko(**cisco1) | ||
print(net_connect.find_prompt()) | ||
output = net_connect.send_command("show ip arp") | ||
print(output) | ||
|
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,7 @@ | ||
host jumphost | ||
IdentityFile ~/.ssh/test_rsa | ||
user gituser | ||
hostname 54.241.72.159 | ||
|
||
host * !jumphost | ||
ProxyCommand ssh jumphost nc %h %p |
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,18 @@ | ||
#!/usr/bin/env python | ||
from netmiko import Netmiko | ||
from getpass import getpass | ||
|
||
import logging | ||
logging.basicConfig(filename='test.log', level=logging.DEBUG) | ||
logger = logging.getLogger("netmiko") | ||
|
||
cisco1 = { | ||
'host': 'cisco1.twb-tech.com', | ||
'username': 'pyclass', | ||
'password': getpass(), | ||
'device_type': 'cisco_ios', | ||
} | ||
|
||
net_connect = Netmiko(**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,64 @@ | ||
DEBUG:paramiko.transport:starting thread (client mode): 0xb63946ec | ||
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.4.1 | ||
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-Cisco-1.25 | ||
INFO:paramiko.transport:Connected (version 2.0, client Cisco-1.25) | ||
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', '3des-cbc', 'aes192-cbc', 'aes256-cbc'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', '3des-cbc', 'aes192-cbc', 'aes256-cbc'] client mac:['hmac-sha1', 'hmac-sha1-96', 'hmac-md5', 'hmac-md5-96'] server mac:['hmac-sha1', 'hmac-sha1-96', 'hmac-md5', 'hmac-md5-96'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False | ||
DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group-exchange-sha1 | ||
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa | ||
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr | ||
DEBUG:paramiko.transport:MAC agreed: hmac-sha1 | ||
DEBUG:paramiko.transport:Compression agreed: none | ||
DEBUG:paramiko.transport:Got server p (2048 bits) | ||
DEBUG:paramiko.transport:kex engine KexGex specified hash_algo <built-in function openssl_sha1> | ||
DEBUG:paramiko.transport:Switch to new keys ... | ||
DEBUG:paramiko.transport:Adding ssh-rsa host key for cisco1.twb-tech.com: b'c77967d9e78b5c6d9acaaa55cc7ad897' | ||
DEBUG:paramiko.transport:userauth is OK | ||
INFO:paramiko.transport:Authentication (password) successful! | ||
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes | ||
DEBUG:paramiko.transport:[chan 0] Max packet out: 4096 bytes | ||
DEBUG:paramiko.transport:Secsh channel 0 opened. | ||
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok | ||
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok | ||
DEBUG:netmiko:read_channel: | ||
pynet-rtr1# | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:write_channel: b'\n' | ||
DEBUG:netmiko:read_channel: | ||
pynet-rtr1# | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:In disable_paging | ||
DEBUG:netmiko:Command: terminal length 0 | ||
|
||
DEBUG:netmiko:write_channel: b'terminal length 0\n' | ||
DEBUG:netmiko:Pattern is: pynet\-rtr1 | ||
DEBUG:netmiko:_read_channel_expect read_data: ter | ||
DEBUG:netmiko:_read_channel_expect read_data: minal length 0 | ||
pynet-rtr1# | ||
DEBUG:netmiko:Pattern found: pynet\-rtr1 terminal length 0 | ||
pynet-rtr1# | ||
DEBUG:netmiko:terminal length 0 | ||
pynet-rtr1# | ||
DEBUG:netmiko:Exiting disable_paging | ||
DEBUG:netmiko:write_channel: b'terminal width 511\n' | ||
DEBUG:netmiko:Pattern is: pynet\-rtr1 | ||
DEBUG:netmiko:_read_channel_expect read_data: t | ||
DEBUG:netmiko:_read_channel_expect read_data: erminal width 511 | ||
pynet-rtr1# | ||
DEBUG:netmiko:Pattern found: pynet\-rtr1 terminal width 511 | ||
pynet-rtr1# | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:write_channel: b'\n' | ||
DEBUG:netmiko:read_channel: | ||
pynet-rtr1# | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:write_channel: b'\n' | ||
DEBUG:netmiko:read_channel: | ||
pynet-rtr1# | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:read_channel: | ||
DEBUG:netmiko:exit_config_mode: | ||
DEBUG:netmiko:write_channel: b'exit\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,19 @@ | ||
#!/usr/bin/env python | ||
from netmiko import Netmiko | ||
from getpass import getpass | ||
import time | ||
|
||
cisco1 = { | ||
'host': 'cisco1.twb-tech.com', | ||
'username': 'pyclass', | ||
'password': getpass(), | ||
'device_type': 'cisco_ios', | ||
} | ||
|
||
net_connect = Netmiko(**cisco1) | ||
print(net_connect.find_prompt()) | ||
net_connect.write_channel("show ip int brief\n") | ||
time.sleep(1) | ||
output = net_connect.read_channel() | ||
print(output) | ||
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 Netmiko | ||
from getpass import getpass | ||
|
||
cisco1 = { | ||
'host': 'cisco1.twb-tech.com', | ||
'username': 'pyclass', | ||
'password': getpass(), | ||
'device_type': 'cisco_ios_telnet', | ||
} | ||
|
||
net_connect = Netmiko(**cisco1) | ||
print(net_connect.send_command("show ip arp")) | ||
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,55 @@ | ||
#!/usr/bin/env python | ||
import time | ||
from netmiko import ConnectHandler, redispatch | ||
|
||
net_connect = ConnectHandler( | ||
device_type='terminal_server', | ||
ip='10.10.10.10', | ||
username='admin', | ||
password='admin123', | ||
secret='secret123') | ||
|
||
# Manually handle interaction in the Terminal Server (fictional example, but | ||
# hopefully you see the pattern) | ||
net_connect.write_channel("\r\n") | ||
time.sleep(1) | ||
net_connect.write_channel("\r\n") | ||
time.sleep(1) | ||
output = net_connect.read_channel() | ||
# Should hopefully see the terminal server prompt | ||
print(output) | ||
|
||
# Login to end device from terminal server | ||
net_connect.write_channel("connect 1\r\n") | ||
time.sleep(1) | ||
|
||
# Manually handle the Username and Password | ||
max_loops = 10 | ||
i = 1 | ||
while i <= max_loops: | ||
output = net_connect.read_channel() | ||
|
||
if 'Username' in output: | ||
net_connect.write_channel(net_connect.username + '\r\n') | ||
time.sleep(1) | ||
output = net_connect.read_channel() | ||
|
||
# Search for password pattern / send password | ||
if 'Password' in output: | ||
net_connect.write_channel(net_connect.password + '\r\n') | ||
time.sleep(.5) | ||
output = net_connect.read_channel() | ||
# Did we successfully login | ||
if '>' in output or '#' in output: | ||
break | ||
|
||
net_connect.write_channel('\r\n') | ||
time.sleep(.5) | ||
i += 1 | ||
|
||
# We are now logged into the end device | ||
# Dynamically reset the class back to the proper Netmiko class | ||
redispatch(net_connect, device_type='cisco_ios') | ||
|
||
# Now just do your normal Netmiko operations | ||
new_output = net_connect.send_command("show ip int brief") |
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,27 @@ | ||
#!/usr/bin/env python | ||
from getpass import getpass | ||
from netmiko import ConnectHandler, file_transfer | ||
|
||
password = getpass() | ||
|
||
cisco = { | ||
'device_type': 'cisco_ios', | ||
'host': 'cisco1.twb-tech.com', | ||
'username': 'pyclass', | ||
'password': password, | ||
} | ||
|
||
source_file = 'test1.txt' | ||
dest_file = 'test1.txt' | ||
direction = 'put' | ||
file_system = 'flash:' | ||
|
||
ssh_conn = ConnectHandler(**cisco) | ||
transfer_dict = file_transfer(ssh_conn, | ||
source_file=source_file, | ||
dest_file=dest_file, | ||
file_system=file_system, | ||
direction=direction, | ||
overwrite_file=True) | ||
|
||
print(transfer_dict) |
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 @@ | ||
whatever |
Oops, something went wrong.