在expect中通过exec cmd 来执行系统命令。
1
2
3
4
5
|
% exec date
2014年
04月 22日 星期二 16:00:39 CST
% exec date > date .txt
% exec cat date .txt
2014年
04月 22日 星期二 16:00:53 CST
|
1
2
|
%
puts "my
name is [exec whoami]"
my
name is root
|
expect中没有小括号(),所有的if/else, while, for的条件全部使用大括号{}, 并且{
与左边要有空格,否则会报错。另,else 不能单独占一行,否则会报错。
记得左大括号{ 的左边要有空格,否则会报错
1.
{ }大括号:保留所有字符原有的意思,而不做解释,类似于shell中的单引号
举例:set var {a$b[set c 3]\tddd} 将{}中的一坨东西直接赋值给var
2. {}的另外一个作用是可以续行,(其实是左大括号)
举例:
if {$count < 0} {
break;
}
而以下的写法则是错误的:
if {$count < 0}
{
break;
}
3. []中括号:执行命令
举例:
set count [expr $count - 1 ]
set a [set b 0]
4. ()小括号:expect中没有小括号。所有在C中用的小括号都要换成大括号
expect中文件操作
1.打开文件的模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
% open "/root/jiang.exp" "r"
file4
% open "/root/jiang.exp" "w"
file5
% open "/root/jiang.exp" "a"
file6
%
close file4
%
close file5
%
close file6
% set file [ open /root/date . date w]
file4
%
puts $ file "hello
world"
% exec cat date . date
%
flush $ file
% exec cat date . date
hello
world
[root@zhu
~]
192.168.56.101
192.168.56.102
[root@zhu
~]
#!/usr/bin/expect
set file [ open /root/ip .list]
while {[gets
$ file line]
!= -1} {
puts
$line
}
变量保存读取的内容
while {[gets
$ file line]
!= -1}
}
读取文件内容常用代码
|
1
2
3
4
5
|
% set file [ open /root/ip .list]
file9
% read $ file 5
192.1
|
expect scp传文件不完整解决方法
手动添加set timeout -1设置超时时间为无穷大,加 expect 100%
<code class="hljs tex has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">sendsystem()<span class="hljs-special" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 102, 0);">{</span>
expect -c "
set timeout -1
spawn scp <span class="hljs-formula" style="margin: 0px; padding: 0px; box-sizing: border-box; background-color: rgb(238, 238, 238); font-style: italic;">$ORACLE_BASE/oradata/$</span>ORACLE_SID/system01.dbf oracle@<span class="hljs-formula" style="margin: 0px; padding: 0px; box-sizing: border-box; background-color: rgb(238, 238, 238); font-style: italic;">$S_IP:$</span>ORACLE_BASE/oradata/standby/
expect <span class="hljs-special" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 102, 0);">{</span>
yes/no <span class="hljs-special" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 102, 0);">{</span> send <span class="hljs-command" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 0, 136);">\"</span>yes<span class="hljs-command" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 0, 136);">\r</span><span class="hljs-command" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 0, 136);">\"</span>; exp_continue <span class="hljs-special" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 102, 0);">}</span>
*assword* <span class="hljs-special" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 102, 0);">{</span> send <span class="hljs-command" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 0, 136);">\"</span>oracle<span class="hljs-command" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 0, 136);">\r</span><span class="hljs-command" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 0, 136);">\"</span> <span class="hljs-special" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 102, 0);">}</span>
<span class="hljs-special" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 102, 0);">}</span>;
expect 100<span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">%</span>
expect eof ;
"
<span class="hljs-special" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 102, 0);">}</span></code>