ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.59k stars 1.31k forks source link

Errors with netmiko + textfsm #2260

Closed hhha7x closed 3 years ago

hhha7x commented 3 years ago

Having some issues trying to run netmiko with textfsm. Here is my code. Running this on cisco ios switches

` from netmiko import ConnectHandler from textfsm import TextFSM

with open('devices.txt') as routers: for IP in routers: Router = { 'device_type': 'cisco_ios', 'ip' : IP, 'username': 'user', 'password': 'password' } net_connect = ConnectHandler(*Router) print('-'79) output = net_connect.send_command('show int status',use_textfsm=True) for i in output: if i["vlan"] == "500" and i["status"] == "notconnect": config_set = ['interface ' + i["port]"], 'shutdown'] x = netconnect_send_config_set(config_set) print(output) print() print('-'*79)

net_connect.disconnect() `

The goal is to shut down unused ports that are only in VLAN 500. When I run this, I get the error "ModuleNotFoundError: No module named 'copyable_regex_object"

Did some research and found another post on this site and Ktbyers said something about it being an issue with textfsm, so I downgraded it from its current version (1.1.0) to 0.4.1

After doing this, it fixed the regex error, but then I get the following error "ImportError: cannot import name 'ConnectHandler".

Did some research on that, and saw that kybyers said about looking for 2 files named netmiko.py and netmiko.pyc. I did a search and did not find any of those files. Tried to reinstall netmiko, and it just brought me back to the first issue with regex because it updated my textfsm then.

Does anyone have any ideas how to fix this error? Netmiko did work fine before I started adding in textfsm.

Sorry for the formatting, trying to insert the code and it doesn't seem to be working.

Thanks!

ktbyers commented 3 years ago

Remove this line:

from textfsm import TextFSM

If that doesn't work, please show your full exception stack trace of your current state and the output of pip freeze. Note, I will only help if you are using TextFSM 1.1.0 or later.

Regards, Kirk

hhha7x commented 3 years ago

Removed that line and still not fix. I am not sure how to show the full exception stack trace, but here is the output of pip3 freeze

ansible==2.9.18 ansible-base==2.10.3 asn1crypto==0.24.0 attrs==20.3.0 Babel==2.5.1 bcrypt==3.1.6 blivet==3.2.2 Brlapi==0.6.7 cffi==1.14.4 chardet==3.0.4 chrome-gnome-shell==0.0.0 ciscoconfparse==1.5.30 colorama==0.4.4 configobj==5.0.6 cryptography==3.3 cupshelpers==1.0 dasbus==1.2 dataclasses==0.7 dbus-python==1.2.4 decorator==4.2.1 dnspython==2.1.0 ethtool==0.14 future==0.18.2 gpg==1.13.1 idna==2.5 importlib-resources==5.1.2 iniparse==0.4 initial-setup==0.3.81.6 isc==2.0 Jinja2==2.11.3 jmespath==0.9.0 junos-eznc==2.5.4 langtable==0.0.51 lxml==4.6.3 MarkupSafe==1.1.1 mypy-extensions==0.4.3 napalm==3.2.0 ncclient==0.6.10 netaddr==0.8.0 netmiko==3.3.3 nftables==0.1 nornir==3.1.1 ntc-templates==2.0.0 ntplib==0.3.3 ordered-set==2.0.2 packaging==20.7 paramiko==2.7.2 passlib==1.7.4 pathspec==0.8.1 perf==0.1 pid==2.1.1 ply==3.9 productmd==1.11 pwquality==1.4.0 pyasn1==0.3.7 pyats.datastructures==21.4 pyats.results==21.4 pycairo==1.16.3 pycparser==2.20 pycups==1.9.72 pycurl==7.43.0.2 pydbus==0.6.0 pyeapi==0.8.4 pyenchant==2.0.0 pygobject==3.28.3 pyinotify==0.9.6 pykickstart==3.16.11 PyNaCl==1.3.0 pyparsing==2.4.7 pyparted==3.11.0 pyserial==3.5 PySocks==1.6.8 python-dateutil==2.6.1 python-dmidecode==3.12.2 python-engineio==3.13.2 python-linux-procfs==0.6.2 python-meh==0.47.2 python-socketio==4.6.0 pytz==2017.2 pyudev==0.21.0 pyxdg==0.25 PyYAML==3.12 requests==2.20.0 requests-file==1.4.3 requests-ftp==0.3.1 rpm==4.14.3 ruamel.yaml==0.16.13 ruamel.yaml.clib==0.2.2 schedutils==0.6 scp==0.13.3 selinux==2.9 sepolicy==1.1 setools==4.3.0 setroubleshoot==1.1 simpleline==1.1.1 six==1.11.0 slip==0.6.4 slip.dbus==0.6.4 sos==3.9 SSSDConfig==2.3.0 style==1.1.0 subscription-manager==1.27.16 syspurpose==1.27.16 systemd-python==234 tenacity==7.0.0 textfsm==1.1.0 transitions==0.8.8 typing-extensions==3.7.4.3 update==0.0.1 urllib3==1.24.2 yamllint==1.26.0 yamlordereddictloader==0.4.0 zipp==3.4.1

Being back to textfsm 1.1.0 gets me back to the error ' ModuleNotFoundError: No module named 'copyable_regex_object '

ktbyers commented 3 years ago

I need all of the error lines around this (i.e. there should be a bunch more output lines indicating where the exception occurred):

 ModuleNotFoundError: No module named 'copyable_regex_object '

How are you executing. your program? Are you using an IDE or just running "python" from the command-line?

What operating system are you on?

hhha7x commented 3 years ago

Oh my fault. I see what you mean. So I now see both errors that I stated above.

` [root@localhost jimmy]# python3 textfsm.py Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/netmiko/_textfsm/_clitable.py", line 37, in from textfsm import copyable_regex_object File "/home/jimmy/textfsm.py", line 1, in from netmiko import ConnectHandler ImportError: cannot import name 'ConnectHandler'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "textfsm.py", line 1, in from netmiko import ConnectHandler File "/usr/local/lib/python3.6/site-packages/netmiko/init.py", line 7, in from netmiko.ssh_dispatcher import ConnectHandler File "/usr/local/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py", line 2, in from netmiko.a10 import A10SSH File "/usr/local/lib/python3.6/site-packages/netmiko/a10/init.py", line 1, in from netmiko.a10.a10_ssh import A10SSH File "/usr/local/lib/python3.6/site-packages/netmiko/a10/a10_ssh.py", line 3, in from netmiko.cisco_base_connection import CiscoSSHConnection File "/usr/local/lib/python3.6/site-packages/netmiko/cisco_base_connection.py", line 2, in from netmiko.base_connection import BaseConnection File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 28, in from netmiko.utilities import ( File "/usr/local/lib/python3.6/site-packages/netmiko/utilities.py", line 9, in from netmiko._textfsm import _clitable as clitable File "/usr/local/lib/python3.6/site-packages/netmiko/_textfsm/init.py", line 3, in from netmiko._textfsm import _clitable File "/usr/local/lib/python3.6/site-packages/netmiko/_textfsm/_clitable.py", line 40, in import copyable_regex_object ModuleNotFoundError: No module named 'copyable_regex_object' `

I am running this on CentOS 8. The script is saved to a file called textfsm.py . I have been executing it both from the command line and from visual studio code. Both with the same results.

ktbyers commented 3 years ago

You cannot call your Python program textfsm.py (that is the same name as the textfsm library and will likely cause things to break).

You need to rename this file to something else:

mv textfsm.py test_textfsm.py

You will need to also delete the textfsm.pyc file created that is located in the __pycache__ directory.

hhha7x commented 3 years ago

So did all that, and now I am getting the following....

x = netconnect_send_config_set(config_set) NameError: name netconnect_send_config_set is not defined

I am guessing this is something with the variable but can't seem to figure out a fix for it.

ktbyers commented 3 years ago

You are missing the period between net_connect and send_config_set

net_connect = ConnectHandler(**Router)
net_connect.send_config_set(config_commands)
hhha7x commented 3 years ago

@ktbyers thanks for the help, got it working.