在shell脚本中使用expect实现telnet登陆路由器

发表时间:2014-02-19 14:22 | 分类:Linux | 浏览:3,373 次

Expect是基于TCL的,作为一个脚本语言,expect能在无需管理员参与的情况下实现自动交互(比如passwd,fsck,telnet等)。expect也能用于自动测试一些应用程序。

这里博主以登陆路由器为例,先简单了解expect实现telnet自动登陆路由器的过程。当然也可以使用expect脚本ssh登陆服务器进行自动化管理。


#!/usr/bin/expect

spawn telnet 192.168.1.1
expect "*Username:*"
send "yourname\r"
expect "*Password:*"
send "yourpwd\r"

send "en\r"
expect "*password:*\r"
send "yourpwd\r"

send "sys show\r"
send "exit\r"

expect eof

运行测试一切正常。

spawn telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
Username:yourname
Password:

TP-LINK > en
Enter password:
TP-LINK # sys show
CPU Used Rate: 17%

TP-LINK > exit
Connection closed by foreign host.

本文标签:,

本文链接:https://www.sijitao.net/1567.html

欢迎您在本博客中留下评论,如需转载原创文章请注明出处,谢谢!

一键脚本 博客历程 留言联系 文章归档 网站地图 谷歌地图
Copyright © 2010-2024 章郎虫博客 All Rights Reserved.