This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathssh-setup.sh
executable file
·58 lines (49 loc) · 1.75 KB
/
ssh-setup.sh
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
# Exit on any errors
set -e
if [[ "x$1" = "x" ]]; then
if [ ! -f hosts ]; then
echo "Usage: ./ssh-setup.sh [hostsfile]"
echo "Note: the hosts file MUST be of a format readable for OpenMPI."
echo "Normal /etc/hosts often contains more information that needed,"
echo "such as hostname aliases and other unrelated hosts."
return
fi
echo "Using existing hosts file."
echo "Make sure that the local host is the first node in this file!"
else
cp "$1" hosts
# Make root node the first node
root=`hostname -s`
sed -i "1s;^;$root\n;" hosts
fi
echo "Updating ~/.ssh/config for agent forwarding..."
if [ -f ~/.ssh/config ]; then
cp ~/.ssh/config ~/.ssh/config.bak
fi
header='### Added by ssh-setup.sh'
echo "$header" >> ~/.ssh/config
sed -i "/^$header/q" ~/.ssh/config # Remove everything after the first header
cut -d' ' -f1 hosts | while read -r node; do
if [[ "$node" == \#* ]]; then
continue
fi
echo "Host $node" >> ~/.ssh/config
echo -e "\tForwardAgent yes" >> ~/.ssh/config
done
echo "Done. Now checking if all nodes are connectible."
echo "If any node listed below should not be a part of the hosts file,"
echo "Then quit this script, remove its line from the file, and rerun"
echo "./ssh-setup.sh without parameters, OR use a different base hosts file"
echo "as first parameter. You might want to mv ~/.ssh/config.bak ~/.ssh/config"
echo "in case anything went wrong, although rerunning should clean itself up."
echo "Any nodes that you trust, but give a warning of being not yet recognized"
echo "must have this question answered with yes."
cut -d' ' -f1 hosts | while read -r node; do
if [[ "$node" == \#* ]]; then
continue
fi
echo "Checking node $node..."
ssh $node "echo OK" < /dev/null # Stop SSH from eating all input
done
echo "Done!"