![:em20 :em20](./images/smilies/em20.gif)
在折腾了很久以后,终于领悟了expect中的位置变量,这才得以在expect 中使用bash的变量。
bash部分
[bash]#!/bin/bash
#依次是服务器 、用户名 、密码
a=192.168.0.101
b=joe
c=123456
#调用foo.exp脚本
./foo.exp $a $b $c [/bash]
脚本名为foo.exp(这部分写得有些粗糙
![:em03 :em03](./images/smilies/em03.gif)
[bash]#!/usr/bin/expect -f
set timeout -1 #很明显这是永远不退出的意思,若设定成30,则30秒后退出
#依次是 第一、二、三个变量
set server [lrange $argv 0 0]
set user [lrange $argv 1 1]
set _pw [lrange $argv 2 2]
spawn ssh -l $user $server
expect {
"*(yes/no)*" { send "yes\r" }
"*password*" { send "$_pw\r" }
}
expect "*Last login*" { interact }
[/bash]
引用:
作者: hjp0021
如:从shell脚本中得到了几个变量的值,接着在脚本中调用写好的expect程序登陆到远程设备上执行命令,该expect程序要用到前面shell得到的变量值。如何才能传递过来?
谢谢!
use position variables. Such as:
代码:
In shell: foo.exp $a $b $c In expect: set var1 [lrange $argv 0 0]
Then $var1 in expect takes the value of $a in shell script.
参考资料:[lrange $argv 0 0]表示第1个参数,[lrange $argv 0 4]为第一个到第五个参数。
标题: 如何在expect与shell之间传递变量?
expect编程例子