-
Notifications
You must be signed in to change notification settings - Fork 0
/
sscp
executable file
·114 lines (101 loc) · 2.48 KB
/
sscp
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
#!/bin/bash
# Global Parameters
connect_timeout=5
current_user=$(whoami)
# Input Parameters
origin_file=$1
target_file=$2
export LC_ALL=C
print_host() {
echo -e "\033[0;3;35m -> \033[0;3;35m$host \033[0m"
}
auto_passwd_trans() {
set timeout -1
expect -c "
spawn $cmd
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password:*\" {send \"$pass\r\"; exp_continue}
}
expect 100%
expect eof
interact
"
}
auto_passwd_cmd() {
set timeout -1
expect -c "
spawn $cmd
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password:*\" {send \"$pass\r\"; exp_continue}
\"*# \" {send_user \"Job Finished.\r\"; expect eof}
}
"
}
trans_file() {
method="trans_file"
print_host $host ${method}
cmd="scp"
if [ ! -z $port ]; then
cmd="${cmd} -P ${port}"
fi
cmd="${cmd} -o ConnectTimeout=${connect_timeout} $(realpath $origin_file) ${user}@${host}:${target_file}"
auto_passwd_trans $cmd $pass
}
trans_folder() {
method="trans_folder"
print_host $host ${method}
cmd="scp"
if [ ! -z $port ]; then
cmd="${cmd} -P ${port}"
fi
cmd="${cmd} -r -o ConnectTimeout=${connect_timeout} $(realpath $origin_file) ${user}@${host}:${target_file}"
auto_passwd_trans $cmd $pass
}
execute_cmd() {
method="execute_cmd"
print_host $host ${method}
cmd="ssh"
if [ ! -z $port ]; then
cmd="${cmd} -p ${port}"
fi
cmd="${cmd} -o ConnectTimeout=${connect_timeout} ${user}@${host} \"${origin_file}\""
auto_passwd_cmd $cmd $pass
}
cluster_cmd() {
for host_union in $(<hosts.conf); do
# Check if it is a comments line.
[ ${host_union:0:1} = "#" ] && continue
# Split hosts.conf
IFS=':' read -a host_array <<< "${host_union}"
user=${host_array[0]}
host=${host_array[1]}
port=${host_array[2]}
pass=${host_array[3]}
$1 $user $host $port $pass
done
}
# Check if 'hosts.conf' config file exist
if [ ! -f hosts.conf ]; then
echo "sscp: sscp's config file 'hosts.conf' not exist in current path!"
exit
fi
# Execute 'sscp' command by the number of arguments
if [ "$#" = 1 ]; then
cluster_cmd execute_cmd
elif [ "$#" = 2 ]; then
if [ -d ${origin_file} ]; then
#trans_folder
cluster_cmd trans_folder
elif [ -f ${origin_file} ]; then
#trans_file
cluster_cmd trans_file
else
echo "sscp: error '$origin_file', file or folder is not exist."
exit
fi
else
echo "sscp: try 'sscp --help' or 'sscp --manual' for more information"
exit
fi