linux 脚本交互

来源:互联网 发布:linux www服务器搭建 编辑:程序博客网 时间:2024/06/03 03:18

yum install expect

首行不是 #!/bin/bash ,注意

send 密码时 后面要么加个\r,要么加个\n

#!/usr/bin/expect
spawn scp root@192.168.137.18:/dev/acq200/data/03 /data1/abc/
set timeout 3
expect "password:"
send "123456\n"
interact

更详细的:

http://www.thegeekstuff.com/2010/10/expect-examples/

http://www.thegeekstuff.com/2010/12/5-expect-script-command-line-argument-examples/

http://wenku.baidu.com/view/64eff008581b6bd97f19ea03.html

后面加不加 -f的问题 下面这个可以测试:

-c的意思是用命令行执行

expect also allows you to execute it directly in the command line using -c 

echo '#! /usr/bin/expect -f
puts aaa' >flagf.exp
chmod +x flagf.exp
./flagf.exp -c 'puts 456'

输出 

456
aaa

echo '#! /usr/bin/expect
puts aaa' >flagf.exp
./flagf.exp -c 'puts 456'

输出

aaa

You can make the expect not interpret the command line arguments using — flag.

加了-f 会把你后面的当做参数而不是 expect的选项。

$ cat  print_cmdline_args.exp#!/usr/bin/expectputs 'argv0 : [lindex $argv 0]';puts 'argv1 : [lindex $argv 1]';
While executing the above script, pass the command line options, which will be treated like an argument (instead of expect options) as shown below.
$ expect print_cmdline_args.exp -d -cargv0 : -dargv1 : -c

0 0