ktbyers / netmiko

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

Cisco SG350-10 - Not much working.... #1589

Closed damienbutt closed 4 years ago

damienbutt commented 4 years ago

Been trying to use netmiko to write a script for a Cisco SG350-10 and not having much luck. All the videos on YouTube made it look so simple haha.

from netmiko import Netmiko
from getpass import getpass

cisco_switch = {
    "host": "192.168.1.17",  # input("Host: "),
    "username": "damien",  # input("Username: "),
    "password": "Cisco12345",  # getpass(),
    "device_type": "cisco_ios"
}

connection = Netmiko(**cisco_switch)
prompt = connection.find_prompt()
print(prompt)

output = connection.send_command("show vlan")
print(output)

connection.disconnect()

This outputs:

[Running] py -u "c:\Users\Damien Butt\Norgate AV Dropbox\Damien Butt\Resources\Crestron\Cisco NVX Setup\cisco_nvx_setup.py"
switch28798b#
Traceback (most recent call last):
  File "c:\Users\Damien Butt\Norgate AV Dropbox\Damien Butt\Resources\Crestron\Cisco NVX Setup\cisco_nvx_setup.py", line 33, in <module>
    output = connection.send_command("show vlan")
  File "C:\Users\Damien Butt\AppData\Local\Programs\Python\Python38\lib\site-packages\netmiko\base_connection.py", line 1405, in send_command
    raise IOError(
OSError: Search pattern never detected in send_command_expect: \[Kswitch28798b\#

[Done] exited with code=1 in 111.877 seconds

It seems to find the prompt fairly quickly, although it has some extra chars at the start. Then it hangs for a long time before the exception is thrown. I think it's something to do with those extra chars in the prompt perhaps. They shouldn't be there.

I've also tried using the send_config_set method and that also throws exceptions.

However, if I use the send_command method and specify the "expect_string" parameter as follows:

output = connection.send_command("show vlan", expect_string=r"#"))

That works and outputs:

[Running] py -u "c:\Users\Damien Butt\Norgate AV Dropbox\Damien Butt\Resources\Crestron\Cisco NVX Setup\cisco_nvx_setup.py"
switch28798b#
Created by: D-Default, S-Static, G-GVRP, R-Radius Assigned VLAN, V-Voice VLAN

Vlan       Name           Tagged Ports      UnTagged Ports      Created by    
---- ----------------- ------------------ ------------------ ---------------- 
 1           1                               gi1-10,Po1-8           DV        

switch28798b#

[Done] exited with code=0 in 8.329 seconds

Ideally, I need to use the send_config_set method as I have a bunch of commands to send.

Any ideas what the problem could be?

Thanks in advance.

Damien

ktbyers commented 4 years ago

@damienbutt You are using the wrong device_type...you need to switch over to:

device_type:     "cisco_s300"

I would try that and see what happens.

damienbutt commented 4 years ago

Amazing! That did it. Thanks.

Sorry if this is a Noob question, but what defines the device type then? Is there any way to get this dynamically so I don't need to know it?

ktbyers commented 4 years ago

@damienbutt There is an SSH autodetect mechanism in Netmiko and an SNMP autodetect mechanism as well. Support for these (as far as Netmiko platforms is somewhat limited, however). I would just look at the ssh_dispatcher.py and find the device_type that most closely matched your platform.

Or in your code, you can put in an invalid device_type and Netmiko will show you all of the options.

harshadsowani commented 2 years ago

just had the same issue and changing the device_type to cisco_s300 worked for me as well, but I also had to add 'ip ssh password-auth' to the switch config for the authentication to actually go through. Just leaving this here in case it helps someone else.

ktbyers commented 2 years ago

Yep, the ip ssh password-auth is required on the s300/s350 devices to get them to behave normal from an SSH auth perspective.

gaby commented 2 years ago

Seems that SSHDetect doesn't support cisco_s300

ktbyers commented 2 years ago

@gaby Did you check if it is supported by ssh_autodetect? If not, someone would need to add that support.

gaby commented 2 years ago

@ktbyers That's exactly the issue, it's missing from the dict. I'd try making a PR for this during the weekend.