C.W.K.
Stream
Lesson 03 of 14 · published

Parallel SSH

~15 min · parallel-ssh, shell-loops, gnu-parallel

Level 0Pinger
0 XP0/101 lessons0/12 achievements
0/150 XP to next level150 XP to go0% complete

가장 단순한 병렬 패턴

Bash 백그라운딩 (&) + wait 가 공짜 병렬성. SSH 명령 loop 에 넣고 각각에 & 붙이고 끝에 wait. 머신 8 대 1 초 SSH 연결이면 8 초 latency 가 1 초로.

GNU parallel — 더 깔끔한 버전

더 정교한 필요 (job control, progress, 로그 capture) 엔 GNU 의 parallel 이 canonical. Stdin 이나 파일에서 읽고 각 명령을 설정 가능 동시성까지 병렬 실행, 출력 합리적 처리.

Code

Bash 백그라운딩 패턴·bash
# Run uptime on all workstations in parallel
for host in office server music worker; do
    ssh "$host" 'uptime' &
done
wait

# With output labeling — preserve which output came from which host
for host in office server music worker; do
    (echo "--- $host ---"; ssh "$host" 'uptime') &
done
wait
GNU parallel·bash
# Install
brew install parallel

# Run uptime on multiple hosts (cleaner)
parallel ssh {} 'uptime' ::: office server music worker

# From a host file
parallel ssh {} 'uptime' :::: ~/.fleet/all.txt

# With explicit concurrency limit
parallel -j 4 ssh {} 'brew update && brew upgrade' :::: ~/.fleet/all.txt
~/.zshrc 위한 fleet() helper·bash
fleet() {
    local hosts_file="${2:-$HOME/.fleet/all.txt}"
    while IFS= read -r host; do
        echo "=== $host ==="
        ssh -o ConnectTimeout=5 -o BatchMode=yes "$host" "$1" 2>/dev/null \
            || echo "  UNREACHABLE"
        echo
    done < "$hosts_file"
}

# Usage:
fleet 'uptime'
fleet 'df -h /' ~/.fleet/workstations.txt
fleet 'brew update && brew upgrade' ~/.fleet/all.txt

External links

Exercise

fleet() 함수를 ~/.zshrc (또는 등가) 에 추가. 셸 reload. ~/.fleet/all.txt 에 대해 fleet 'uptime' 돌려. 그다음 fleet 'df -h /' ~/.fleet/workstations.txt. 두 명령, 전체 fleet 가시성. 트랙 나머지가 빌드하는 근육 기억.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.