#!/usr/bin/expect

#Author Aladin

#date 20140504

#auto input passwd

#Version 1.0

spawn scp -P22  /root/shell_test/123.txt root@192.168.0.80:/tmp/

set timeout 100

expect "root@192.168.0.80's password:"

set timeout 10

send "123456\r"

#expect eof

interact


#注意:服务器需要安装expect

一、源码包安装要安装expect,linux expect的安装

1.安装相应的包

   yum install -y tcl tclx tcl-devel

2.下载expect-5.43.tar.gz包(我这里用的这个包,大家也可以用别的)

根据参数,运行./configure
./configure --with-tcl=/usr/lib --with-tclinclude=/usr/include/tcl-private/generic
3.make && make install  安装完毕


二、yum 安装

yum install -y expect


问题处理:

./auto_scpfile.sh: line 2: spawn: command not found

couldn't read file "password:": no such file or directory

./auto_scpfile.sh: line 8: send: command not found

./auto_scpfile.sh: line 10: interact: command not found

这样的话,脚本要指定expect:  #!/usr/bin/expect