-
Notifications
You must be signed in to change notification settings - Fork 0
/
se
executable file
·832 lines (693 loc) · 22.1 KB
/
se
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#!/bin/bash
# vim:et:ft=sh:
# @author ZHOU Ling(周龄) <zhou.0@foxmail.com>
# @brief an automation tool based on ssh, sshpass, and GNU parallel.
# @copyright ZHOU Ling(周龄) <zhou.0@foxmail.com>
shopt -s extglob # for trim
declare -A g_optval_dict
declare -a g_arg_list
declare -a g_host_list
declare -a g_user_list
declare -a g_pass_list
declare -a g_port_list
declare -a g_cmnt_list
init() {
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
local tools_path=$(dirname $(readlink -f "$0"))
PATH=$tools_path:$PATH
PARALLEL_BIN='parallel --will-cite'
SSHPASS_BIN='sshpass'
SSH_OPTS='-o stricthostkeychecking=no -o userknownhostsfile=/dev/null -q'
REMOTE_EXE_FILE='/tmp/.se_exe_file'
export COLUMN_SIZE=$(stty size 2>/dev/null | awk '{print $2}')
[[ $COLUMN_SIZE ]] || COLUMN_SIZE=100
export g_implict_copy=0
export g_debug=0
export g_quiet=0
g_action=''
g_to_local=0
g_keep_order=''
g_jobs=$(getconf _NPROCESSORS_CONF 2>/dev/null)
[[ $g_jobs ]] || g_jobs=8
g_timeout=60 # seconds
g_default_user='root'
g_default_pass='x'
g_default_port='22'
g_field_sep=';'
g_comment_sep='//'
g_request_sep=':@_@:'
g_http_proxy=''
g_empty_host_line_pattern=''
g_host_line_pattern=''
export g_request_line_pattern=''
g_arg_num=0
g_host_num=0
g_selected_host_index=-1
g_request=''
g_ssh_cmd='' # command for ssh
g_ssh_exe_file=''
g_ssh_bin='ssh'
g_scp_bin='scp'
g_rsync_bin='rsync'
g_copy_bin=''
g_src=''
g_dst=''
parse_args "$@"
}
timestamp() {
date +%s
}
trim() {
trimmed_str="${1##*([[:space:]])}"
trimmed_str="${trimed_str%%*([[:space:]])}"
((g_debug)) && echo "{{$1}} trimmed to {{$trimmed_str}}"
}
echo_exit() {
echo "${@:2}"
exit $1
} && export -f echo_exit
print_global_help() {
cat <<\EOF
usage: se <action> [options] [command] [args]
use 'se help <action>' to print help for a specific action
actions:
help (h)
ssh
scp
rsync (rs)
example (ex)
common-options:
-H, --Host hosts-file
line-format: host;user;password;port, do not use it with '-h', no default value.
-h, --host host;user;password;port
do not use it with '-H', no default value.
-u, --user default-user
optional, defaults to 'root'.
-p, --password default-password
optional, defaults to 'x'.
-P, --port default-port
optional, defaults to '22'.
-d, --debug
optional, prints debug info.
-k, --keep-order
optional, keeps sequence of output same as the order of input,
normally the output of a job will be printed as soon as the job completes.
-q, --quiet
optional, suppresses extra information for se, such as se prompt and debug info.
-s, --select
optional, select one host which will take effect, the others will be ignored.
-j, --jobs N
optional, runs up to N jobs in parallel, 0 means as many as possible,
defaults to one job per CPU.
-t, --timeout duration
optional, time out in seconds, if the command runs for longer than duration
it will get killed, defaults to 60s.
--field-sep single-line-field-separator
optional, defaults to ';'.
--comment-sep single-line-comment-separator
optional, defaults to '//'.
--proxy http_proxy_ip:http_proxy_port
optional, no default value.
attentions:
1. '-H' and '-h' can not be used together.
2. user specified by '-u' can be overridden by USER in 'host;USER;password;port'.
3. password specified by '-p' can be overridden by PASSWORD in 'host;user;PASSWORD;port'.
4. port specified by '-P' can be overridden by PORT in 'host;user;password;PORT'.
5. field-separator is needed if the member in between is not specified,
for example: host;;password
in this case, default user will be used, and default user can be overridden by -u.
6. user defined single-line-field-separator must be specified if password contains ';'.
for example: '1.2.3.4_@_root_@_my;/passwd_@_22 // this is comment'
# se ... --field-sep '_@_' ...
7. user defined single-line-comment-separator must be specified if password contains '//'.
for example: '1.2.3.4;root;my//passwd;22 !@# this is comment'
# se ... --comment-sep '!@#' ...
se is an automation tool based on ssh, sshpass, and GNU parallel, it can be used to:
1. scp|rsync local file|dir(s) to remote, or in reverse.
2. ssh into host(s) (and execute command|file).
report bugs to <https://github.com/ling-zhou/sshaid>.
EOF
exit
}
print_help_help() {
cat <<\EOF
usage: se help [action]
EOF
exit
}
print_ssh_help() {
cat <<\EOF
usage: se ssh [--ssh-bin your-ssh-binary-path] [common-options] [-e|--exec exe_file | command]
use 'se help' to print help about 'common options'
notice that '-e|--exec exe_file' and 'command' can not be used together
if neither '-e|--exec exe_file' nor 'command' is used, then
log into host(s) specified by '-H|-h'
else
log into host(s) specified by '-H|-h', execute command and exit
EOF
exit
}
print_scp_help() {
cat <<\EOF
usage: se scp [--scp-bin your-scp-binary-path] [-L|--to-local] [common-options]
source(s) destination
use 'se help' to print help about 'common options'
without option '-L|--to-local':
scp local source(s) to remote destination
with option '-L|--to-local':
scp remote source(s) to local destination
EOF
exit
}
print_rsync_help() {
cat <<\EOF
usage: se rsync [--rsync-bin your-rsync-binary-path] [-L|--to-local] [common-options]
source(s) destination
use 'se help' to print help about 'common options'
without option '-L|--to-local':
rsync local source(s) to remote destination
with option '-L|--to-local':
rsync remote source(s) to local destination
EOF
exit
}
print_examples() {
cat <<\EOF
# log into host.
$ se -h 'host;user;passwd;port' ssh
# log into host without host prompt.
$ se -h 'host;user;passwd;port' ssh -q
# log into host and execute a command.
$ se -h 'host;user;passwd;port' ssh 'ps -ef | grep python'
# log into host and execute a command with debug info printed.
$ se -h 'host;user;passwd;port' ssh 'ps -ef | grep python' -d
# log into host and execute a local file,
# the file will be automatically copied to host in advance.
$ se -h 'host;user;passwd;port' ssh -e executable_file
# log into hosts one by one, use '<C-d>' to exit current machine and enter next one,
# use '<C-d><C-c>' to exit the entire process.
$ se -H hosts.txt ssh
# log into the selected host.
$ se -H hosts.txt ssh -s
# log into hosts and execute a command in parallel.
$ se -H hosts.txt ssh 'ps -ef | grep python'
# log into the selected host and execute a command.
$ se -H hosts.txt ssh 'ps -ef | grep python' -s
# log into hosts and execute a local file in parallel,
# the file will be automatically copied to hosts in advance.
$ se -H hosts.txt ssh -e executable_file -j 5
# scp local file(s) and dir(s) to remote_dir on hosts in parallel.
$ se -H hosts.txt scp local_file1 local_dir2 local_file3 remote_dir -j 8
# scp remote file(s) and dir(s) on hosts to local_dir in parallel.
$ se -H hosts.txt scp -L remote_file1 remote_dir2 remote_file3 local_dir
# rsync local file(s) and dir(s) to remote_dir on hosts in parallel.
$ se -H hosts.txt rsync local_file1 local_dir2 local_file3 remote_dir -j 8
# rsync remote file(s) and dir(s) on hosts to local_dir in parallel.
$ se -H hosts.txt rsync -L remote_file1 remote_dir2 remote_file3 local_dir
# when separators do not meet the requirements, you can specify your own.
$ cat hosts.txt
www.abc.com@@user1@@pass1@@22 ^_^ this is host1
1.2.3.4@@user2@@pass2@@222 ^_^ this is host2
$ se -H hosts.txt ssh 'ls -l' --field-sep '@@' --comment-sep '^_^'
EOF
exit
}
build_line_patterns() {
local fsep="$g_field_sep"
local csep="$g_comment_sep"
local rsep="$g_request_sep"
[[ $fsep ]] || fsep=';'
[[ $csep ]] || csep='//'
[[ $rsep ]] || rsep=':@_@:'
g_empty_host_line_pattern="^([[:space:]]*)($csep([[:space:]]*)(.*))?\$"
g_host_line_pattern="^([]\[\.:_0-9a-zA-Z]+)$fsep([_0-9a-zA-Z]*)$fsep(.*)$fsep([0-9]*)"
g_host_line_pattern+="(([[:space:]]*)$csep([[:space:]]*)(.*))?\$"
g_request_line_pattern="^([^ ]+) ([^ ]+) ([^ ]+) (.+)$rsep(.+)\$"
if ((g_debug)); then
echo "field-sep: {{$fsep}}"
echo "comment-sep: {{$csep}}"
echo "request-sep: {{$rsep}}"
echo
echo "empty-host-line-pattern: {{$g_empty_host_line_pattern}}"
echo "host-line-pattern: {{$g_host_line_pattern}}"
echo "request-line-pattern: {{$g_request_line_pattern}}"
echo
fi
}
parse_host_line() {
if [[ "$1" =~ $g_empty_host_line_pattern ]]; then
((g_debug)) && echo "empty-host-line: {{$1}} is ignored"
return
fi
if [[ "$1" =~ $g_host_line_pattern ]]; then
local host=${BASH_REMATCH[1]}
local user=${BASH_REMATCH[2]}
local pass=${BASH_REMATCH[3]}
local port=${BASH_REMATCH[4]}
local cmnt=${BASH_REMATCH[8]}
else
echo_exit 22 "host line: {{$1}} is invalid"
fi
if ((g_debug)); then
echo "line: {{$1}}"
echo " host: {{$host}}"
echo " user: {{$user}}"
echo " pass: {{$pass}}"
echo " port: {{$port}}"
echo " cmnt: {{$cmnt}}"
fi
if [[ -z $user ]]; then
user="$g_default_user"
((g_debug)) && echo "default user: {{$user}} will be used"
fi
if [[ -z $pass ]]; then
pass="$g_default_pass"
((g_debug)) && echo "default password: {{$pass}} will be used"
fi
if [[ -z $port ]]; then
port="$g_default_port"
((g_debug)) && echo "default port: {{$port}} will be used"
fi
g_host_list[$g_host_num]=$host
g_user_list[$g_host_num]=$user
g_pass_list[$g_host_num]=$pass
g_port_list[$g_host_num]=$port
g_cmnt_list[$g_host_num]=$cmnt
((++g_host_num))
}
parse_host_file() {
((g_debug)) && echo -e "host-file: {{$1}}"
while read line; do
parse_host_line "$line"
done <"$1"
}
parse_args() {
(($# < 1)) && print_global_help
local long_opts='debug,keep-order,quiet,to-local,Host:,host:,user:,password:'
long_opts+=',port:,jobs:,timeout:,exec:,field-sep:,comment-sep:'
long_opts+=',request-sep:,proxy:,ssh-bin:,scp-bin:,rsync-bin:'
local short_opts='dkqsLH:h:u:p:P:j:t:e:'
local options=$(getopt -l $long_opts -o $short_opts -- "$@" 2>/dev/null)
eval set -- "$options"
while (($# > 0)); do
local optarg="$1"
case "$optarg" in
-d | --debug)
g_optval_dict['-d']=1
;;
-k | --keep-order)
g_optval_dict['-k']=1
;;
-q | --quiet)
g_optval_dict['-q']=1
;;
-s | --select)
g_optval_dict['-s']=1
;;
-L | --to-local)
g_optval_dict['-L']=1
;;
-H | --Host)
shift
g_optval_dict['-H']="$1"
;;
-h | --host)
shift
g_optval_dict['-h']="$1"
;;
-u | --user)
shift
g_optval_dict['-u']="$1"
;;
-p | --password)
shift
g_optval_dict['-p']="$1"
;;
-P | --port)
shift
g_optval_dict['-P']="$1"
;;
-j | --jobs)
shift
g_optval_dict['-j']="$1"
;;
-t | --timeout)
shift
g_optval_dict['-t']="$1"
;;
-e | --exec)
shift
g_optval_dict['-e']="$1"
;;
--field-sep | --comment-sep | --request-sep | \
--proxy | --ssh-bin | --scp-bin | --rsync-bin)
shift
g_optval_dict[$optarg]="$1"
;;
--)
;;
-*)
echo_exit 22 "unknown option: {{$optarg}}"
;;
*)
g_arg_list[$g_arg_num]="$optarg"
((++g_arg_num))
;;
esac
shift
done
set_and_check_action
check_option '-H' && check_option '-h' && echo_exit 1 "'-H' and '-h' can not be used together!"
check_option '-d' && g_debug=1
check_option '-k' && g_keep_order='-k'
check_option '-q' && g_quiet=1 && g_debug=0
check_option '-j' && g_jobs="$optval"
check_option '-u' && g_default_user="$optval"
check_option '-p' && g_default_pass="$optval"
check_option '-P' && g_default_port="$optval"
check_option '-t' && g_timeout="$optval"
check_option '--field-sep' && g_field_sep="$optval"
check_option '--comment-sep' && g_comment_sep="$optval"
check_option '--request-sep' && g_request_sep="$optval"
check_option '--proxy' && g_http_proxy="$optval"
check_option '--ssh-bin' && g_ssh_bin="$optval"
check_option '--scp-bin' && g_scp_bin="$optval"
check_option '--rsync-bin' && g_rsync_bin="$optval"
build_line_patterns
if check_option '-H'; then
parse_host_file "$optval"
elif check_option '-h'; then
parse_host_line "$optval"
else
echo_exit 22 "'-H' or '-h' is needed!"
fi
debug_args
check_option '-s' && select_host
}
check_option() {
optval="${g_optval_dict["$1"]}"
[[ "$optval" ]]
}
set_and_check_action() {
((g_arg_num == 0)) && echo_exit 2 "action is needed!"
g_action=${g_arg_list[0]}
local found=0
for action in h help ssh scp rs rsync ex example; do
if [[ "$action" == $g_action ]]; then
found=1
break
fi
done
((!found)) && echo_exit 22 "action: {{$g_action}} is invalid"
[[ $g_action == h || $g_action == help ]] && dispatch_help
[[ $g_action == ex || $g_action == example ]] && print_examples
}
debug_args() {
((g_debug)) || return
local i
local k
echo
echo "action: {{$g_action}}"
echo
echo "quiet: {{$g_quiet}}"
echo "keep-order: {{$g_keep_order}}"
echo
echo "jobs: {{$g_jobs}}"
echo "timeout: {{$g_timeout}}"
echo
echo "default-user: {{$g_default_user}}"
echo "default-pass: {{$g_default_pass}}"
echo "default-port: {{$g_default_port}}"
echo
echo "http-proxy: {{$g_http_proxy}}"
echo
echo "ssh-bin: {{$g_ssh_bin}}"
echo "scp-bin: {{$g_scp_bin}}"
echo "rsync-bin: {{$g_rsync_bin}}"
echo
echo "optval-dict:"
for k in "${!g_optval_dict[@]}"; do
echo " {{$k}} => {{${g_optval_dict[$k]}}}"
done
echo
echo "arg-num: {{$g_arg_num}}"
echo "arg-list:"
for ((i = 0; i < g_arg_num; i++)); do
echo " arg[$i]: {{${g_arg_list[i]}}}"
done
echo
echo "host-num: {{$g_host_num}}"
echo "host-list:"
for ((i = 0; i < g_host_num; i++)); do
echo " host[$i]: {{${g_host_list[i]}}}"
echo " user[$i]: {{${g_user_list[i]}}}"
echo " pass[$i]: {{${g_pass_list[i]}}}"
echo " port[$i]: {{${g_port_list[i]}}}"
echo " cmnt[$i]: {{${g_cmnt_list[i]}}}"
echo
done
}
select_host() {
local host_index
local i
for ((i = 0; i < g_host_num; i++)); do
printf "#%d: %s@%s // %s\n" $i ${g_user_list[$i]} ${g_host_list[$i]} "${g_cmnt_list[$i]}"
done
printf "\nenter host index: "
read host_index
echo
if [[ ! $host_index =~ [0-9]+ ]]; then
echo_exit 22 "!!!!! error: host index is invalid !!!!!"
elif ((host_index < 0 || host_index >= g_host_num)); then
echo_exit 22 "!!!!! error: host index should be in range[0, $g_host_num) !!!!!"
else
g_selected_host_index=$host_index
fi
}
filter_host() {
local host_index=$1
((g_selected_host_index >= 0 && g_selected_host_index != host_index))
}
build_request() {
local action=$1
local host_index=$2
local host=${g_host_list[$host_index]}
local user=${g_user_list[$host_index]}
local port=${g_port_list[$host_index]}
if [[ $g_http_proxy ]]; then
local proxy_opts="-o 'ProxyCommand nc -X CONNECT -x $g_http_proxy %h %p'"
else
local proxy_opts=''
fi
if [[ $g_copy_bin ]]; then
local dst="$g_dst"
if ((g_to_local)); then # from remote to local
if ((g_host_num > 1)); then
dst+="/$host"
mkdir -p "$dst"
fi
if [[ $g_copy_bin == *scp* ]]; then
g_request=$(cat <<EOF
$SSHPASS_BIN -e $g_scp_bin -pr $SSH_OPTS $proxy_opts -P$port $user@$host:$g_src $dst
EOF
)
else # rsync
g_request=$(cat <<EOF
$g_rsync_bin -ae "$SSHPASS_BIN -e $g_ssh_bin $SSH_OPTS $proxy_opts -p$port" $user@$host:$g_src $dst
EOF
)
fi
else # from local to remote
if [[ $g_copy_bin == *scp* ]]; then
g_request=$(cat <<EOF
$SSHPASS_BIN -e $g_scp_bin -pr $SSH_OPTS $proxy_opts -P$port $g_src $user@$host:$dst
EOF
)
else # rsync
g_request=$(cat <<EOF
$g_rsync_bin -ae "$SSHPASS_BIN -e $g_ssh_bin $SSH_OPTS $proxy_opts -p$port" $g_src $user@$host:$dst
EOF
)
fi
fi
else
g_request=$(cat <<EOF
$SSHPASS_BIN -e $g_ssh_bin $SSH_OPTS $proxy_opts -p$port -l$user $host '$g_ssh_cmd'
EOF
)
fi
[[ $action != 'login' ]] && \
echo "$host_index $host $user $g_request$g_request_sep${g_pass_list[$host_index]}"
}
build_requests() {
local i
for ((i = 0; i < g_host_num; i++)); do
filter_host $i && continue
build_request 'exec' $i
done
}
exec_request() {
if [[ "$1" =~ $g_request_line_pattern ]]; then
local host_index="${BASH_REMATCH[1]}"
local host="${BASH_REMATCH[2]}"
local user="${BASH_REMATCH[3]}"
local request="${BASH_REMATCH[4]}"
SSHPASS="${BASH_REMATCH[5]}"
else
echo_exit 22 "arg: {{$1}} is invalid"
fi
((g_debug || !g_quiet && !g_implict_copy)) && echo_host_prompt $host_index $host $user
if ((g_debug)); then
echo "request-line-pattern: {{$g_request_line_pattern}}"
echo "request-line: {{$1}}"
echo "host-index: {{$host_index}}"
echo "host: {{$host}}"
echo "user: {{$user}}"
echo "password: {{$SSHPASS}}"
echo "request: {{$request}}"
echo
fi
export SSHPASS
eval "$request"
} && export -f exec_request
exec_in_parallel() {
$PARALLEL_BIN $g_keep_order --jobs $g_jobs --timeout $g_timeout \
exec_request :::: <(build_requests)
}
se_ssh() {
g_ssh_cmd="${g_arg_list[1]}" # maybe empty
check_option '-e' && g_ssh_exe_file="$optval"
if ((g_debug)); then
echo "ssh-cmd: {{$g_ssh_cmd}}"
echo "ssh-exe-file: {{$g_ssh_exe_file}}"
echo
fi
if (($g_arg_num > 1)) && check_option '-e'; then
echo_exit 1 "'-e' and 'command' can not be used together!"
elif (($g_arg_num > 1)); then
exec_in_parallel
elif check_option '-e'; then
g_implict_copy=1
g_to_local=0
g_src="$g_ssh_exe_file"
g_dst="${REMOTE_EXE_FILE}.$(timestamp)_$$_$RANDOM"
[[ -f $g_ssh_exe_file ]] || echo_exit 2 "!!!!! error: '$g_ssh_exe_file' is not found !!!!!"
[[ "$g_scp_bin" != 'scp' ]] && g_copy_bin="$g_scp_bin"
[[ -z "$g_copy_bin" && "$g_rsync_bin" != 'rsync' ]] && g_copy_bin="$g_rsync_bin"
[[ "$g_copy_bin" ]] || g_copy_bin='scp'
exec_in_parallel
g_implict_copy=0
g_copy_bin=
g_ssh_cmd="chmod +x $g_dst && $g_dst; rm -f $g_dst"
exec_in_parallel
else
se_login
fi
}
se_login() {
local i
for ((i = 0; i < g_host_num; i++)); do
filter_host $i && continue
build_request 'login' $i
((!g_quiet)) && echo_host_prompt $i ${g_host_list[i]} ${g_user_list[i]}
if ((g_debug)); then
echo "host-index: {{$i}}"
echo "host: {{${g_host_list[i]}}}"
echo "user: {{${g_user_list[i]}}}"
echo "password: {{${g_pass_list[i]}}}"
echo "request: {{$g_request}}"
echo
fi
export SSHPASS="${g_pass_list[i]}"
eval "$g_request"
echo
done
}
echo_host_prompt() {
local host_index=$1
local host=$2
local user=$3
local str=$(printf "#%d: %s@%s\n" $host_index $user $host)
local str_len=$[${#str} + 2]
local left_size=$[(COLUMN_SIZE - str_len) / 2]
local right_size=$[COLUMN_SIZE - str_len - left_size]
local i
local prompt=''
for ((i = 0; i < left_size; i++)); do
prompt+='*'
done
prompt+=" $str "
for ((i = 0; i < right_size; i++)); do
prompt+='*'
done
echo "$prompt"
} && export -f echo_host_prompt
se_remote_copy() {
((g_arg_num < 3)) && echo_exit 2 "source(s) and destination are both needed!"
check_option '-L' && g_to_local=1
((g_to_local && g_arg_num != 3)) && echo_exit 1 "can copy only ONE source from remote to local!"
local i
for ((i = 1; i < g_arg_num - 1; i++)); do
[[ $g_src ]] && g_src+=" ${g_arg_list[i]}" || g_src="${g_arg_list[i]}"
done
g_dst="${g_arg_list[$[g_arg_num - 1]]}"
debug_remote_copy
exec_in_parallel
}
debug_remote_copy() {
((g_debug)) || return
echo "to-local: {{$g_to_local}}"
echo "copy-bin: {{$g_copy_bin}}"
echo "source(s): {{$g_src}}"
echo "destination: {{$g_dst}}"
echo
}
se_scp() {
g_copy_bin="$g_scp_bin"
se_remote_copy
}
se_rsync() {
g_copy_bin="$g_rsync_bin"
se_remote_copy
}
dispatch_help() {
local action=${g_arg_list[1]}
case $action in
ssh)
print_ssh_help
;;
scp)
print_scp_help
;;
rs | rsync)
print_rsync_help
;;
h | help)
print_help_help
;;
*)
print_global_help
;;
esac
}
run() {
case $g_action in
ssh)
se_ssh
;;
scp)
se_scp
;;
rs | rsync)
se_rsync
;;
*)
print_global_help
;;
esac
}
main() {
init "$@"
run
}
main "$@"