1 开启远程机器 telnet 服务端和客户端

2 开启远程机器 telnet 服务

3 尝试本机 cmd 窗口操作看是否可以成功,本机 telnet 记得开启

4 python 调用 下面的代码

# encoding UTF-8
import telnetlib, sys
from time import sleep
import threading

def telnetdo(HOST=None, USER=None, PASS=None, COMMAND=None): #define a function

    tn = telnetlib.Telnet() #
    try:
        tn.open(HOST)

    except:
        print "Cannot open host"
        return

    tn.write('\r\n')
    tn.read_until("login:")
    tn.write(USER+'\r\n')
    print tn.read_until("password:")
    tn.write(PASS +'\r\n')
    print tn.read_until(">")
    tn.write(COMMAND + '\r\n')

    tmp = tn.read_all()
    tn.close()
    return tmp.decode('GBK')

if __name__ == '__main__':
    t1=threading.Thread(target=telnetdo,args=('ipadress','username','passwd','start.bat'))
    t1.setDaemon(True)
    t1.start()
    print '*****************'
    #do something test
    sleep(90)


↙↙↙阅读原文可查看相关链接,并与作者交流