免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1597 | 回复: 5
打印 上一主题 下一主题

[文本处理] expect 求教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-12-07 18:23 |只看该作者 |倒序浏览
大神好。

我在写一个脚本,希望通过交互式模式,先登录一台跳板机,再从跳板机登录到内网交换机,并采集交换机上的命令行配置。

用expect:


[root@rhel6 /]# vim abc.sh

#!/usr/bin/expect -f
set timeout 10

#登录跳板机
spawn ssh yonghu@10.10.10.75
expect "Password:"
send "mypass"
expect "press Enter to continue..."
send "\r"

#采集交换机数据
expect "Select page:"
send "10.3.0.1"                #登录该主机
expect "Username:"       
send "user1\r"                #登录用户名
expect "Password:"
send "pass1\r"                #密码
expect "switch>"          #普通用户模式
send "enable\r"                #进入特权模式
expect "Password:"
send "pass1\r"
expect "switch#"        #进入到特权模式

send "show run"             -------------我需要将此处show run命令的输出导出到某文件,就类似于 cat a.txt >b.txt一样,请教这个语法咋破?

论坛徽章:
12
IT运维版块每日发帖之星
日期:2015-11-17 06:20:00程序设计版块每日发帖之星
日期:2016-01-19 06:20:0015-16赛季CBA联赛之江苏
日期:2016-01-17 15:31:3915-16赛季CBA联赛之上海
日期:2016-01-16 15:44:3015-16赛季CBA联赛之浙江
日期:2016-01-15 20:38:1815-16赛季CBA联赛之北京
日期:2016-01-09 14:30:15CU十四周年纪念徽章
日期:2016-01-07 12:31:5115-16赛季CBA联赛之四川
日期:2016-01-01 11:49:1515-16赛季CBA联赛之深圳
日期:2015-12-24 14:23:4115-16赛季CBA联赛之山西
日期:2015-12-15 16:22:31技术图书徽章
日期:2015-12-10 17:41:0015-16赛季CBA联赛之北控
日期:2016-02-03 10:03:24
2 [报告]
发表于 2015-12-07 18:53 |只看该作者
expect 里面有个 log_file 文件 的用法,但是貌似是记录整个expect的标准输出

论坛徽章:
2
射手座
日期:2015-11-08 16:55:47操作系统版块每日发帖之星
日期:2015-11-11 06:20:00
3 [报告]
发表于 2015-12-07 18:58 |只看该作者
看上去好高深的样子

论坛徽章:
16
CU十二周年纪念徽章
日期:2013-10-24 15:41:3415-16赛季CBA联赛之广东
日期:2015-12-23 21:21:55青铜圣斗士
日期:2015-12-05 10:35:30黄金圣斗士
日期:2015-11-26 20:42:16神斗士
日期:2015-11-19 12:47:50每日论坛发贴之星
日期:2015-11-18 06:20:00程序设计版块每日发帖之星
日期:2015-11-18 06:20:002015亚冠之城南
日期:2015-11-10 19:10:492015亚冠之萨济拖拉机
日期:2015-10-28 18:47:282015亚冠之柏太阳神
日期:2015-08-30 17:21:492015亚冠之山东鲁能
日期:2015-07-07 18:48:39摩羯座
日期:2014-08-29 23:01:42
4 [报告]
发表于 2015-12-07 21:56 |只看该作者
show run  后面输出有特征标志没有 如果有可以考虑有正则匹配后 抓取到 expect_out(0,string)

Upon  matching  a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the vari-
             able expect_out(buffer).  Up to 9 regexp substring matches are saved in the  variables  expect_out(1,string)  through
             expect_out(9,string).   If  the  -indices  flag  is used before a pattern, the starting and ending indices (in a form
             suitable for lrange) of the 10 strings are stored in the variables expect_out(X,start) and expect_out(X,end) where  X
             is  a  digit, corresponds to the substring position in the buffer.  0 refers to strings which matched the entire pat-
             tern and is generated for glob patterns as well as regexp patterns.  For example, if a process has produced output of
             "abcdefgh\n", the result of:

                 expect "cd"

             is as if the following statements had executed:

                 set expect_out(0,string) cd
                 set expect_out(buffer) abcd

             and "efgh\n" is left in the output buffer.  If a process produced the output "abbbcabkkkka\n", the result of:

                 expect -indices -re "b(b*).*(k+)"

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
5 [报告]
发表于 2015-12-08 08:50 |只看该作者

  1. ...........
  2. send "show run"
  3. set showrun $spawn_id
  4. spawn -open [ open b.txt a+ ]
  5. interact -u $showrun
复制代码

论坛徽章:
5
金牛座
日期:2015-07-03 13:32:00卯兔
日期:2015-07-03 13:32:17程序设计版块每日发帖之星
日期:2015-11-29 06:20:0015-16赛季CBA联赛之同曦
日期:2015-12-15 09:36:06CU十四周年纪念徽章
日期:2016-07-06 17:18:48
6 [报告]
发表于 2015-12-08 09:06 |只看该作者
交换机可以写文件记录吗?

如有可以可以考虑将show run > file.txt

再将file.txt文件copy回来
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP