aboutsummaryrefslogtreecommitdiffstats
path: root/pars.sh
blob: 819153bca68913e7a345ce2ba711092b2403ec69 (plain)
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
#!/bin/bash

parssh () {
    local origopts=$-
    set -m
    while [[ "$1" == -* ]]; do
        case ${1#-} in
            [0-9]*)
                [[ "${1#-}" != *[^0-9]* ]] && {
                    local parssh_concurrency=${1#-}
                    shift
                }
            ;;
            fo|fouter)
                [[ -r "$2" ]] || {
                    echo "'$2': invalid file name or permissions issue"
                    return 99
                }
                local parssh_fouter="$2"
                shift
                shift
            ;;
            fi|finner)
                [[ -r "$2" ]] || {
                    echo "'$2': invalid file name or permissions issue"
                    return 98
                }
                local parssh_finner="$2"
                shift
                shift
            ;;
        esac
    done

    [[ -z "$parssh_fouter" ]] && {
        exec 9<&0
    } || {
        exec 9<"$parssh_fouter"
    }

    while read host; do
        while (( $(jobs -pr | wc -l) >= ${parssh_concurrency:-4} )); do
            sleep 1
        done
        if [[ -z "$parssh_finner" ]]; then
            ssh -no StrictHostKeyChecking=no $host "$@" &
        else
            ssh -To StrictHostKeyChecking=no $host "$@" < "$parssh_finner" &
        fi
    done <&9
    wait
    exec 9>&-
    [[ "${origopts//[^m]/}" == "m" ]] || set +m
}