Linux批量scp命令执行工具 可批量对多个Linux主机执行传输文件命令 方便省力 适合统一化管理Linux服务器
- expect的安装(参考资料 博客园)
#expect是在Tcl基础上创建起来的,所以在安装expect前我们应该先安装Tcl
wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz
tar xfvz tcl8.4.11-src.tar.gz
cd tcl8.4.11/unix
./configure --prefix=/usr/tcl --enable-shared
make
make install
#安装完毕以后,进入tcl源代码的根目录,把子目录unix下面的tclUnixPort.h copy到子目录generic中
#expect 安装 (需Tcl的库)
wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download
tar xzvf download
cd expect5.45
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.4.11/generic
make
make install
ln -s /usr/tcl/bin/expect /usr/expect/bin/expect
#!/usr/bin/bash
func_expect(){
/usr/expect/bin/expect -c "
set timeout 30;
spawn $*;
expect {
"(yes/no)?" {send "yes
";exp_continue}
"*ssword:" {send "$pass
";exp_continue}
#"*~]$" {send "df -h
exit
";interact}
}
"
}
func_exec(){
command="scp $1 $2@$3:$4"
echo -e " 33[36m@@@@@@@@@@@@@@@@@@@@@@@@命 令 : $command 33[0m"
func_expect $command
}
echo ' '
if [ $# -ne 2 ];then
echo -e "
wrong call !
for eample: $0 file cfg
"
exit 1
else
sdir='.'
sfile=$sdir/$1
echo -e " 33[31m########################目 录 : $sdir 33[0m"
echo -e " 33[31m########################文 件 : $sfile 33[0m"
echo -e " 33[31m########################参数个数 : $# 33[0m"
cat $2 | grep -v '^#' | awk '{print $1,$2,$3,$4,$5}' | while read type ip user pass tdir
do
echo '============================================================'
func_exec $sfile $user $ip $tdir
sleep 1
done
fi
echo ' '
###############################################
#备注:seq ip/hostname user pass dir###########
1 f2 root 123 /root/
2 f3 root 123 /tmp/
3 192.168.6.203 root 123 /root/
###############################################