Python for Network Engineers
ktbyers / pynet Goto Github PK
View Code? Open in Web Editor NEWPython for Network Engineers
License: Apache License 2.0
Python for Network Engineers
License: Apache License 2.0
Python for Network Engineers
Hi Kirk,
In the solution for this assignment, when I use the join method, the result is not as I expect it to be. What am I missing?
a
'FE80:0000:0000:0000:0101:A3EF:EE1E:1719'
a.split(":")
['FE80', '0000', '0000', '0000', '0101', 'A3EF', 'EE1E', '1719']
":".join(a)
'F:E:8:0:::0:0:0:0:::0:0:0:0:::0:0:0:0:::0:1:0:1:::A:3:E:F:::E:E:1:E:::1:7:1:9'
Hi Kirk,
I tried to run this script "snmp_helper.py" in https://github.com/ktbyers/pynet/tree/master/snmp But I import error. Your script says "Requires the pysnmp4 library" but I see it is already there.
Do you have any idea what is missing?
mathew@Tools-SRV:~$ python2
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from snmp_helper import snmp_get_oid_v3,snmp_extract
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named snmp_helper
>>> quit()
mathew@Tools-SRV:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from snmp_helper import snmp_get_oid,snmp_extract
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'snmp_helper'
>>>
root@Tools-SRV:/home/mathew# pip3 install pysnmp
Requirement already satisfied: pysnmp in /usr/local/lib/python3.5/dist-packages (4.3.9)
Requirement already satisfied: pycryptodome in /usr/local/lib/python3.5/dist-packages (from pysnmp) (3.4.7)
Requirement already satisfied: pysmi in /usr/local/lib/python3.5/dist-packages (from pysnmp) (0.0.7)
Requirement already satisfied: pyasn1>=0.2.3 in /usr/local/lib/python3.5/dist-packages (from pysnmp) (0.4.2)
Requirement already satisfied: ply in /usr/local/lib/python3.5/dist-packages (from pysmi->pysnmp) (3.9)
root@Tools-SRV:/home/mathew# pip3 install snmp_helper
Collecting snmp_helper
Could not find a version that satisfies the requirement snmp_helper (from versions: )
No matching distribution found for snmp_helper
root@Tools-SRV:/home/mathew#
Hi @ktbyers
I have discoverd that delay_factor in last netmiko version (3.2.2) is not working
I downgraded netmiko to 3.1.1, paramiko to 2.7.1 and now I have no issue with delay_factor
Hi Kirk,
The file transfer of a nxos image (e.g.: I7(6).bin) takes longer than expected and it fails with following error:
paramiko.ssh_exception.SSHException: Key-exchange timed out waiting for key negotiation
Below is complete error :
Also are you aware of a way to track the progress of the file transfer as a background task, and show them the UI in django?
I am not able to get the file transfer working with a user that doesn't have privilege 15. I set the "secret: password" in the groups.yaml file and tested that it works on a 'netmiko_send_config'.
I am new to programming but tried a few things but still no luck.
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_file_transfer, netmiko_send_config, netmiko_send_command
from nornir_utilities import nornir_set_creds, std_print
import time
# Initialize Nornir object using default "SimpleInventory" plugin
nr = InitNornir()
# nornir_set_creds(nr)
host = nr.filter(hostname='172.30.1.1')
test_file = 'test_xfer.txt'
startTime = time.time()
result = host.run(task=netmiko_file_transfer,
source_file=test_file,
dest_file=test_file,
direction='put',
)
endTime = time.time()
print(f"Total time : {endTime - startTime} \n")
std_print(result)
\venv\lib\site-packages\netmiko\cisco_base_connection.py", line 186, in _autodetect_fs
raise ValueError("Must be in enable mode to auto-detect the file-system.")
ValueError: Must be in enable mode to auto-detect the file-system.
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_file_transfer, netmiko_send_config, netmiko_send_command
from nornir_utilities import nornir_set_creds, std_print
import time
# Initialize Nornir object using default "SimpleInventory" plugin
nr = InitNornir()
# nornir_set_creds(nr)
host = nr.filter(hostname='172.30.1.1')
test_file = 'test_xfer.txt'
startTime = time.time()
def testup(task):
task.run(task=netmiko_send_command,
enable=True,
command_string="sh ip int b"
)
task.run(task=netmiko_file_transfer,
source_file=test_file,
dest_file=test_file,
direction='put',
)
result = host.run(task=testup)
endTime = time.time()
print(f"Total time : {endTime - startTime} \n")
std_print(result)
\NornirGudeTest\venv\lib\site-packages\scp.py", line 363, in _recv_confirm
raise SCPException(asunicode(msg[1:]))
scp.SCPException: Privilege denied.
I also tried editing the netmiko_file_transfer.py by adding the line below. which result in a Privilege denied.
net_connect.enable()
Hi Kirk,
When i use the netmiko_file_transfer module, I am getting the following error:
OSError: Search pattern never detected in send_command_expect: [\$#]
Nornir Code:
nr_arista = nr.filter(F(hostname__in=device_urlArray))
nornir_set_creds(nr_arista ,fabricList,device_urlArray)
source_file = project_path + "/media/" + BinFile
result = nr_arista.run(
task=netmiko_file_transfer,
source_file=source_file,
dest_file=BinFile,
direction='put',
num_workers=len(NodeSwitch_tmp),
)
Hi Eric ,
I read your book Python network automation hence thought to get an help on below issue.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
I am new in script world and have a requirement to automate network device. Please refer below connection and advise suitable scripts.
Linux(Machine)----->>>>>>>(RSA Token enabled)Linux(Jump Server)------>>>>>Device(Cisco)
Below is the code which gives me error.
from netmiko import ConnectHandler
RSA_PASSCODE = raw_input("Enter RSA passcode:")
Jump_server = {
'device_type': 'linux',
'ip': '10.10.10.227',
'username': 'cisco',
'password': 'cisco',
'passcode':'RSA_PASSCODE'
}
net_connect = ConnectHandler(**Jump_server)
net_connect.find_prompt()
output = net_connect.send_command("ls -l)
print output
I am following the post "Using Nornir for OS Upgrades (Part 2)" to upgrade a switch but at the section of the code to verify the boot variable, I don't get a print out on screen. I tried a couple of things to troubleshoot but haven't figured out the issue. one of my problems is I don't know how to debug :(. As for the testing I've done, I tested that section of the code by itself and it worked fine. i also move it above the set_boot_var task and that worked. so it seems to me something in the set_boot_var function is causing netmiko_send_command to not return a value. interestingly even though it doesn't return a value it still performs the command. i am using the script "nornir_os_upgrade.py" with few adjustments.
def main():
print("************** Starting File Transfer *********************")
# Initialize Nornir object using default "SimpleInventory" plugin
nr = InitNornir().filter(hostname="172.30.14.5")
result = nr.run(
task=os_upgrade,
num_workers=20,
)
std_print(result)
# Filter to only a single device
nr_ios = nr.filter(hostname="172.30.14.5")
aggr_result = nr_ios.run(task=set_boot_var)
# If setting the boot variable failed (assumes single device at this point)
for hostname, val in aggr_result.items():
if val[0].result is False:
sys.exit("Setting the boot variable failed")
# Verify the boot variable
result = nr_ios.run(
task=netmiko_send_command,
command_string= f'send log "configured from Nornir {time.time()}"',
num_workers=20,
)
print(result) # print result to see what was returned
std_print(result) #I dont get a result back
continue_func()
Result
GudeTest2960-X
GudeTest2960-X
Any help with this will be appreciated. Thanks.
Hi,
I tried this SNMPv3 part and I get the below import error.
Any idea what is the python module missing in my system?
I use Ubuntu 16.04.
ython 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
from snmp_helper import snmp_get_oid_v3,snmp_extract
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named 'snmp_helper'
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from snmp_helper import snmp_get_oid_v3,snmp_extract
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named snmp_helper
Mathew
The file show_arp.txt (https://github.com/ktbyers/pynet/blob/master/learning_python/lesson2/show_arp.txt
) provided for lesson 2 of the Learning Python for Network Engineers course is already sorted by IP address.
If participants in the course are attempting to sort it, they have no validation that the .sort() method works, because the input is the same as the output.
Hi Kirk,
I read your article "Using Nornir for OS Upgrades (Part 1)" and it is really nice article.
During the reading, i have couple of question. I would appreciate if you can help me with these queries I am using Windows 10 machine.
So that newstyle is applied to these links.
Hi Kirk,,
I followed your document of "Introduction to nornir' and facing the issues on nornir with windows OS.
The debugger logs :
c:\users\tkmamja.pycharmce2019.1\venv\scripts\lpthw\test\ktbyers\hey\heyhey.py(1)()
-> from nornir import InitNornir
(Pdb) n
c:\users\tkmamja.pycharmce2019.1\venv\scripts\lpthw\test\ktbyers\hey\heyhey.py(3)()
-> nr = InitNornir()
(Pdb) p nr
*** NameError: name 'nr' is not defined
(Pdb) p
*** SyntaxError: unexpected EOF while parsing
(Pdb) p nr
*** NameError: name 'nr' is not defined
(Pdb) n
ruamel.yaml.scanner.ScannerError: mapping values are not allowed in this context
in "hosts.yaml", line 2, column 5
c:\users\tkmamja.pycharmce2019.1\venv\scripts\lpthw\test\ktbyers\hey\heyhey.py(3)()
-> nr = InitNornir()
(Pdb) n
--Return--
c:\users\tkmamja.pycharmce2019.1\venv\scripts\lpthw\test\ktbyers\hey\heyhey.py(3)()->None
-> nr = InitNornir()
(Pdb) n
ruamel.yaml.scanner.ScannerError: mapping values are not allowed in this context
in "hosts.yaml", line 2, column 5
(1)()->None
(Pdb) p nr.inventory.hosts
*** NameError: name 'nr' is not defined
(Pdb) exit
When I ran the program I get this :
$ python heyhey.py
Traceback (most recent call last):
File "heyhey.py", line 5, in
nr = InitNornir()
File "C:\Users\TKMAMJA.PyCharmCE2019.1\venv\lib\site-packages\nornir\init_nornir.py", line 78, in InitNornir
**conf.inventory.options,
File "C:\Users\TKMAMJA.PyCharmCE2019.1\venv\lib\site-packages\nornir\core\deserializer\inventory.py", line 129, in deserialize
deserialized = cls(*args, **kwargs)
File "C:\Users\TKMAMJA.PyCharmCE2019.1\venv\lib\site-packages\nornir\plugins\inventory\simple.py", line 55, in init
super().init(hosts=hosts, groups=groups, defaults=defaults, *args, **kwargs)
File "C:\Users\TKMAMJA.PyCharmCE2019.1\venv\lib\site-packages\pydantic\main.py", line 142, in init
self.setstate(self._process_values(data))
File "C:\Users\TKMAMJA.PyCharmCE2019.1\venv\lib\site-packages\pydantic\main.py", line 312, in _process_values
return validate_model(self, input_data)
File "C:\Users\TKMAMJA.PyCharmCE2019.1\venv\lib\site-packages\pydantic\main.py", line 474, in validate_model
raise ValidationError(errors)
pydantic.error_wrappers.ValidationError: 4 validation errors
hosts -> rtr1 -> nornir_host
extra fields not permitted (type=value_error.extra)
groups -> defaults -> nornir_password
extra fields not permitted (type=value_error.extra)
groups -> defaults -> nornir_username
extra fields not permitted (type=value_error.extra)
groups -> cisco_ios -> nornir_nos
extra fields not permitted (type=value_error.extra)
What am I doing wrong here?????????? Could be great help.
Make sure this is disabled on all of the pynet and pynet-cl-beg list follow-up series emails. I am pretty sure that I already did this for pynet-cl-beg, but should double check it.
Can someone help me with example of using this with snmp v1 ? Default v2 works fine.
I have device that supports only v1.
I am getting an error while installing packages from requirements.txt. The package I am having issue with is black.
Downloading https://ci-repo.aexp.com/repository/python-proxy-internal/packages/2d/43/64205493cfb4c8e1720208d73502a121ace2a195cd4d1d49bd7470e4fd92/black-18.6b4.tar.gz (141kB)
100% |████████████████████████████████| 143kB 1.8MB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-LVunnl/black/setup.py", line 13
def get_long_description() -> str:
^
SyntaxError: invalid syntax```
Any help is appreciated. Thanks!!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.