Chinaunix

标题: expect 脚本问题 [打印本页]

作者: jiean    时间: 2009-09-23 14:26
标题: expect 脚本问题

  1. proc do_console_login {login pass} {

  2.         set timeout 5
  3.         set done 1
  4. #        set timeout_case 0

  5.         while { $done } {
  6.                 expect {
  7.                         "(yes/no)?" {
  8.                                         send "yes\n"
  9.                                         expect "assword:"
  10.                                         send "$pass\n"
  11.                                         incr done
  12.                                         send_user "Login Successfully...\n"
  13.                         }
  14.                         "*assword:" {
  15.                                         send "$pass\n"
  16.                                         incr done
  17.                                         send_user "Login Successfully...\n"
  18.                                 }
  19. #                       timeout {
  20. #                                switch -- $timeout_case {
  21. #                                        0 { send "\n" }
  22. #                                        1 {
  23. #                                                send_user "Send a return...\n"
  24. #                                                send "\n"
  25. #                                        }
  26. #                                        2 {
  27. #                                                puts stderr "Login time out...\n"
  28. #                                                exit 1
  29. #                                        }
  30. #                                }
  31. #                                incr timeout_case
  32. #                        }
  33. #                }
  34.                 }
  35.         }
  36. }

  37. proc do_exec_cmd {} {
  38.         expect ":~>"
  39.         send "uname -a\n"
  40.         expect ":~>"
  41.         send "exit\n"
  42.         expect eof
  43. }
  44. if { $argc !=3 } {
  45. puts stderr "Usage: command host-address username host-password\n"
  46. exit 1
  47. }


  48. set host [ lindex $argv 0 ]

  49. set name [ lindex $argv 1 ]

  50. set password [ lindex $argv 2 ]

  51. spawn ssh $name@$host

  52. do_console_login $name $password
  53. do_exec_cmd

  54. close

  55. exit 0
复制代码




这是一个远程登录到终端执行SHELL 的expect程序,不知道为什么提示proc do_console_login无法执行,请帮忙改下

如果我要是用expect 远程登录到服务器上,执行编译安装软件,是不是得把timeout设置的时间长一点啊,才能阻止超时问题
作者: phoenixson    时间: 2009-09-23 23:37
yes/no "/"是不是需要写成\/
作者: waker    时间: 2009-09-24 08:12
\n ==> \r
作者: jiean    时间: 2009-09-24 14:21
还是不行提示第一个proc无法运行,括号处有问题
invalid command name "}"
    while executing
"}"
作者: cute_orange    时间: 2009-09-24 17:12
标题: 回复 #4 jiean 的帖子
多了个}




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2