Linux下scp命令加强版 优化scp命令 批量对多个Linux主机传输文件

例如搭建集群或者区块链或者分布式,需要把文件传输到其它多台机器上,而且这个步骤可能会重复n次,通过此脚本即可解决

Linux批量scp命令执行工具 可批量对多个Linux主机执行传输文件命令 方便省力 适合统一化管理Linux服务器


所需( expect,sscp.sh,spd )

#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\r\";exp_continue}
    	  \"*ssword:\" {send \"$pass\r\";exp_continue}
          #\"*~]\$\" {send \"df -h\r exit\r\";interact}
	}
 "
}

func_exec(){
 command="scp $1 $2@$3:$4"
 echo -e "\033[36m@@@@@@@@@@@@@@@@@@@@@@@@命    令 : $command\033[0m"
 func_expect $command
}

echo '                                                    '
if [ $# -ne 2 ];then
 echo -e "\nwrong call !\nfor eample: $0 file cfg\n"
 exit 1
else
 sdir='.'
 sfile=$sdir/$1
 echo -e "\033[31m########################目    录 : $sdir\033[0m"
 echo -e "\033[31m########################文    件 : $sfile\033[0m"
 echo -e "\033[31m########################参数个数 : $#\033[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/
###############################################

直接下载


猜你喜欢

转载自blog.csdn.net/u013938484/article/details/80755827