免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Shell expect
最近访问板块 发新帖
查看: 2472 | 回复: 8
打印 上一主题 下一主题

expect [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-21 16:48 |只看该作者 |倒序浏览
expect <<!
set timeout -1
spawn ssh czcb@10.10.17.3
     expect {
                "*(yes/no)?*" {send -- "yes\r"; exp_continue}
                "*password:*" {send -- "kjk123\r"}
                }
send "touch a b c d e\r"
send "exit\r"
!

为什么 在10.10.17.3 机器上没建立起来呢

论坛徽章:
0
2 [报告]
发表于 2011-06-21 17:07 |只看该作者
真心求教

论坛徽章:
0
3 [报告]
发表于 2011-06-21 17:22 |只看该作者
set timeout -1  是什么意思?确定这样写对?
应该是set timeout 1 吧

论坛徽章:
0
4 [报告]
发表于 2011-06-21 17:29 |只看该作者
-1 永不超时吧

论坛徽章:
0
5 [报告]
发表于 2011-06-21 17:44 |只看该作者
哦,又学习了!
两种方式,如果你这样写的话。需要这样:
     expect {
                "*(yes/no)?*" {send -- "yes\r"; exp_continue}
                "*password:*" {send -- "123456\r"}
                }
expect "]"
send "touch a b c d e\r"
expect "]"
send "exit\r"

或者set timeout 1  ,send password后面也加上exp_continue。我这里需要这样写,因为经常需要跳转两次,就会出现两次yes/no和password

两种方法我测试过了,应该没有问题

论坛徽章:
0
6 [报告]
发表于 2011-06-21 17:49 |只看该作者
expect <<!
set timeout -1
spawn ssh julia@10.10.17.3
     expect {
                "*(yes/no)?*" {send -- "yes\r"; exp_continue}
                "*password:*" {send -- "kjk123\r"}
                }
expect "*"
send "touch a b c d e\r"
send "exit\r"
!

这样为什么不行

论坛徽章:
0
7 [报告]
发表于 2011-06-21 17:51 |只看该作者
还是不行

论坛徽章:
0
8 [报告]
发表于 2011-06-21 18:19 |只看该作者
默认的使用“*”好像是不行的,csh特性,不是十分清楚。
但是使用-re参数后使用单独的“*”是报错的。
老兄,踏踏实实的写进去期待的字符呗,非要完全省事啊?

我提到的两种方法在我机器上测试是没有问题的
  1. [root@bill cu]# more expect.sh
  2. #!/bin/bash
  3. expect << !
  4. set timeout -1
  5. spawn ssh bill@10.2.54.77
  6.      expect {
  7.                 "(yes/no)?" {send -- "yes\r"; exp_continue}
  8.                 "password:" {send -- "123456\r"}
  9.                 }
  10. expect  "]"
  11. send "touch a b c d e\r"
  12. expect  "]"
  13. send "exit\r"
  14. !

  15. [root@bill cu]# sh expect.sh
  16. spawn ssh bill@10.2.54.77
  17. Address 10.2.54.77 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
  18. bill@10.2.54.77's password:
  19. Last login: Tue Jun 21 18:06:51 2011 from 10.2.54.77
  20. [bill@bill ~]$ touch a b c d e
  21. [bill@bill ~]$ [root@bill cu]#
  22. [root@bill cu]# ll /home/bill/
  23. total 4
  24. -rw-rw-r-- 1 bill bill    0 Jun 21 18:17 a
  25. -rw-rw-r-- 1 bill bill    0 Jun 21 18:17 b
  26. -rw-rw-r-- 1 bill bill    0 Jun 21 18:17 c
  27. -rw-rw-r-- 1 bill bill    0 Jun 21 18:17 d
  28. -rw-rw-r-- 1 bill bill    0 Jun 21 18:17 e
  29. drwxrwxrwx 2 bill bill 4096 Feb 23 04:45 test
  30. [root@bill cu]#
复制代码

论坛徽章:
0
9 [报告]
发表于 2011-06-21 19:01 |只看该作者
本帖最后由 南极雨 于 2011-06-21 19:06 编辑

回复 1# 267020090


这样测试一下:
expect <<!
set timeout -1
spawn ssh czcb@10.10.17.3
     expect {
                "*(yes/no)?*" {send -- "yes\r"; exp_continue}
                "*password:*" {send -- "kjk123\r"}
                }
send "touch a b c d e\r"
send "exit\r"
interact
!

还有password: 后面就没有了,如果有的话应该是写成" .* "(个人认为,expect的正则不是太了解, 路过的知道的请指正!)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP