ktbyers / netmiko

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

Cannot SSH to "ubiquiti_edgeswitch" #808

Closed fahadtuhafi closed 6 years ago

fahadtuhafi commented 6 years ago

Not able to ssh into a Ubiquiti Edge-switch using a python script with Netmiko library imported. I changed the device type to 'device_type': 'ubiquiti_edgeswitch'. But still not able to ssh into a Ubiquiti Edge switch. However I can SSH into a Cisco. Could you help please?

Here is the code:

from netmiko import ConnectHandler

os = { 'device_type': 'ubiquiti_edgeswitch', 'ip': '192.168.1.2, 'username': 'ubnt', 'password': 'ubnt' }

net_connect = ConnectHandler(**os) output = net_connect.send_command('show run') print (output)

Thanks! Fahad

ktbyers commented 6 years ago

@FahadPy What error do you see when you run your program?

fahadtuhafi commented 6 years ago

Hello @ktbyers this is the error I'm getting:

root:~$ python netmiko-ubnt.py ^ % Invalid input detected at '^' marker.

However same script works with Cisco nexus devices, I just change device type to "cisco_nxos"

Thank you you @ktbyers for your reply :)

ktbyers commented 6 years ago

that looks like an error from the device.

If you change the command to show version, what does that show?

output = net_connect.send_command('show version', strip_prompt=False, strip_command=False)

Kirk

fahadtuhafi commented 6 years ago

Wow that worked!! Thank you so much @ktbyers for your great answer!

$ python netmiko-ubnt.py show version

Switch: 1

System Description............................. EdgeSwitch 16-Port 150W, 1.7.1.4993748, Linux 3.6.5-f4a26ed5, 1.0.0.4857129 Machine Type................................... EdgeSwitch 16-Port 150W Machine Model.................................. ES-16-150W Serial Number.................................. Burned In MAC Address.......................... Software Version............................... 1.7.1.4993748

ktbyers commented 6 years ago

What happens if you swap 'show run' in now (into what I posted earlier)?

I wonder if 'show run' is even supported in that context you tried to execute it in?

fahadtuhafi commented 6 years ago

I changed it to "show run" and still doesn't work. But now the output shows the switch prompt!

$ python netmiko-ubnt.py show run ^ % Invalid input detected at '^' marker.

(EdgeSwitch) >

ktbyers commented 6 years ago

Okay, I think you can't do that command in that context (so I don't know whether it doesn't allow the abbreviated command or whether you need to be in at a more privileged level (what on Cisco IOS would be privilege exec).

Regardless though, Netmiko is sending the command to the device and giving you back the output.

fahadtuhafi commented 6 years ago

I think because netmiko enters the user mode of the switch "(EdgeSwitch) >" so we need to get to the exec mode "Enable mode".

ktbyers commented 6 years ago

Add a secret argument to your os variable and then call the enable() method after connecting to the device.

ktbyers commented 6 years ago

See example here:

https://github.com/ktbyers/netmiko/blob/develop/examples/use_cases/case2_using_dict/enable.py

fahadtuhafi commented 6 years ago

It worked even without the context "strip_prompt=False, strip_command=False". So it means this is a privilege issue like you said!!

fahadtuhafi commented 6 years ago

I just changed the "show run" to "show ver" and worked!!

fahadtuhafi commented 6 years ago

I did like you said. Again same error with "show run" and it worked with "show ver"

ktbyers commented 6 years ago

Can you post your current code?

And what you see when you execute it.

fahadtuhafi commented 6 years ago

from netmiko import ConnectHandler

os = { 'device_type': 'ubiquiti_edgeswitch', 'ip': '192.168.1.2', 'username': 'ubnt', 'password': 'ubnt', 'secret': 'ubnt' }

net_connect = ConnectHandler(**os) output = net_connect.send_command('show run') net_connect.enable() print (output)

ktbyers commented 6 years ago

You need to call enable() before you try to execute the show run command.

fahadtuhafi commented 6 years ago

It shows this error:

$ python netmiko-ubnt.py Traceback (most recent call last): File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 689, in establish_connection self.remote_conn_pre.connect(**ssh_connect_params) File "/anaconda3/lib/python3.6/site-packages/paramiko/client.py", line 338, in connect retry_on_signal(lambda: sock.connect(addr)) File "/anaconda3/lib/python3.6/site-packages/paramiko/util.py", line 279, in retry_on_signal return function() File "/anaconda3/lib/python3.6/site-packages/paramiko/client.py", line 338, in retry_on_signal(lambda: sock.connect(addr)) TimeoutError: [Errno 60] Operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "netmiko-ubnt.py", line 14, in net_connect = ConnectHandler(*os) File "/anaconda3/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py", line 178, in ConnectHandler return ConnectionClass(args, **kwargs) File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 207, in init self.establish_connection() File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 693, in establish_connection raise NetMikoTimeoutException(msg) netmiko.ssh_exception.NetMikoTimeoutException: Connection to device timed-out: ubiquiti_edgeswitch 192.168.1.2

ktbyers commented 6 years ago

I would try it again. That error indicates Netmiko was unable to connect to the device.

fahadtuhafi commented 6 years ago

I tried again: $ python netmiko-ubnt.py Traceback (most recent call last): File "netmiko-ubnt.py", line 16, in output = net_connect.send_command('show run') File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 1051, in send_command search_pattern)) OSError: Search pattern never detected in send_command_expect: (EdgeSwitch)\ #

This is the current code:

from netmiko import ConnectHandler

from netmiko.ubiquiti import UbiquitiEdgeSSH

os = { 'device_type': 'ubiquiti_edgeswitch', 'ip': '192.168.1.2', 'username': 'ubnt', 'password': 'ubnt', 'secret': 'ubnt' }

net_connect = ConnectHandler(**os) net_connect.enable() output = net_connect.send_command('show run') print (output)

ktbyers commented 6 years ago

@FahadPy Okay, show what doing show run looks like when you do it from the CLI, including the login and going to enable mode.

You can obfuscate anything that is confidential like passwords, and IP addresses.

fahadtuhafi commented 6 years ago

@ktbyers It's exactly like Cisco CLI. However it was prompting a password when I go from user mode to enable mode, now I made it possible to enter enable mode without prompting a password (I increased the privilege on the switch) but still we don't go directly to enable mode, so we need to write "enable" to move to exec mode but now without prompting a password.

fahadtuhafi commented 6 years ago

I can run "show version" command from user mode, but now I'm having a problem entering the enable mode and run commands.

ktbyers commented 6 years ago

Okay, I am going to close this.

If you want assistance, let me know, but I am going to re-ask what I asked earlier and for you undo your last changes (it is just causing the behavior to change and then I have to solve a different issue).

Please show what doing show run looks like when you do it from the CLI, including the login and going to enable mode.
fahadtuhafi commented 6 years ago

Here is the output when you log in to the switch CLI, and then I ran "show run" command:

Welcome to EdgeSwitch

By logging in, accessing, or using the Ubiquiti product, you acknowledge that you have read and understood the Ubiquiti License Agreement (available in the Web UI at, by default, http://192.168.1.2) and agree to be bound by its terms.

(EdgeSwitch) >enable

(EdgeSwitch) #show run

!Current Configuration: ! !System Description "EdgeSwitch 16-Port 150W, 1.7.1.4993748, Linux 3.6.5-f4a26ed5, 1.0.0.4857129" !System Software Version "1.7.1.4993748" !System Up Time "7 days 19 hrs 36 mins 15 secs" !Additional Packages QOS,IPv6 Management,Routing !Current SNTP Synchronized Time: SNTP Last Attempt Status Is Not Successful ! hostname "EdgeSwitch" network ipv6 address autoconfig vlan database vlan 411 exit

network mgmt_vlan 411 ip ssh server enable configure aaa authentication enable "enableList" enable none line console exit

line telnet exit

line ssh exit

snmp-server sysname "EdgeSwitch" !

interface 0/1 description 'Xena' switchport mode access switchport access vlan 411 exit

interface 0/9 switchport mode access switchport access vlan 411 exit

interface 0/10 switchport mode access --More-- or (q)uit

ktbyers commented 6 years ago

Okay, thanks. Now what is the error you are currently receiving (I see from the above no password was required to go to enable mode).

fahadtuhafi commented 6 years ago

Yes no password is required. Now Netmiko still accesses the user mode. I want to access the enable mode. I tried to insert a separated file with switch commands like this enable show run but still not able to access the enable mode.

ktbyers commented 6 years ago

Post your current code please.

fahadtuhafi commented 6 years ago

I created a separate file with switch commands and sent it to the switch with send_command().

from netmiko import ConnectHandler

from netmiko.ubiquiti import UbiquitiEdgeSSH

os = { 'device_type': 'ubiquiti_edgeswitch', 'ip': '192.168.1.2', 'username': 'ubnt', 'password': 'ubnt' }

with open('ubnt') as f: lines = f.read() print(lines)

net_connect = ConnectHandler(**os) output = net_connect.send_command(lines) print (output)

fahadtuhafi commented 6 years ago

This is the output I got:

$ python netmiko-ubnt.py enable show run

Traceback (most recent call last): File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 689, in establish_connection self.remote_conn_pre.connect(**ssh_connect_params) File "/anaconda3/lib/python3.6/site-packages/paramiko/client.py", line 338, in connect retry_on_signal(lambda: sock.connect(addr)) File "/anaconda3/lib/python3.6/site-packages/paramiko/util.py", line 279, in retry_on_signal return function() File "/anaconda3/lib/python3.6/site-packages/paramiko/client.py", line 338, in retry_on_signal(lambda: sock.connect(addr)) TimeoutError: [Errno 60] Operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "netmiko-ubnt.py", line 17, in net_connect = ConnectHandler(*os) File "/anaconda3/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py", line 178, in ConnectHandler return ConnectionClass(args, **kwargs) File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 207, in init self.establish_connection() File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 693, in

establish_connection raise NetMikoTimeoutException(msg) netmiko.ssh_exception.NetMikoTimeoutException: Connection to device timed-out: ubiquiti_edgeswitch 192.168.1.2

fahadtuhafi commented 6 years ago

Now all I need is just a way to go from user mode to privilege exec mode. Thank you for your help.

fahadtuhafi commented 6 years ago

Now I called "splitlines()" function to lines variable. This is the code and output:

from netmiko import ConnectHandler

from netmiko.ubiquiti import UbiquitiEdgeSSH

os = { 'device_type': 'ubiquiti_edgeswitch', 'ip': '192.168.1.2', 'username': 'ubnt', 'password': 'ubnt' }

with open('ubnt') as f: lines = f.read().splitlines() print(lines)

net_connect = ConnectHandler(**os) output = net_connect.send_command(lines) print (output)

Output:

$ python netmiko-ubnt.py ['enable', 'show run'] Traceback (most recent call last): File "netmiko-ubnt.py", line 18, in output = net_connect.send_command(lines) File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 1019, in send_command command_string = self.normalize_cmd(command_string) File "/anaconda3/lib/python3.6/site-packages/netmiko/base_connection.py", line 1124, in normalize_cmd command = command.rstrip() AttributeError: 'list' object has no attribute 'rstrip'

ktbyers commented 6 years ago

I am going to close this issue, unfortunately, you keep changing the issue so I can't effectively help debug your original problem.

Your new issue is caused by your file read operation, you need a list and not a string (so do a splitines() on your lines string.

fahadtuhafi commented 6 years ago

Ok thank you for your help anyway. I will open a new one with more details. Thanks again!