Python3 telnetlib - telnet with username and password and then execute other commands

1.2k Views Asked by At

I would like to be able to telnet, input login and password credentials and then execute commands once connected but my code seems to not continue after password is entered.

import getpass
import telnetlib

HOST = "172.25.1.1"
user = input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until(b"login: ")
tn.write(user.encode('utf-8') + "\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('utf-8') + "\n")

tn.write(b"command to be issued")
print(tn.read_all())

with this code I want to telnet, input login credentials, input password, and once connected issue a command

1

There are 1 best solutions below

0
bgardne7 On

Looks like the problem was a timing issue, I needed to import time and add time.sleep(2) after issuing the command before print(tn.read_all()).