免费注册 查看新帖 |

Chinaunix

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

[学习共享] 麻烦各位指点,expect结尾处理的问题 。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-08-31 09:46 |只看该作者 |倒序浏览
本帖最后由 Iceecream 于 2015-08-31 11:47 编辑

写一个通用的expect程序,从一个文本文档一条条读取命令在远端执行。现在的问题是,命令可以正常执行完,但是不能后续处理了。
  1. if { $argc<5 } {
  2.         send_user "usage: $argv0 <ipaddr> <lowuser> <lowuserpwd> <rootpwd> <cmdfile>\n"
  3.         exit
  4. }
  5. set timeout 30
  6. set ipaddr [lindex $argv 0]
  7. set lowuser [lindex $argv 1]
  8. set lowuserpwd [lindex $argv 2]
  9. set rootpwd [lindex $argv 3]
  10. set cmdfile [ open [lindex $argv 4] ]
  11. set date [lindex $argv 5]
  12. # Logfile
  13. set xjlogfile /tmp/xunjian.log
  14. spawn ssh $lowuser@$ipaddr


  15. expect {
  16.         "yes/no)?\ " {send "yes\r";exp_continue}
  17.         "*assword:\ " {send "$lowuserpwd\r"}
  18. }
  19. expect  "*]$\ " {send "export LANG=en_US.UTF-8\r"}
  20. expect  "*]$\ " {send "su -\r"}
  21. expect  "Password:\ " {send "$rootpwd\r"}
  22. expect  "*]#\ " {send ">$xjlogfile\r"}
  23. expect  "*]#\ " {
  24.         while {[gets $cmdfile cmd] >= 0 } {
  25.                 send "$cmd >>$xjlogfile\r"
  26.                 while 1 {
  27.                         sleep 1
  28.                         expect "]#\ " { break } #send "\r";break
  29.                         }
  30. }
  31. }
  32. #---以下的命令无法顺利执行,有时候会卡在远程服务器上,有时候会自动跳出-----
  33. expect  "*]#\ " {send "scp /tmp/xunjian.log log@1.1.1.1:~/log/ \r"}   #upload logfile
  34. expect  {
  35. "]#\ " {send "exit\r"};exp_continue
  36. "]$\ " {send "exit\r"}}
复制代码
除了加载文本文件里面的命令,如果我还想加一些通用的命令,貌似就实现不了了。


跟进,改成这样就可以了,expect 脾气不好使啊。
  1. # Logfile
  2. #log_file $date/${ipaddr}-${date}.log
  3. expect "*]#\ " {send "cat $xjlogfile\r"}
  4. expect "*]#\ " {send "exit\r"}
  5. #log_file   
  6. expect "*]$\ " {send "exit\r"}
  7. close $cmdfile
  8. expect eof
复制代码

论坛徽章:
14
15-16赛季CBA联赛之辽宁
日期:2019-06-16 15:47:3515-16赛季CBA联赛之广夏
日期:2016-08-13 21:24:352015亚冠之武里南联
日期:2015-07-07 17:37:372015亚冠之萨济拖拉机
日期:2015-07-06 17:07:482015亚冠之全北现代
日期:2015-06-04 13:54:272015亚冠之城南
日期:2015-05-21 15:43:212015年亚洲杯之伊朗
日期:2015-04-25 18:20:362015年亚洲杯之伊朗
日期:2015-04-20 16:06:052015年亚洲杯之科威特
日期:2015-03-07 12:51:26丑牛
日期:2014-12-30 10:26:38申猴
日期:2014-09-28 22:40:18金牛座
日期:2014-09-13 21:12:22
2 [报告]
发表于 2015-08-31 09:52 |只看该作者
本帖最后由 reb00t 于 2015-08-31 09:53 编辑

ansbile 工具完全满足你得需求~  建议一下。没必要写这复杂的expect脚本。


记住一条, expect脚本能实现的,ansible一定能实现。

脚本里面的权限切换,传送文件啊, 远程执行命令啊,ansible都用相应的模块直接调用即可~~~

论坛徽章:
0
3 [报告]
发表于 2015-08-31 09:56 |只看该作者
感谢楼上,我去了解一下,这个是新东西。

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
4 [报告]
发表于 2015-08-31 09:58 |只看该作者
回复 1# Iceecream

#ceated a log file for debug
log_file ...


$ man expect
NAME
       expect - programmed dialogue with interactive programs, Version 5

      ...

       log_file [args] [[-a] file]
             If a filename is provided, log_file will record a  transcript  of
             the session (beginning at that point) in the file.  log_file will
             stop recording if no argument is given.  Any previous log file is
             closed.

             Instead  of  a filename, a Tcl file identifier may be provided by
             using the -open or -leaveopen flags.   This  is  similar  to  the
             spawn command.  (See spawn for more info.)

             The -a flag forces output to be logged that was suppressed by the
             log_user command.

             By default, the log_file command appends to old files rather than
             truncating  them,  for the convenience of being able to turn log‐
             ging off and on multiple  times  in  one  session.   To  truncate
             files, use the -noappend flag.

             The  -info  flag  causes  log_file to return a description of the
             most recent non-info arguments given.

   

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
5 [报告]
发表于 2015-08-31 13:53 |只看该作者
回复 2# reb00t


    那for ...ssh也可以的  也有某些特殊场合,不给用ssh key登录,比如交换机等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP