Skip to content

Instantly share code, notes, and snippets.

@gebn
Last active March 30, 2016 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gebn/1a93ea06f6f2ed2c4438 to your computer and use it in GitHub Desktop.
Save gebn/1a93ea06f6f2ed2c4438 to your computer and use it in GitHub Desktop.
Finds and connects to a UG04 lab machine.
#!/bin/bash
# will hold available machine hostnames
alive=()
# iterate through a subset of machines in UG04
for suffix in {10..100}; do
# ping each one
ping -c 1 -t 1 "cca-ug04-130$suffix" > /dev/null 2>&1
# if the ping command succeeded, the machine is available
if [ $? -eq 0 ]; then
# add it to the list
alive+=("cca-ug04-130$suffix")
fi
done
# count the number of available machines
count=${#alive[@]}
echo "Found $count machines."
# pick a machine from the list at random
theChosenOne=${alive[$((RANDOM % $count))]}
# connect to it, accepting any unseen host key
echo "Connecting to $theChosenOne..."
ssh -o StrictHostKeyChecking=no "$theChosenOne"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment