Chinaunix

标题: [expect] 请教如果在纯expect 脚本中包含文件 [打印本页]

作者: vdlee    时间: 2010-05-31 15:03
标题: [expect] 请教如果在纯expect 脚本中包含文件
本帖最后由 vdlee 于 2010-06-01 10:07 编辑

比如shell 使用

.  function # 引用文件

纯expect 脚本中, 他是怎么做到的呢。

有一个问题,我是在坛子里面翻到的一个老帖,

#!/bin/sh
expect << EOF
spawn ssh .....
`while read i j; do
echo expect \"$i\"
echo send \"$j\\\r\"
done < expect.txt`
expect eof
EOF

我将命令写在一个 expect.txt文本中,

*#         ifconfig
*#         netstat

这样不会有问题,这里用的是 shell 和  expect 混用,有没有办法只使用 expect 实现呢?

这样如果expect 执行的命令部分需要使用变量该怎么办?
*#         使用 $host $ip 等,这些变量该如何传递expect 脚本内部去?

(不过expect 主要是交互操作哦, 变量可能不可以跨度主机吧?)
作者: fllintel    时间: 2010-05-31 15:46
你 set 就可以了
eg.
     set host xxxx
     set  ip  xxxx
作者: vdlee    时间: 2010-05-31 17:22
expect 大牛们出来点拨下撒。
作者: vdlee    时间: 2010-06-01 10:08
各位仙家都去仙游了吗
作者: chinaboywg    时间: 2011-02-09 15:48
#!/usr/bin/expect

if {$argc!=3} {
send_user "Usage: $argv0 {Array IP} {Password} {CMD}\n\n"
exit
}
#log_file ./out
set IP [lindex $argv 0]
set Password [lindex $argv 1]
set CMD [lindex $argv 2]

spawn ssh admin@$IP
#spawn ssh root@$IP
#expect {
#"Are you" { send "yes\n"
#          }
#}
#set timeout 50
expect {
        "assword:" {
                exec sleep 1
                send "${Password}\r"
                        }
        "*continue connecting*" {
                         exec sleep 1
                         send "yes\r"
                         expect  "*Password:" {
                                 exec sleep 1
                                  send "${Password}\r"
                                 }         
                        }
}
#exec sleep 1
#send "${Password}\r"
expect {
"*Password*" { send_user "\n1assword error!\n"
               exit
             }
"*already*" { send_user "\n2:Repeated login!\n"
              exit
            }
"OceanStor: admin>" { send "${CMD}\r"
                    }
}
#send_user "sleep"
#expect "OceanStor: admin>"
#send "${CMD}\r"
#exec sleep 5

expect         "*>"
#send "exit\r"
send "exit\r"
#send "exit\r"
expect "*closed*"
exit
#exec sleep 10




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