python telnetlib not working on Mikrotik routerOs

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'))

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.

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.

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:Console_login_process#Console_login_options

There’s nothing pretty about it. It’s a HUGE PITA :imp: Even with +cti, it’s still not a “dumb” terminal.

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.

I’m sorry you feel I’m complaining about “colors.”

Drop the ANSI completely from the terminal. Like any other sane network vendor.

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