在expect中通过exec cmd 来执行系统命令。
% exec date 2014年 04月 22日 星期二 16:00:39 CST % exec date > date.txt % exec cat date.txt 2014年 04月 22日 星期二 16:00:53 CST
% puts "my name is [exec whoami]" my name is root
expect中文件操作
1.打开文件的模式
#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
}
读取文件内容常用代码#read指令也是用来读取文件内容的,读取方式是读取固定的字符;gets是逐行读取 % set file [open /root/ip.list] file9 % read $file 5 192.1