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
|
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打开文件
%
open
"/root/jiang.exp"
"r"
#默认模式读
file4
%
open
"/root/jiang.exp"
"w"
#写
file5
%
open
"/root/jiang.exp"
"a"
#追加
file6
#close 关闭文件
% close file4
% close file5
% close file6
#puts向文件写内容
%
set
file
[
open
/root/date
.
date
w]
file4
% puts $
file
"hello world"
%
exec
cat
date
.
date
% flush $
file
%
exec
cat
date
.
date
hello world
#gets 或read读取文件
[root@zhu ~]
# expect jiang.exp
192.168.56.101
192.168.56.102
[root@zhu ~]
# cat jiang.exp
#!/usr/bin/expect
set
file
[
open
/root/ip
.list]
while
{[gets $
file
line] != -1} {
puts $line
}
#####################
#gets 文件标志符 变量
变量保存读取的内容
#gets每次读取文件的一行,返回的是读取的字符长度,当读取到最后时返回值为-1
while
{[gets $
file
line] != -1}
# do something with $line
}
读取文件内容常用代码
|
1
2
3
4
5
|
#read指令也是用来读取文件内容的,读取方式是读取固定的字符;gets是逐行读取
%
set
file
[
open
/root/ip
.list]
file9
%
read
$
file
5
192.1
|
expect scp传文件不完整解决方法
手动添加set timeout -1设置超时时间为无穷大,加 expect 100%