12-12-2023 08:23 PM
Dears,
I am trying to run a code that could SSH list of devices and come back with data in an external file from these devices.
But, at the beginning I am facing some issues regarding netmiko and other features however I have already installed these functions. Could you please give a support hand?
Code:
from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'C:\Test.txt','w')
old_stdout = sys.stdout
sys.stdout = fd
platform = 'cisco_ios'
username = 'admin'
password = 'cisco'
ip_add_file = open(r'C:\IPAddressList.txt','r')
for host in ip_add_file:
host = host.strip()
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
output = device.send_command('terminal length 0')
output = device.send_command('enable')
print('##############################################################\n')
print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
output = device.send_command('sh run')
print(output)
print('##############################################################\n')
print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
output = device.send_command('sh ip int br')
print(output)
print('##############################################################\n')
fd.close()
Error:
Traceback (most recent call last):
File "C:\Users\adam\PycharmProjects\test\testt.py", line 2, in <module>
from netmiko import ConnectHandler
ModuleNotFoundError: No module named 'netmiko'
Process finished with exit code 1
Solved! Go to Solution.
12-13-2023 03:27 AM
There is something wrong with your certificates, most probably an intermediate proxy server. Can you try this from a CMD window:
C:\Users\ahmed.mmsaad\PycharmProjects\test\.venv\Scripts\activate
pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org netmiko
12-12-2023 10:19 PM - edited 12-12-2023 10:28 PM
Hi
First you need to install netmiko:
pip install netmiko
Also, I think there are some other errors in your script. So, here is an example which should work for your usecase:
Your IPAddressList.txt file is assumed in the same directory as your script, the commands you want to execute are listed in the commands list. The script will print the output of each command for each device to the screen, and also writes the output to a file Output.txt in the same directory.
from netmiko import ConnectHandler
host_list = "IPAddressList.txt"
out_file = "Output.txt"
username = "admin"
password = "cisco"
commands = [
"show run",
"show ip int br"
]
with open(host_list, "r") as fo:
hosts = [host.strip() for host in fo.readlines()]
with open(out_file, "w") as fo:
for host in hosts:
dev = {
"device_type": "cisco_ios",
"host": host,
"username": username,
"password": password,
}
with ConnectHandler(**dev) as net_connect:
for cmd in commands:
print(f"=== Get {cmd} on {host} ===")
output = net_connect.send_command(cmd)
print(output)
fo.write(f"=== Get {cmd} on {host} ===\n")
fo.write(output)
HTH
Marcel
12-12-2023 11:28 PM - edited 12-12-2023 11:30 PM
Hello Marcel,
Thanks I will try this, but netmiko is already installed (output: requirement already satisfied). But, I maybe have installed module in Python and run code with other PyCharm... Could you please guide how to install netmiko for PyCharm?
when I did is CMD path --> C:\windows\system32 ---> pip install netmiko.
But when I use PyCharm;
import sys
print (sys.executable)
my path is C:\Users\adam\PycharmProjects\test\.venv\Lib\site-packages_virtualenv.py
12-12-2023 11:48 PM
PyCharm creates a python virtual environment. How to install packages into this environemnt is documented here:
HTH
12-13-2023 03:12 AM - edited 12-13-2023 03:13 AM
Thank you Marcel. Could you please support in this showing error?
12-13-2023 03:27 AM
There is something wrong with your certificates, most probably an intermediate proxy server. Can you try this from a CMD window:
C:\Users\ahmed.mmsaad\PycharmProjects\test\.venv\Scripts\activate
pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org netmiko
12-14-2023 01:21 AM
Thank you Marcel for that great effort. Appreciated, mate.
I just have a simple silly question if I have list of IPS and then I want the output to be separately for each of them in a file text what might be the change on the code?
12-14-2023 03:22 AM
Hi, this should wirte a file for each IP:
from netmiko import ConnectHandler
host_list = "IPAddressList.txt"
username = "admin"
password = "cisco"
commands = [
"show run",
"show ip int br"
]
with open(host_list, "r") as fo:
hosts = [host.strip() for host in fo.readlines()]
for host in hosts:
dev = {
"device_type": "cisco_ios",
"host": host,
"username": username,
"password": password,
}
with open(f"output_{host}.txt", "w") as fo:
with ConnectHandler(**dev) as net_connect:
for cmd in commands:
print(f"=== Get {cmd} on {host} ===")
output = net_connect.send_command(cmd)
print(output)
fo.write(f"=== Get {cmd} on {host} ===\n")
fo.write(output)
HTH
03-02-2024 11:14 AM
Hi.
Hello,
How could I include a .txt file where all the commands are?
12-13-2023 03:33 AM
I don't use PyCharm but google[1] told me the above should also be possible directly in PyCharm:
File --> Settings --> Project: name_of_the_project --> Project Interpreter
Choose (double click) the package you want to update and the Available Packages will pop-up. There is a checkbox Options on the lower-left part of the window. Check that and enter
--trusted-host pypi.org --trusted-host files.pythonhosted.org
[1] https://www.appsloveworld.com/python/119/how-do-i-add-the-trusted-host-in-pycharm-package-install
04-22-2024 06:14 PM
You should consider using DevNetUI. It makes tasks like this simple and puts it in a webUI as well. Includes working examples of SSH and API calls among tons of other things and deployable working apps in seconds. www.devnetui
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide