cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
765
Views
4
Helpful
10
Replies

Python SSH devices

lAhmed Saadl
Level 1
Level 1

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:

lAhmedSaadl_0-1702441246044.png

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

1 Accepted Solution

Accepted Solutions

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

 

View solution in original post

10 Replies 10

Marcel Zehnder
Spotlight
Spotlight

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

 

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

 

PyCharm creates a python virtual environment. How to install packages into this environemnt is documented here:

https://www.jetbrains.com/guide/python/tutorials/getting-started-pycharm/installing-and-managing-python-packages/

HTH

Thank you Marcel. Could you please support in this showing error?

lAhmedSaadl_0-1702465933029.png

 

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

 

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?

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

Hi.

Hello,

How could I include a .txt file where all the commands are?

Marcel Zehnder
Spotlight
Spotlight

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

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