1

with python script I want to make some configuration on mikrotik routers, looks like script is right and no gives errors but ends without printing command outputs

import telnetlib
import time
dev_ip = "172.16.62.160"
user = "admin"
PASSWORD = ""
comm1 = "ip address print"

tn = telnetlib.Telnet(dev_ip, timeout=1)
tn.read_until(b"Login: ")
tn.write(user.encode("ascii") + b'\n')
tn.read_until(b"Password: ")
tn.write(PASSWORD.encode("ascii") + b'\n')
tn.read_until(b">")
time.sleep(1)
tn.write(comm1.encode("ascii") + b"\r\n")
Showcmdoutput = tn.read_very_eager().decode('ascii')
print(Showcmdoutput)
tn.close()
print("DONE")

running on Ubuntu Desktop

  • 1
    If there is even the slightest delay before the router starts responding to the command you sent, `.read_very_eager()` is going to return an empty string - it doesn't wait for data to become available. A short `sleep()` after the command might help, or changing to use `.read_until()` if there's some guaranteed string that will always appear at the end of the command's results. – jasonharper May 17 '21 at 20:24

1 Answers1

1

problem solved after putting:

time.sleep(1)

before Showcmdoutput = tn.read_very_eager().decode('ascii')

tn.write(comm1.encode("ascii") + b"\r\n")
time.sleep(1)
Showcmdoutput = tn.read_very_eager().decode('ascii')