python implements the telnet client method

  • 2020-05-10 18:22:52
  • OfStack

This article illustrates an example of how python implements the telnet client. Share with you for your reference. The details are as follows:

python implements telnet client program, python comes with one telnetlib module, which can realize telnet operation through its Telnet class


import getpass
import sys
import telnetlib
HOST = "hostname"
user = raw_input("Enter your remote account:")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
  tn.read_until("Password: ")
  tn.write(password + "\n")
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()

I hope this article has been helpful to your Python programming.


Related articles: