華為設備開啟telnet
user-interface vty 0 4
authentication-mode aaa
user privilege level 15
protocol inbound all
telnet server enable
aaa
local-user test password cipher test
local-user test service-type telnet
python
# -*- coding: gb2312 -*-
import telnetlib
import time
HOST = "192.168.0.1"
user = "test"
password = "test"
def command(con, flag, str_=""):
data = con.read_until(flag.encode())
print(data.decode('gb2312', errors='ignore'), end="")
# errors -- 設置不同錯誤的處理方案。 end="" 輸出不做換行處理
con.write(str_.encode() + b"\n")
tn = telnetlib.Telnet(HOST, port=23)
command(tn, "Username:", user)
if password:
command(tn, "Password:", password)
command(tn, ">", "sys")
command(tn, "]", )
time.sleep(2)
tn.close()