expect一些资料

在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打开文件
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%

<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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值