#!/bin/bash
a=$1
b=$2
ip=$3
port=$4
passwd=$5
/bin/rpm -qa|/bin/grep -q expect
if [ $? -ne 0 ];then
echo "please install expect"
exit
fi
if [ $# -ne 5 ];then
echo "must 5 parameter,1:source file,2:object file,3:object ip 4:object port,5:passwd"
exit
fi
expect -c "
spawn scp -P $port $a root@$ip:$b
expect {
\"*assword\" {set timeout 300; send \"$passwd\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
==================================================
1.建立机器间完全信任关系
其实只要用一条单行命令就可以完成步骤2,它被commandlinefu.com的用户投票选为十大最酷的Linux单行命令之一:
2.expect脚本
#!/usr/bin/expectspawn scp -P 22 20111111.log root@192.168.0.1:/log/expect { "*assword" {set timeout 300; send "passwd\r";} "yes/no" {send "yes\r"; exp_continue;}}expect eof
上面是一个独立的expect脚本文件,如果像把这段脚本嵌入其它shell脚本中就要用到expect -c