通过scp批量上传文件,也可以批量在服务器上执行上传文件

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/bin/bash
#Author: 		Castle Liu
#Description:	upload a file or excute a script on remote servers
#Usage:			excute_cmd.sh -l {hosts_list} -f {script_file} -d /tmp

####functions###
#help
#function used to print help info.
help() {
	cat <<EOF
	-l 	preferred,the list file of hosts,1st host,2nd user name,and,optional,3st password for the user.
	-f 	preferred,the file to upload.
	-d 	preferred,directory to place the file uploaded.
	-p 	optional,remote sshd listen port,if not provided,use 22 as default.
	-L 	optional,log file path to save excute log,if not provided,use "excute_cmd.log" as default.
	-S 	optional,selected if chosed to use sudo
	-e 	optional,if selected,will excute the script file 
	-h 	print help info.
EOF
}
#help

#parse_args
#function to parse paremeters.
#do pass in the args from shell,for example:parse_args $*
parse_args(){
	while getopts "hl:f:d:p:L:Se" arg
	do 
		case ${arg} in
			h)
				help
				exit 0
				;;
			l)
				export _HOST_F=${OPTARG}
				;;
			f)
				export _SCRIPT_F=${OPTARG}
				;;
			d)
				export _DIR_R=${OPTARG}
				;;
			p)
				export _PORT_R=${OPTARG}
				;;
			L)
				export _LOG_F=${OPTARG}
				;;
			S)
				export _USE_SUDO=true
				;;
			e)
				export _EXC_F=true
				;;
			?)
				echo "Unknown argument,exit..."
				exit 1
				;;
			esac
	done
	#echo ${_HOST_F},${_SCRIPT_F}
}
#parse_args $*

#scp_file
#function to scp a file to a remote server.
#$1,Remote host ip addresss
#$2,Remote user name
#$3,Remote user password
#$4,The file to be copied local
#$5,Remote loacation of the script to be copied,can be a directory ora absulote file name 
#$6,The remote sshd port in use 
scp_file() {
	_host_r=$1
	_user_r=$2
	_passwd_r=$3
	_script_l=$4
	_script_r=$5
	_port_r=$6
	sed -i "/${_host_r}/d" ~/.ssh/known_hosts
expect  <<  EXPECT_EOF >> .runninglog
	spawn scp -P ${_port_r} ${_script_l} ${_user_r}@${_host_r}:${_script_r}
		expect "continue connecting (yes/no)?" {send "yes\n"}
 		expect  "password:" {send "${_passwd_r}\n"}
	expect off
EXPECT_EOF
}

#excute_script_with_sudo
#function to excute a script on remote server and scp back the running log,sudo will be used.
#$1,Remote host ip addresss
#$2,Remote user name
#$3,Remote user password
#$4,The script to be excuted in remote server,must be a absulote file name
#$5,The remote sshd port in use
excute_script_with_sudo () {
	_host_r=$1
	_user_r=$2
	_passwd_r=$3
	_script_r=$4
	_port_r=$5
	_log_r=`dirname ${_script_r}`"/.tmpexc.log"
expect  <<  EXPECT_EOF >> .runninglog
	spawn ssh  -p ${_port_r} ${_user_r}@${_host_r} -C \""sudo su - -c \"bash ${_script_r} > ${_log_r}\""\"
 		expect  "password:" {send "${_passwd_r}\n"}
 		expect  "*password*" {send "${_passwd_r}\n"}
	expect off
	spawn ssh -p ${_port_r} ${_user_r}@${_host_r} -C \""sudo su - -c \"chown ${_user_r} ${_log_r}\""\"
 		expect  "password:" {send "${_passwd_r}\n"}
 		expect  "*password*" {send "${_passwd_r}\n"}
 	expect off
	spawn scp -P ${_port_r} ${_user_r}@${_host_r}:${_log_r} ./${_host_r}.log.tmp
 		expect  "password:" {send "${_passwd_r}\n"}
	expect off
	spawn ssh -p ${_port_r} ${_user_r}@${_host_r} -C "rm -rf ${_log_r};rm -rf ${_script_r}"
		expect  "password:" {send "${_passwd_r}\n"}
	expect off
EXPECT_EOF
}

#excute_script
#function to excute a script on remote server and scp back the running log,sudo will be used.
#$1,Remote host ip addresss
#$2,Remote user name
#$3,Remote user password
#$4,The script to be excuted in remote server,must be a absulote file name
#$5,The remote sshd port in use
excute_script () {
	_host_r=$1
	_user_r=$2
	_passwd_r=$3
	_script_r=$4
	_port_r=$5
	_log_r=`dirname ${_script_r}`"/.tmpexc.log"
expect  <<  EXPECT_EOF  >> .runninglog
	spawn ssh  -p ${_port_r} ${_user_r}@${_host_r} -C "bash ${_script_r} > ${_log_r}"
 		expect  "password:" {send "${_passwd_r}\n"}
	expect off
	spawn scp -P ${_port_r} ${_user_r}@${_host_r}:${_log_r} ./${_host_r}.log.tmp
 		expect  "password:" {send "${_passwd_r}\n"}
	expect off
	spawn ssh -p ${_port_r} ${_user_r}@${_host_r} -C "rm -rf ${_log_r};rm -rf ${_script_r}"
		expect  "password:" {send "${_passwd_r}\n"}
	expect off
EXPECT_EOF
}

#handle_excute_log
#function to uniq the log file excute remotely and scp back.
#$1,the final log file to be make
#$2,the host list file,using awk -F [,],so the file must be seperated by ',',and the field 1 will be used.
handle_excute_log() {
	_log_l=$1
	_host_l=$2
	echo "###Handling  Log###"
	if [[ -f ${_log_l} ]];then
		echo "log file exist,will be move as .bak..."
		mv ${_log_l} ${_log_l}.bak
	fi
	cat ${_host_l}|awk -F [,] '{print $1}'|while read _host
	do
		if [[ "${host}" != "EOF" ]];then
			_tmp_l=${_host}.log.tmp
			echo "###################"  >> ${_log_l}
			echo "###${_host} excute log begin...." >> ${_log_l}
			cat ${_tmp_l}  >> ${_log_l}
			echo "###${_host}'s excute log ended." >> ${_log_l}
			echo "###################"  >> ${_log_l}
			echo "" >> ${_log_l}
			rm -rf ${_tmp_l}
		fi 
	done
}
#excute_script 192.168.56.5 shell shell /home/shell/test.sh
#handle_excute_log

##$$main$$##
#parsing the args
parse_args $*
export _LOG_F=${_LOG_F:-'excute_cmd.log'}
export _PORT_R=${_PORT_R:-'22'}
#check the required file valid or not
if [[ ! -f ${_HOST_F} ]];then
	echo 'The host_list file not valid,please retry!!!'
	exit 1
fi
if [[ ! -f ${_SCRIPT_F} ]];then
	echo 'The script file not valid,please retry!!!'
	exit 1
fi
#read in password
read -t 20 -s -p "Enter password for ${_HOST},just [enter] if provided by file:" _PASSWD

#excute script file one by one according to _HOST_F
cat ${_HOST_F}|while read _LINE
	do
		if [[ ${_LINE} != "EOF" ]];then
			_HOST=`echo ${_LINE}|awk -F [,] '{print $1}'`
			_USER=`echo ${_LINE}|awk -F [,] '{print $2}'`
			#check if password is provided.
			if [[ "${_PASSWD}"x == ""x  ]];then
				_PASSWD=`echo ${_LINE}|awk -F [,] '{print $3}'`
				if [[ "${_PASSWD}"x == ""x  ]];then
					echo ""
					echo "Password needed,please retry."
					exit 1
				fi
			fi

			echo "Handling ${_HOST}......"
			#upload file to remote server.
			scp_file ${_HOST} ${_USER} ${_PASSWD} ${_SCRIPT_F} ${_DIR_R} ${_PORT_R}
			
			#excute the script if "-e" is selected.
			if [[ ${_EXC_F} ]];then
				echo "${_SCRIPT_F} will be excuted on ${_HOST}."
				_NAME_F=`basename ${_SCRIPT_F}`
				_SCRIPT_R="${_DIR_R}/${_NAME_F}"
				#use excute_script_with_sudo if "-S" is selected
				if [[ ${_USE_SUDO} ]];then
					echo "Script will be excuted with sudo."
					excute_script_with_sudo ${_HOST} ${_USER} ${_PASSWD} ${_SCRIPT_R} ${_PORT_R}
				else
					excute_script ${_HOST} ${_USER} ${_PASSWD} ${_SCRIPT_R} ${_PORT_R}
				fi
			fi
			echo "${_HOST} done."
		fi
	done

#collect the log file 
if [[ ${_EXC_F} ]];then
	handle_excute_log ${_LOG_F} ${_HOST_F}
fi