-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_n_run
executable file
·69 lines (54 loc) · 1.48 KB
/
copy_n_run
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
#!/bin/bash
usage() {
cat <<EOS >&2
Usage:
`basename $0` [-p npasswords] [-u username] hostsfile srcpath dstpath command
Options:
-p npasswords number of passwords to try (5 top)
-u username username to pass to ssh
Arguments:
hostsfile file name holding a list of servers to connect to.
Use - for standard input
srcpath full path to the source script
dstpath destination directory path on the servers
command command to execute
Examples:
./copy_n_run -p 2 -- list /home/me/myscript /tmp /tmp/myscript --an_option
EOS
exit 1
}
while getopts :p:u: opt; do
case $opt in
p) npasswords=$OPTARG
[ $npasswords -lt 1 -o $npasswords -gt 5 ] && usage;;
u) useropt="$OPTARG@";;
?) usage;;
esac
done
shift `expr $OPTIND - 1`
[ $# -lt 4 ] && usage
[ -z "$npasswords" ] && npasswords=1
input=$1; shift
srcpath=$1; shift
dstpath=$1; shift
cmd=$@
[ -z "$input" ] && input=-
. ./feedpass.inc
n=1
while [ $n -le $npasswords ]; do
read -s -p "Password[$n]: " password$n < /dev/tty; echo > /dev/tty
n=`expr $n + 1`
done
for hostname in `grep -v '^#' $input | grep -v '^[[:space:]]*$'`; do
copy="scp -r $srcpath $useropt$hostname:$dstpath"
run="ssh $useropt$hostname {$@}"
n=1
while [ $n -le $npasswords ]; do
password=`eval echo \\$password$n`
feedpass "$copy" "$password"
[ $? -eq 0 ] && break
n=`expr $n + 1`
done
[ $n -gt $npasswords ] && continue
feedpass "$run" "$password"
done