In [1]: Host = '1.1.1.1'
In [2]: username = ‘abc’ #用户名
In [3]: password = 'abc’ #密码
In [4]: superpass = 'abc' #超级密码
In [6]: import telnetlib
In [7]: tn = telnetlib.Telnet(Host) #连接交换机
In [8]: tn.read_until('Username:') #一直读到“Username:”
Out[8]: "\r\n******************************************************************************\r\n* Copyright (c) 2004-2012 Hangzhou H3C Tech. Co., Ltd. All rights reserved. *\r\n* Without the owner's prior written consent, *\r\n* no decompiling or reverse-engineering shall be allowed. *\r\n******************************************************************************\r\n\r\n\r\nLogin authentication\r\n\r\n\r\nUsername:"
In [9]: tn.write(username + '\n') #输入用户名
In [10]: tn.read_until('Password:')
Out[10]: '\r\n% Username: timeout expired!\r\nUsername:nimanager\r\nPassword:'
In [11]: tn.write(password + '\n') #输入密码
In [14]: tn.read_until('>') #一直读到“>”提示符
Out[14]: '\r\n
In [17]: tn.write('super\n')
In [18]: tn.write(superpass + '\n') #输入超级密码
In [19]: tn.read_until('>')
Out[19]: 'super\r\nPlease input the password to change the privilege level, press CTRL_C to abort.\r\n Password:\r\nUser privilege level is 3, and only those commands can be used \r\nwhose level is equal or less than this.\r\nPrivilege note: 0-VISIT, 1-MONITOR, 2-SYSTEM, 3-MANAGE\r\n
In [20]: tn.write('display cur\n') #输入 display curr 命令
In [21]: tn.read_until('---- More ----') #获取 display curr 命令执行结果
Out[21]: 'display cur\r\n#\r\n version 5.20, Test 1804\r\n#\r\n sysname BJ\r\n#\r\n clock timezone 8 add 08:00:00\r\n#\r\n super password level 3 ........................................................................................................................................................................................................................................................................................................................................................................................................... ---- More ----'