+ -
当前位置:首页 → 问答吧 → expect

expect

时间:2011-06-21

来源:互联网

expect <<!
set timeout -1
spawn ssh [email protected]
     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 机器上没建立起来呢

作者: 267020090   发布时间: 2011-06-21

真心求教

作者: 267020090   发布时间: 2011-06-21

set timeout -1  是什么意思?确定这样写对?
应该是set timeout 1 吧

作者: dzhxc   发布时间: 2011-06-21

-1 永不超时吧

作者: 267020090   发布时间: 2011-06-21

哦,又学习了!
两种方式,如果你这样写的话。需要这样:
     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

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

作者: dzhxc   发布时间: 2011-06-21

expect <<!
set timeout -1
spawn ssh [email protected]
     expect {
                "*(yes/no)?*" {send -- "yes\r"; exp_continue}
                "*password:*" {send -- "kjk123\r"}
                }
expect "*"
send "touch a b c d e\r"
send "exit\r"
!

这样为什么不行

作者: 267020090   发布时间: 2011-06-21

还是不行

作者: 267020090   发布时间: 2011-06-21

默认的使用“*”好像是不行的,csh特性,不是十分清楚。
但是使用-re参数后使用单独的“*”是报错的。
老兄,踏踏实实的写进去期待的字符呗,非要完全省事啊?

我提到的两种方法在我机器上测试是没有问题的
  1. [root@bill cu]# more expect.sh
  2. #!/bin/bash
  3. expect << !
  4. set timeout -1
  5. spawn ssh [email protected]
  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 [email protected]
  17. Address 10.2.54.77 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
  18. [email protected]'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]#
复制代码

作者: dzhxc   发布时间: 2011-06-21