Community discussions

MUM Europe 2020
 
tega2rule
just joined
Topic Author
Posts: 1
Joined: Mon Jun 05, 2017 10:46 am

python telnetlib not working on Mikrotik routerOs

Mon Jun 05, 2017 11:36 am

please can some help me out here, the below Python script was suppose to log in to my mikrotik router 750 ver6.30.2 and disable interface 4, but it is working. i have tried several modifications, but cant get it to work. from the router log, the sript is able to log in, but its unable to carryout the configuration change
import getpass
import telnetlib
import time

HOST = "192.168.1.5"
user = input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Login: ")
tn.write(user.encode('ascii') + b"\n")
time.sleep(1)
#tn.write(b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') +  b"\n")
time.sleep(1)
tn.read_until(b"] > ")
print ("[OK] Login successful")
time.sleep(1)
tn.write(b" /interface disable 4"+ b"\n")
print(tn.read_all().decode('ascii'))
 
idlemind
Forum Guru
Forum Guru
Posts: 1115
Joined: Fri Mar 24, 2017 11:15 pm
Location: USA

Re: python telnetlib not working on Mikrotik routerOs

Mon Jun 26, 2017 5:33 pm

You should consider at least SSH and key based authentication or the MikroTik API to make your changes.

TELNET passes everything in clear text, including that password of yours.

Side-note: I wasn't able to get the telnet package from PIP to install on my machine (Python3) to test. I'm not sure if it is compatible.
 
User avatar
che
Frequent Visitor
Frequent Visitor
Posts: 94
Joined: Fri Oct 07, 2005 1:04 pm

Re: python telnetlib not working on Mikrotik routerOs

Mon Jun 26, 2017 8:06 pm

Agreed with idlemind, avoid telnet like a plague. But in case you are doing it just on a local device, here is something you can tinker with to fix that script assuming login works like you stated:
mtcommand = "/interface disable 4"
prompt = ">"

...

tn.read_until(prompt)
time.sleep(4)
tn.write(mtcommand.encode("UTF-8")+"\r\n")

Use "paramiko" library and create ssh connection if you want to write a Python script for production.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5970
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: python telnetlib not working on Mikrotik routerOs

Tue Jun 27, 2017 10:27 am

You forgot that RouterOS terminal by default will output pretty lines with colour codes. Either you take this into account or turn off colours and/or enable dumb mode.
See the manual for console login options
https://wiki.mikrotik.com/wiki/Manual:C ... in_options
 
savage
Forum Guru
Forum Guru
Posts: 1220
Joined: Mon Oct 18, 2004 12:07 am
Location: Cape Town, South Africa
Contact:

Re: python telnetlib not working on Mikrotik routerOs

Tue Jun 27, 2017 9:04 pm

You forgot that RouterOS terminal by default will output pretty lines with colour codes
There's nothing pretty about it. It's a HUGE PITA :evil: Even with +cti, it's still not a "dumb" terminal.
Regards,
Chris
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5970
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: python telnetlib not working on Mikrotik routerOs

Wed Jun 28, 2017 3:28 pm

There's nothing pretty about it. It's a HUGE PITA :evil:
I am sorry to hear that it feels ugly for you, unfortunately we cannot choose colours to fit everyone's taste. We are open to suggestions, so if you have something in mind, feel free to suggest what other colour scheme to use.
 
savage
Forum Guru
Forum Guru
Posts: 1220
Joined: Mon Oct 18, 2004 12:07 am
Location: Cape Town, South Africa
Contact:

Re: python telnetlib not working on Mikrotik routerOs

Wed Jun 28, 2017 3:34 pm

I'm sorry you feel I'm complaining about "colors."

Drop the ANSI completely from the terminal. Like any other sane network vendor.
Regards,
Chris
 
idlemind
Forum Guru
Forum Guru
Posts: 1115
Joined: Fri Mar 24, 2017 11:15 pm
Location: USA

Re: python telnetlib not working on Mikrotik routerOs

Thu Jun 29, 2017 6:56 am

Not to derail the thread anymore but I actually really like the ANSI coloring. It feels way more readable than the "other" vendors.

Who is online

Users browsing this forum: aboiles, AhmadSyahmi, Google Feedfetcher, mkx and 47 guests