0

I read "python network programming" and got to learn about netmiko. I tried connecting to the Cisco network given in the book and it did not connect. I went online and read other articles on netmiko and tried their Examples using the router login details given in the book, but none of them worked. My internet connection is good (I checked it). Please what is wrong? Is it my location (I stay in Nigeria)?

The error given to me is as follows:

possible reasons why a connection cannot be established are as follows:
*Wrong hostname
*Wrong TCP/IP port
*Wrong password
*Blocked access to router

Please what is wrong, I need help. Or if you have any free router I can connect to just for the sake of learning I would like to know.

Tes3awy
  • 2,166
  • 5
  • 29
  • 51
  • I think the error is pretty self explanatory, did you search what the error means and how it can be resolved? – Matiiss Aug 25 '21 at 20:06
  • @matiiss yeah i did.. but did not get a clear answer.. –  Aug 25 '21 at 21:00
  • 1
    You can check Cisco Sandboxes if you are interested in working. Here is a [link](developer.cisco.com/site/sandbox/) to where you can find a lot of sandboxes. One you can use is CSR1000v `sandbox-iosxe-latest-1.cisco.com` with username and password of `developer` and `C1sco12345` respectively. It's an Always-On Sandbox – Tes3awy Aug 26 '21 at 12:58
  • Maybe should use time delay between sending command. Use "time.sleep(.5) – Alchimie Sep 18 '21 at 17:23
  • please send your sample code to explain the problem that you heard it. – Shervin Hariri Jan 11 '22 at 08:21

2 Answers2

1

If you don't have direct ssh permission and you want to connect through a private server, you can also try paramiko instead of netmiko. Below I wrote an example of connecting via a server and entering the device by typing a command.

import paramiko
import time
from termcolor import colored
from timeit import default_timer as timer
import sys
                
global password
username = "ldap"
password = "pwd"
                

def vrf_implement_control(device_ip):
                
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                
    try:
        remote_connection = ssh.connect(device_ip, port="2222", username=username, password=password, timeout=15)
        remote_connection = ssh.invoke_shell()
        # device_connection
        remote_connection.send(" 192.168.0.10\n")
        time.sleep(2)
        print (colored ("connected_ip_address_"+ device_ip,"blue"))

    except Exception as e:
        print("erisim yok_\n")
tripleee
  • 175,061
  • 34
  • 275
  • 318
Mert Kulac
  • 294
  • 2
  • 19
  • `try` without `except` is a syntax error. You also appear to have a quoting error. – tripleee Apr 09 '22 at 19:25
  • my goal is to show how to connect to a device with the command "remote_connection.send(" 192.168.0.10+"\n")" after making a connection to a server with paramiko. – Mert Kulac Apr 09 '22 at 20:03
  • A blanket `except` is also strongly discouraged, but thanks for the update. I fixed the remaining quoting errors. (Stack Overflow performs syntax highlighting on Python code; you should suspect a missing quote when all your code after a certain point gets the "string" color.) – tripleee Apr 10 '22 at 06:54
0

I don't know which sample script you are trying. But as per your error, it looks like there is an issue with device connectivity, You can try to ping the device and check ssh is enabled or not.

Try to run the below script with your credentials, It should work.

from netmiko import ConnectHandler                                                                                                         

cisco = { 
        'device_type': 'cisco_ios',   #refer netmiko device type
        'host': '10.10.1.1',         #Replace with your device ip
        'username': 'admin',        #Router username
        'password': 'cisco123',     #password
       }  

net_connect = ConnectHandler(**cisco) 
output = net_connect.send_command("show version")
print(output)
Sachin
  • 1,460
  • 17
  • 24