-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplicate_ssh.sh
More file actions
executable file
·58 lines (48 loc) · 1.81 KB
/
replicate_ssh.sh
File metadata and controls
executable file
·58 lines (48 loc) · 1.81 KB
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
#!/bin/bash
# Configuration
SSH_DIR="$HOME/.ssh"
EXCLUDE_FILE="authorized_keys"
usage() {
echo "Usage: $0 [user@hostname]"
echo "Example: $0 antoine@192.168.122.23"
exit 1
}
if [ -z "$1" ]; then
usage
fi
REMOTE="$1"
echo "Starting SSH configuration replication to ${REMOTE}..."
echo "[1/3] Checking connectivity and preparing remote directory..."
ssh -q -o BatchMode=yes -o ConnectTimeout=5 "$REMOTE" "mkdir -p .ssh && chmod 700 .ssh"
if [ $? -ne 0 ]; then
# Try interactive if batch mode fails (maybe first connect/password needed)
echo "Batch mode failed or password required. Retrying interactively..."
ssh "$REMOTE" "mkdir -p .ssh && chmod 700 .ssh"
if [ $? -ne 0 ]; then
echo "Error: Cannot connect to $REMOTE or create .ssh directory."
exit 1
fi
fi
# We exclude authorized_keys to avoid locking out the current user if their key isn't in the source authorized_keys.
# We also exclude known_hosts potentially? No, known_hosts is useful.
# We DEFINITELY want config, id_*, etc.
echo "[2/3] Syncing files..."
if command -v rsync &> /dev/null; then
rsync -avz --exclude "$EXCLUDE_FILE" "$SSH_DIR/" "$REMOTE:.ssh/"
else
echo "rsync not found, falling back to scp..."
# Create a temporary tarball to preserve permissions and handle multiple files
TAR_FILE="/tmp/ssh_replication_$(date +%s).tar.gz"
tar -czf "$TAR_FILE" -C "$HOME" .ssh --exclude "$EXCLUDE_FILE"
scp "$TAR_FILE" "$REMOTE:/tmp/"
ssh "$REMOTE" "tar -xzf /tmp/$(basename "$TAR_FILE") -C ~/ && rm /tmp/$(basename "$TAR_FILE")"
rm "$TAR_FILE"
fi
if [ $? -eq 0 ]; then
echo "[3/3] Fix permissions on remote..."
ssh "$REMOTE" "chmod 700 .ssh && chmod 600 .ssh/id_*"
echo "SUCCESS! SSH configuration replicated to ${REMOTE}."
else
echo "Error during file transfer."
exit 1
fi