ktbyers / netmiko

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

Mikrotik Send Command (Netmiko 3.0.0) #1600

Closed wpureza closed 3 years ago

wpureza commented 4 years ago

Hi,

after upgrade the netmiko module from 2.4.2 to 3.0.0 the "send_command" function, for mikrotik RouterOS device, is not working. The traceback message is showing "IndexError" exception for the list "response_list[-2]" in mikrotik_ssh module.

Follow:

Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/netmiko_test/teste.py", line 21, in print(connection.send_command('/interface bridge host print')) File "C:\Users\Administrator\PycharmProjects\netmiko_test\venv\lib\site-packages\netmiko\base_connection.py", line 1415, in send_command strip_prompt=strip_prompt, File "C:\Users\Administrator\PycharmProjects\netmiko_test\venv\lib\site-packages\netmiko\base_connection.py", line 861, in _sanitize_output output = self.strip_prompt(output) File "C:\Users\Administrator\PycharmProjects\netmiko_test\venv\lib\site-packages\netmiko\mikrotik\mikrotik_ssh.py", line 78, in strip_prompt last_line = response_list[-2] IndexError: list index out of range

Process finished with exit code 1

The "find_prompt" function is working. I'm using Python 3.7.

Regards, Walter

ktbyers commented 4 years ago

Can you try this fix here:

https://github.com/ktbyers/netmiko/pull/1606

wpureza commented 4 years ago

Hi, Kirk. Thank you for your reply. I tried the fix, but im not getting the output from var "a_string". Follow my code:

from netmiko import ConnectHandler

def login(ip, user, password):
    device = {
        'device_type': 'mikrotik_routeros',
        'host': ip,
        'username': user,
        'password': password,
        'port': 22
    }
    conn = ConnectHandler(**device)
    return conn

if __name__ == '__main__':
    ip = '192.168.88.1'
    user = 'admin'
    password = 'pass'
    connection = login(ip, user, password)
    print(connection.find_prompt())
    cmd = connection.send_command('/interface bridge host print')
    print(cmd)

Output:

C:\Users\Administrator\PycharmProjects\netmiko_test\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/netmiko_test/teste.py
[admin@MikroTik] >

Process finished with exit code 0
ktbyers commented 4 years ago

Can you turn on Netmiko logging so we can get some more details about what is occurring?

https://github.com/ktbyers/netmiko/blob/develop/COMMON_ISSUES.md#enable-netmiko-logging-of-all-reads-and-writes-of-the-communications-channel

wpureza commented 4 years ago

Hi, Kirk. Sorry for the delay.

Follow the log file for the same code posted before: netmiko_mikrotik_10032020.log

ktbyers commented 4 years ago

@wpureza So is the issue you don't get output--correct?

mehrzadmo commented 4 years ago

@ktbyers
Hi , I have same problem . I tried to use netmiko sample , and got an error I resolved the error with your previous message , I fix it but there is no result of my command . this is my code :

`#!/usr/bin/env python from netmiko import Netmiko from getpass import getpass

dev = { "host": "x.x.x.x", "username": "username", "password": "password", "device_type": "mikrotik_routeros", }

net_connect = Netmiko(**dev) command = "/ip add pr"

print() print(net_connect.find_prompt()) output = net_connect.send_command(command) net_connect.disconnect() print(output) print()`

and the result :

`PS C:\Users\Mehrzad> & C:/Users/Mehrzad/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Mehrzad/Desktop/mik.py

[admin@Mehr_Home] >

PS C:\Users\Mehrzad> `

PatrickstarWritesCode commented 4 years ago

I'm on the same page here. If I try the suggested fix #1606 I don't get the output.

ktbyers commented 4 years ago

@mehrzadmo

If you do this manually via SSH on the CLI:

command = "/ip add pr"

Can you show me what the SSH session looks like i.e. what it looks like manually?

mehrzadmo commented 4 years ago

@ktbyers thanks for your attention , there is the result

[admin@Mehr_Home] > /ip add pr
Flags: X - disabled, I - invalid, D - dynamic 
 #   ADDRESS            NETWORK         INTERFACE                                                                          
 0   192.168.10.1/24    192.168.10.0    ether1                                                                             
 1 D a.b.c.d/32      x.y.z.a      pppoe-out1                                                                         
 2 D 192.168.121.254/24 192.168.121.0   eoip-VPN                                                                           
[admin@Mehr_Home] > 
ktbyers commented 4 years ago

@mehrzadmo And your code works with Netmiko 2.4.2?

mehrzadmo commented 4 years ago

@ktbyers I tried ! thats worked !

[admin@Mehr_Home] >
[admin@Mehr_Home] >

PS C:\Users\Mehrzad> & C:/Users/Mehrzad/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/Mehrzad/Desktop/mik.py

[admin@Mehr_Home] >
[admin@Mehr_Home] >
Flags: X - disabled, I - invalid, D - dynamic
 #   ADDRESS            NETWORK         INTERFACE
 0   192.168.10.1/24    192.168.10.0    ether1
 1 D x.x.x.x/32     x.x.x.x      pppoe-out1
 2 D 192.168.121.254/24 192.168.121.0   eoip-VPN
MajesticFalcon commented 4 years ago

The same issue is happening for me as described by @shahro0z . The described fix provides no output. I will add logs soon.

MajesticFalcon commented 4 years ago

test.log

ktbyers commented 4 years ago

@MajesticFalcon So just to verify...using Netmiko 3.1.0 when you execute:

/system routerboard print using send_command() you receive no output back. Is that correct?

MajesticFalcon commented 4 years ago

When I run the command at the terminal from putty, I get a response. When using netmiko 3.0, I get the index error. When applying the fix up above, I get blank output.

ktbyers commented 4 years ago

Hmmm, and what happens if you use Netmiko 3.1.0 and you set global_cmd_verify=False? This is an argument to ConnectHandler.

MajesticFalcon commented 4 years ago

That works. I believe there are two existing issues.

  1. Netmiko isn't handling spaces inside host names (see attached, happens on both 3.0 and 3.1)
  2. Netmiko is including multiple lines of empty space (see attached, happens on 3.1) lots_of_space_returned.log prompt_failure.log
MajesticFalcon commented 4 years ago

After more testing, it appears the space in the hostname is not the issue. Replacing spaces with other characters doesnt fix it. I believe it may have to do with firmware. I will update tonight and report back.

ktbyers commented 4 years ago

@MajesticFalcon What did you find out here?

jgmel commented 4 years ago

Set global_cmd_verify=False does not solve the issue. Mikrotik returns for each command doble echo command with prompt \r{comand}\n{echo_promp}{comand}\n{comand_result}{next_prompt} if cmd_verify is enabled only remove first comand and read loop (https://github.com/ktbyers/netmiko/blob/063bdd4184bbb08fad5f82f2b429ab1323155fc3/netmiko/base_connection.py#L1405) read until echo_prompt and skip command result.

jgmel commented 4 years ago

a posible solution https://github.com/johnbarneta/netmiko/commit/d408eb532cbd2b5ee407ff1fa854978784886571 tested on various devices and it works fine!

aleksandr-bembel commented 4 years ago

@johnbarneta it works, but your next commit https://github.com/johnbarneta/netmiko/commit/439dc5159f5b56a7c0c2cc61712367d0f918ab88 is also needed to completely fix the issue

tonoitp commented 4 years ago

Ran into this too :s On netmiko 3.1.1 the 'global_cmd_verify':False is (still) required to get commands working

jgmel commented 4 years ago

Ran into this too :s On netmiko 3.1.1 the 'global_cmd_verify':False is (still) required to get commands working

When 'global_cmd_verify':False is set, https://github.com/ktbyers/netmiko/commit/944953c896e96d76ecac1cf25991f3a0e148c9b0 and https://github.com/johnbarneta/netmiko/commit/d408eb532cbd2b5ee407ff1fa854978784886571 gives a partial result

zerosays commented 4 years ago

Ran into this too :s On netmiko 3.1.1 the 'global_cmd_verify':False is (still) required to get commands working

When 'global_cmd_verify':False is set, 944953c and johnbarneta@d408eb5 gives a partial result

This worked for me

ktbyers commented 3 years ago

Anyone have a Microtik device I can test against so I can see if I can get a more permanent fix in place?

PatrickstarWritesCode commented 3 years ago

Anyone have a Microtik device I can test against so I can see if I can get a more permanent fix in place?

You can also grab a Cloud Hosted Router and setup a virtual one.

zerosays commented 3 years ago

Anyone have a Microtik device I can test against so I can see if I can get a more permanent fix in place?

Sure. Ping me.

orkasolutionscz commented 3 years ago

Hmmm, and what happens if you use Netmiko 3.1.0 and you set global_cmd_verify=False? This is an argument to ConnectHandler.

Yes, this working :)

antonc42 commented 3 years ago

I was able to get send_command() to a Mikrotik working. I started with the "v3.4.0" tag from this repo (the most recent released version as of my writing), then applied the patches jgmel/netmiko@d408eb532cbd2b5ee407ff1fa854978784886571 and jgmel/netmiko@439dc5159f5b56a7c0c2cc61712367d0f918ab88 in that order. After re-installing this patched version, everything seemed to work, even without using global_cmd_verify=False.

The prompt string is not stripped from the output, however.

ktbyers commented 3 years ago

Proposed fix for the Mikrotik issues:

https://github.com/ktbyers/netmiko/pull/2362

If anyone is willing to test this, it would be very much appreciated.

NoifP commented 3 years ago

Anyone have a Microtik device I can test against so I can see if I can get a more permanent fix in place?

Apart from the VM there is also an online demo (read only user account) that you can ssh to. hostname: demo.mt.lv OR hostname: demo2.mt.lv username: demo no password. More details here: https://mikrotik.com/software demo2.mt.lv is currently running beta software so demo.mt.lv would be a better test target.

ktbyers commented 3 years ago

Should be fixed in this PR:

https://github.com/ktbyers/netmiko/pull/2362