简体   繁体   中英

how can I connect through telnet with python?

I made this program in python to connect with a device that support telnet using 23 port, with user:admin and password:pass:

import getpass

import telnetlib

HOST = '192.168.1.10'
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")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")

tn.write(b"dir\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))

But I have this ouput and the program doesn't connect: Enter your remote account: admin Warning: QtConsole does not support password mode, the text you type will be visible. pass

What I am doing wrong? help please

I am not understanding properly what you are telling but if you want to connect using cmd or powershell then you can use subprocess module just import it and if you want to run cmd commands then just type:

subprocess.run(["dir"])

If you have some argument in the cmd command then you can do this:

subprocess.run(["dir", "-a"]

Remember not to put spaces inside command and remember to use lists if you have more parameter.

Hope it will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM