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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/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