标签:
#encoding=utf-8 import telnetlib import time def do_telnet(Host, username, password, finish, commands): # 连接Telnet服务器 tn = telnetlib.Telnet(Host, port=23, timeout=10) tn.set_debuglevel(2) # 输入登录用户名 tn.read_until(‘ENTER USERNAME < ‘) tn.write(username + ‘\r\n‘) # 输入登录密码 tn.read_until(‘ENTER PASSWORD < ‘) tn.write(password + ‘\r\n‘) # 登录完毕后执行命令 tn.read_until(finish) for command in commands: result = tn.write(‘%s\r\n‘ % command); #执行完毕后,终止Telnet连接(或输入exit退出) tn.read_until(‘COMMAND EXECUTED‘) tn.close() # tn.write(‘exit\n‘) if __name__==‘__main__‘: # 配置选项 Host = ‘10.68.165.25‘ # Telnet服务器IP username = ‘SYSTEM‘ # 登录用户名 password = ‘SYSTEM‘ # 登录密码 finish = ‘LEVEL COMMAND <___>‘ # 命令提示符 commands = [‘ZUSI;‘] do_telnet(Host, username, password, finish, commands)
下一步:如何将write的执行结果输出???
标签:
原文地址:http://www.cnblogs.com/jp927/p/4661947.html