3 # SSH authentication script for cluster edition
4 # This script will create a single key pair, which is then
5 # propagated throughout the entire cluster.
6 # There are two options for setup; temporary setup
7 # persistent setup. If no options are specified, and the script
8 # is only given ip addresses or host names, it will default to
9 # the temporary setup. An ssh directory is then created in
10 # /tmp/mn/ssh on each node, and mounted with the keys over the
11 # user's ssh directory. This setup can easily be torn down by running
12 # clustersetup with the -c option.
13 # If the -p option is used, the setup will be persistent. In this
14 # case, the key pair will be be distributed directly to each node's
15 # ssh directory, but will be called cluster_key. An option to
16 # specify this key for use will be added to the config file in each
17 # user's ssh directory.
29 usage="./clustersetup.sh [ -p|h|c ] [ host1 ] [ host2 ] ...\n
30 Authenticate yourself and other cluster nodes to each other
31 via ssh for mininet cluster edition. By default, we use a
32 temporary ssh setup. An ssh directory is mounted over
33 $USERDIR on each machine in the cluster.
36 -p: create a persistent ssh setup. This will add
37 new ssh keys and known_hosts to each nodes
39 -c: method to clean up a temporary ssh setup.
40 Any hosts taken as arguments will be cleaned
44 echo "***creating key pair"
45 ssh-keygen -t rsa -C "Cluster_Edition_Key" -f $USERDIR/cluster_key -N '' # &> /dev/null
46 cat $USERDIR/cluster_key.pub >> $USERDIR/authorized_keys
47 echo "***configuring ssh"
48 echo "IdentityFile $USERDIR/cluster_key" >> $USERDIR/config
49 echo "IdentityFile $USERDIR/id_rsa" >> $USERDIR/config
51 for host in $hosts; do
52 echo "***copying public key to $host"
53 ssh-copy-id -i $USERDIR/cluster_key.pub $user@$host &> /dev/null
54 echo "***copying key pair to remote host"
55 scp $USERDIR/cluster_key $user@$host:$USERDIR
56 scp $USERDIR/cluster_key.pub $user@$host:$USERDIR
57 echo "***configuring remote host"
58 ssh -o ForwardAgent=yes $user@$host "
59 echo 'IdentityFile $USERDIR/cluster_key' >> $USERDIR/config
60 echo 'IdentityFile $USERDIR/id_rsa' >> $USERDIR/config"
63 for host in $hosts; do
64 echo "***copying known_hosts to $host"
65 scp $USERDIR/known_hosts $user@$host:$USERDIR/cluster_known_hosts
67 cat $USERDIR/cluster_known_hosts >> $USERDIR/known_hosts
68 rm $USERDIR/cluster_known_hosts"
74 echo "***creating temporary ssh directory"
76 echo "***creating key pair"
77 ssh-keygen -t rsa -C "Cluster_Edition_Key" -f $SSHDIR/id_rsa -N '' &> /dev/null
79 echo "***mounting temporary ssh directory"
80 sudo mount --bind $SSHDIR $USERDIR
81 cp $SSHDIR/id_rsa.pub $SSHDIR/authorized_keys
83 for host in $hosts; do
84 echo "***copying public key to $host"
85 ssh-copy-id $user@$host &> /dev/null
86 echo "***mounting remote temporary ssh directory for $host"
87 ssh -o ForwardAgent=yes $user@$host "
89 cp $USERDIR/authorized_keys $SSHDIR/authorized_keys
90 sudo mount --bind $SSHDIR $USERDIR"
91 echo "***copying key pair to $host"
92 scp $SSHDIR/{id_rsa,id_rsa.pub} $user@$host:$SSHDIR
95 for host in $hosts; do
96 echo "***copying known_hosts to $host"
97 scp $SSHDIR/known_hosts $user@$host:$SSHDIR
103 for host in $hosts; do
104 echo "***cleaning up $host"
105 ssh $user@$host "sudo umount $USERDIR
109 echo "**unmounting local directories"
111 echo "***removing temporary ssh directory"
118 if [ $# -eq 0 ]; then
119 echo "ERROR: No Arguments"
123 while getopts 'hpc' OPTION
133 shift $(($OPTIND - 1))
136 if [ "$num_options" -gt 1 ]; then
137 echo "ERROR: Too Many Options"
148 output=$(getent ahostsv4 "$i")
149 if [ -z "$output" ]; then
150 echo '***WARNING: could not find hostname "$i"'
162 echo "***authenticating to:"
163 for host in $hosts; do
170 echo '***Setting up persistent SSH configuration between all nodes'
172 echo $'\n*** Sucessfully set up ssh throughout the cluster!'
175 echo '*** Setting up temporary SSH configuration between all nodes'
177 echo $'\n***Finished temporary setup. When you are done with your cluster'
178 echo $' session, tear down the SSH connections with'
179 echo $' ./clustersetup.sh -c '$hosts''