In python, The telnetlib module that supports Telnet is a built-in module and can be directly imported. Its basic usage is relatively simple.
# Encoding = utf-8def do_telnet (host, username, password, finish, commands): Import telnetlib ''' telnet remote login: windows client to connect to Linux Server ''' # connect to telnet server Tn = telnetlib. telnet (host, Port = 23, timeout = 10) tn. set_debuglevel (2) # enter the logon username tn. read_until ('login: ') tn. write (username + '\ n') # enter the logon password tn. read_until ('password: ') tn. write (password + '\ n') # Run the TN command after logon. read_until (finish) for command in commands: tn. WRI Te ('% s \ n' % command) # After the execution is complete, terminate the telnet connection (or enter exit to exit) tn. read_until (finish) tn. close () # tn. write ('exit \ n') If _ name __= = '_ main _': # configuration option host = '10. 255.254.205 '# telnet server ipusername = 'admin' # login Username Password = 'dell1950' # login password finish = ':~ $ '# Command prompt commands = ['echo "test"'] do_telnet (host, username, password, finish, commands)
Port and timeout are optional parameters, while timeout only takes effect when initializing the socket connection. Once the connection is successful, it does not take effect if it waits, for example, if the returned content is not consistent with the specified content when the read_until method is used to obtain the content, a prompt will be displayed, and timeout will not work at this time, this socket connection will remain, and will never die.
So how can we solve this problem? In fact, there is also a primitive method, that is, to use the sleep method instead of the read_until method, so there will be no such situation, because the point will be self-input, at most, that is, the desired result is not obtained at the end, but this method is unstable and has poor compatibility. Another method is to use a thread to start this function and then set timeout for the Child thread, in this way, the telnet connection can be indirectly controlled.
Import threading pars = replace_db_keyworlds (vars_dict, pars) configs = pars. split (R' @ ') host = configs [0]. encode () user = configs [1] passwd = configs [2] Finish = configs [3] commands = configs [4]. split (R' \ n') Th1 = threading. thread (target = do_telnet, argS = (host. encode ('utf-8'), user. encode ('utf-8'), passwd. encode ('utf-8'), finish. encode ('utf-8'), commands) th1.start () th1.join (20) #20 seconds timeout
Another note is that all strings passed to the Telnet method are decoded once, so if you pass the string that needs to be written in the past to be decoded Unicode, therefore, an error will be reported. Therefore, UTF-8 is compiled before the sent string is passed. Other characters are not supported. I only tried UTF-8 and didn't see the source code.
In addition, it seems that there is a third-party pexpect module that supports Telnet and a series of protocol connections and interactive communication, but this module is not enough to learn. Please note here.