-
Notifications
You must be signed in to change notification settings - Fork 33
/
cs
executable file
·198 lines (165 loc) · 4.78 KB
/
cs
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
#!/usr/bin/env bash
# The scripts is for connect, copyto and download for multiple servers.
# The known_iplist should be provided with username@ip@port@password@index@servername.
# The known_iplist should be put in ~/.ssh and status code change to 600.
# IP LIST
ipfile=${HOME}/.ssh/known_iplist
[[ ! -s ${ipfile} ]] && {
echo "Error: ${ipfile} does not exist."
exit 1
}
# check the mode of known_iplist
[[ $(stat -c "%a" ${ipfile}) -ne 600 ]] && {
echo "Warning: The mode of file ${ipfile} is not 700. chmod 600 for ${ipfile}."
chmod 600 ${ipfile}
}
# load ipmaps
declare -a ipmaps
declare -a servernames
readarray known_ips < ${ipfile}
# number of input parameters
param=$#
# set up functions
# in [login|copyto|download]
func=$1
funcs[1]='login'
funcs[2]='copyto'
funcs[3]='download'
funcs[4]='lg'
funcs[5]='cp'
funcs[6]='dl'
# interface to server
server=$2
# source file
src=$3
# destination directory
dest=${4:-${PWD}}
function fn_errorinfo {
echo "Usage:"
echo " cs [login|copyto|download] [server]"
echo "Details:"
echo " login|lg -> login, log into server."
echo " copyto|cp -> copyto, copyto files to server."
echo " download|dl -> downlaod, download from server."
echo "For example1:"
echo " cs login 1."
echo " example2:"
echo " cs copyto 1 source destination."
echo " example3:"
echo " cs download 1 source destination."
echo "Notice: The source and destination should be absolute path. Source is a file and destination is directory."
}
function fn_inarray {
# $1 is element
# $2 is ${array[@]}
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
function fn_load_iplist {
for line in ${known_ips[@]};
do
line=${line//\~/ }
line=(${line})
# save ips
ipmaps[${line[1]}]=${line[0]}
# save server names
servernames[${line[1]}]=${line[2]}
done
}
function fn_parse_ip {
local ip_str="$1"
line=${ip_str//\`/ }
echo $line
}
function fn_usage {
# The parameters should be in 2 3 4
[[ ! (${param} -eq 2 || ${param} -eq 4) ]] && {
echo "Error: Parameters should be in 2, 4."
echo "****************************************************"
fn_errorinfo
exit 1
}
# func should be in the funcs list
! fn_inarray ${func} ${funcs[@]} && {
echo "Error: Function '${func}' not in connect server."
echo "****************************************************"
fn_errorinfo
exit 1
}
# server should be valid.
! fn_inarray ${server} ${!ipmaps[@]} && {
echo "Error: Server '${server}' should be integer and in the '${!ipmaps[@]}'."
echo "****************************************************"
fn_errorinfo
exit 1
}
}
function fn_login {
# func == login, param should be equal to 2
# copyto and download should be four parameters.
[[ ${param} -ne 2 ]] && {
echo "Error: login should be two parameters."
echo "****************************************************"
fn_errorinfo
exit 1
}
cmd="sshpass -p '${loginfo[3]}' ssh -p ${loginfo[2]} ${loginfo[0]}@${loginfo[1]}"
echo "Notice: log into '${servernames[${server}]}'."
echo ${cmd}
echo "****************************************************"
eval ${cmd}
exit 0
}
function fn_copyto {
# for copyto, src file should exist.
[[ ! -e ${src} ]] && {
echo "Error: file '${src}' not exits."
echo "****************************************************"
fn_errorinfo
exit 1
}
cmd="sshpass -p '${loginfo[3]}' rsync -rvz -e 'ssh -p ${loginfo[2]}' --progress '${src}' ${loginfo[0]}@${loginfo[1]}:${dest} "
echo "Notice: copy file '${src}' to '${dest}' on server '${servernames[${server}]}'."
echo ${cmd}
echo "****************************************************"
eval ${cmd}
exit 0
}
function fn_download {
# for download, dest directory should exist.
[[ ! -d ${dest} ]] && {
echo "Error: directory '${dest}' not exists."
echo "****************************************************"
fn_errorinfo
exit 1
}
cmd="sshpass -p '${loginfo[3]}' rsync -rvz -e 'ssh -p ${loginfo[2]}' --progress ${loginfo[0]}@${loginfo[1]}:${src} ${dest}"
echo "Notice: download file '${src}' to '${dest}' from server '${servernames[${server}]}'."
echo ${cmd}
echo "****************************************************"
eval ${cmd}
exit 0
}
function fn_run {
# Run
[[ ${func} = 'login' ]] && fn_login || fn_usage
[[ ${func} = 'copyto' ]] && fn_copyto || fn_usage
[[ ${func} = 'download' ]] && fn_download || fn_usage
# Alias
[[ ${func} = 'lg' ]] && fn_login || fn_usage
[[ ${func} = 'cp' ]] && fn_copyto || fn_usage
[[ ${func} = 'dl' ]] && fn_download || fn_usage
}
function main {
# load ip list
fn_load_iplist
# Usage
fn_usage
# parse text ip
loginfo=($(fn_parse_ip ${ipmaps[$server]}))
# main run function
fn_run
}
main