News
发布日期:2023-09-26 浏览次数:1190 来源:WOLF-LAB杨广成
EI CCNP培训企业基础架构-Telnetlib自动备份思科路由器交换机设备配置
【WOLF-LAB网络技术实验室】思科认证CCNA、CCNP、CCIE培训、学习、考试资讯联系网站客服!
如上图,通过python telnetlib模块,将图中路由器/交换机配置备份至本机。
def run_telnet(host, username, password, commands):
# 通过telnet登录网络设备
tn = telnetlib.Telnet(host, port=23, timeout=10)
tn.set_debuglevel(10)
# 输入用户名
tn.read_until(b"Username: ")
tn.write(username.encode("ascii") + b"\n")
# 输入密码
tn.read_until(b"Password: ")
tn.write(password.encode("ascii") + b"\n")
for comment in commands:
tn.write(comment.encode("ascii") + b"\n")
time.sleep(5)
out = tn.read_very_eager().decode("ascii")
print(out)
file = open(file=file_name, mode="a")
file.write(out)
file.close()
tn.close()
if __name__ == "__main__":
comment_list = ["terminal length 0", "show runn"]
run_telnet("10.10.1.1", "admin", "wolf", comment_list)
WOLFLAB官方微信:17316362402
WOLFLAB官方QQ:2569790740
思科认证CCNP EI企业基础架构课程咨询联系WOLF-LAB实验室