Python Forum
telnet question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: telnet question (/thread-16985.html)



telnet question - jacklee26 - Mar-23-2019

Hi All,
i have some question related to telnet this script. Are we able to login and don't close the session. For example login, and user is able to type in command like that. I saw most of the resource on net, only show login and run some command. Is it possible to just login automatic and stop.
For my script, it will login and run the command. But what i wants is just login, and don't exit the session.

Is python able to do this?

Name: telnet_demo.py

Tesed in python3.5
"""
import telnetlib

HOST = "192.168.1.253"
user = "root"
password = "arrisc4"


def command(con, flag, str_=""):
    data = con.read_until(flag.encode())
    print(data.decode(errors='ignore'))
    con.write(str_.encode() + b"\n")
    return data

tn = telnetlib.Telnet(HOST)
command(tn, "Login:", user)
if password:
    command(tn, "Password:", password)
command(tn, "#", "en")
#command(tn, "$", " exit")
#command(tn, "$", "")
tn.close()



RE: telnet question - nilamo - Mar-29-2019

What are you trying to achieve?
I don't think the OS will just close any open sockets when the program completes.


RE: telnet question - jacklee26 - Mar-30-2019

i just wants to automatic telnet login.