Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect mikrotik through Juniper router #3502

Open
jonafernaho-1 opened this issue Sep 28, 2024 · 0 comments
Open

Connect mikrotik through Juniper router #3502

jonafernaho-1 opened this issue Sep 28, 2024 · 0 comments

Comments

@jonafernaho-1
Copy link

jonafernaho-1 commented Sep 28, 2024

I want to establish a connection to a mikrotik router from a juniper router using netmiko

Netmiko version

netmiko==4.2.0

Netmiko device_type (if relevant to the issue)

juniper_junos
mikrotik_routeros

If I make the connection manually (connecting to the Juniper router and from there sending the connection via ssh) it works for me (the conection works with the interface: ssh [email protected] interface et-0/3/2.5363, if i try without it it doesn't work) and after the welcome message I can see the prompt ready to receive commands. But with netmiko it doesn''t work.

with the Manual conection through the router Juniper, this is the prompt that i get after the initial message in the mikrotik:
[user@CID_XXXXX_PPAL] >

This i s what i've tried:

from netmiko import ConnectHandler, redispatch
import time

juniper = {
    'device_type': 'juniper_junos',
    'host': 'X.X.120.130',
    'username': {username},
    'password': {password},
    'session_log': 'netmiko_session.log'
}
net_connect = ConnectHandler(**juniper)
mikrotik_ssh_command = "ssh [email protected] interface et-0/3/2.5363\n"
net_connect.write_channel(mikrotik_ssh_command)
time.sleep(1)

output = net_connect.read_channel()
print(output)

if 'ssword' in output:
    net_connect.write_channel("1fx@dm1nC0!\n")
    time.sleep(2)

if i send a read_channel, i can see the prompt at the end (i paste the complete output) which allows me to conclude that the connection was successful:

>>> net_connect.read_channel()
'\n\n\n\n\n\n\n\n\n\n  MMM      MMM       KKK                          TTTTTTTTTTT      KKK\n\n  MMMM    MMMM       KKK                          TTTT
TTTTTTT      KKK\n\n  MMM MMMM MMM  III  KKK  KKK  RRRRRR     OOOOOO      TTT     III  KKK  KKK\n\n  MMM  MM  MMM  III  KKKKK     RRR  RRR  OOO  OOO
    TTT     III  KKKKK\n\n  MMM      MMM  III  KKK KKK   RRRRRR    OOO  OOO     TTT     III  KKK KKK\n\n  MMM      MMM  III  KKK  KKK  RRR  RRR   OOO
OOO      TTT     III  KKK  KKK\n\n\n  MikroTik RouterOS 6.47.10 (c) 1999-2021       http://www.mikrotik.com/\n\n[?]             Gives the list of ava
ilable commands\n\ncommand [?]     Gives help on the command and list of arguments\n\n\n[Tab]           Completes the command/word. If the input is a
mbiguous,\n\n                a second [Tab] gives possible options\n\n\n/               Move up to base level\n\n..              Move up one level\n\
n/command        Use command at the base level\n\n\x1b[9999B\n\x1b[9999B**************************************************************\n*
                                                *\n*    ATENCION:  Este equipo es propiedad de xxxxxxxxx     *\n*     El uso no autorizado esta es
trictamente prohibido.     *\n*   Todos los usuarios son legalmente responsables de sus    *\n* acciones sobre el sistema y toda actividad sera regis
trada *\n*                                                            *\n**************************************************************\n\x1bZ  \x1b[
6n\n\n\n\n<CID_XXXXX_PPAL] >                                       \n<CID_XXXXX_PPAL] > '

redispatch(net_connect, device_type='mikrotik_routeros')
#this last command throw this exception: 
netmiko.exceptions.ReadTimeout:

Pattern not detected: '(>|>)' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value. 

Alternatively, after confirming connection to the device, I have tried to send commands to the mikrotik through a write channel after to send the password but in the output I see that it simply writes the command and does not return anything.

if '>' in output or '#' in output:
    # i'm connected to the mikrotik (theoricaly)
    mikrotik_command = 'system resource print\n'
    net_connect.write_channel(mikrotik_command)
    time.sleep(2)  

    # read comand output
    output = net_connect.read_channel()
    print("Output of 'system resource print':", output)
else:
    print("No se pudo conectar correctamente al MikroTik.")

This is what i Get in the console: 

Output of 'system resource print': system resource print

If i try send_command throw this exception:

>>> net_connect.send_command('system resource print')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/netmiko/utilities.py", line 592, in wrapper_decorator
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/netmiko/base_connection.py", line 1790, in send_command
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout:
Pattern not detected: {JUNIPER_PROMPT} in output.

Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.

As you see, I've tried the solution there redispatch feature, but doesn't work for me (apparently because of the device pattern). And alternatively using the write channel method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant