Closed fahadtuhafi closed 6 years ago
@FahadPy What error do you see when you run your program?
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 :)
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
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
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?
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) >
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.
I think because netmiko enters the user mode of the switch "(EdgeSwitch) >" so we need to get to the exec mode "Enable mode".
Add a secret
argument to your os
variable and then call the enable()
method after connecting to the device.
It worked even without the context "strip_prompt=False, strip_command=False". So it means this is a privilege issue like you said!!
I just changed the "show run" to "show ver" and worked!!
I did like you said. Again same error with "show run" and it worked with "show ver"
Can you post your current code?
And what you see when you execute it.
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)
You need to call enable()
before you try to execute the show run
command.
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
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "netmiko-ubnt.py", line 14, in
I would try it again. That error indicates Netmiko was unable to connect to the device.
I tried again:
$ python netmiko-ubnt.py
Traceback (most recent call last):
File "netmiko-ubnt.py", line 16, in
This is the current code:
from netmiko import ConnectHandler
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)
@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.
@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.
I can run "show version" command from user mode, but now I'm having a problem entering the enable mode and run commands.
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.
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
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).
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.
Post your current code please.
I created a separate file with switch commands and sent it to the switch with send_command().
from netmiko import ConnectHandler
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)
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
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "netmiko-ubnt.py", line 17, in
establish_connection raise NetMikoTimeoutException(msg) netmiko.ssh_exception.NetMikoTimeoutException: Connection to device timed-out: ubiquiti_edgeswitch 192.168.1.2
Now all I need is just a way to go from user mode to privilege exec mode. Thank you for your help.
Now I called "splitlines()" function to lines variable. This is the code and output:
from netmiko import ConnectHandler
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
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.
Ok thank you for your help anyway. I will open a new one with more details. Thanks again!
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