先下载安装包:tcl: http://prdownloads.sourceforge.net/tcl/tcl8.5.10-src.tar.gz
expect: http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download。
安装tcl:
tar zxvf tcl8.5.10-src.tar.gz //按实际tar包名写 cd tcl8.5.10/unix //按实际文件名 ./configure --prefix=/usr/local/tcl make && make install |
安装完tcl再安装expect:
tar zxvf expect5.45.tar.gz //按实际tar包名写 cd expect5.45 //按实际文件名 ./configure --prefix=/usr/local/expect --with-tcl=/usr/local/tcl/lib --with-x=no make && make install |
Demo: A机给若干主机发送文件,存放这些主机的的位置:hosts.txt(内容:B,C,D…),假设密码都是一样的:
#/bin/bash a=`cat hosts.txt` #读入的文件,保存着主机的信息 OLD_IFS="$IFS" IFS="," arr=($a) #拆分后的主机数组 IFS="$OLD_IFS" #将分隔符恢复 passwd='5172551' #统一的密码 sendfile='demo.sh' #要发送的文件 for s in ${arr[@]} do expect <<-EOF set time 30 spawn scp demo.sh root@$s:~/.ssh expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "$passwd\r" } } expect eof #结束expect EOF done |