mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-01-31 18:47:11 +00:00
60 lines
833 B
Bash
Executable File
60 lines
833 B
Bash
Executable File
#!/bin/sh
|
|
#
|
|
# Wait until a given host is online (determined by ping) then execute the
|
|
# given command
|
|
#
|
|
# Usage:
|
|
# ./when-up HOST COMMAND...
|
|
#
|
|
# Example
|
|
# ./when-up 1.2.3.4 ssh 1.2.3.4
|
|
#
|
|
# Special case:
|
|
# when using when-up to ssh to a host, this host does not need to be given twice
|
|
# ./when-up ssh 1.2.3.4
|
|
#
|
|
|
|
# Ensure we received the correct number of arguments.
|
|
if [ "$#" -lt 2 ]; then
|
|
echo "Usage: $0 HOST COMMAND..."
|
|
exit 1
|
|
fi
|
|
|
|
get_host()
|
|
{
|
|
if [ "$1" = "ssh" ]; then
|
|
echo "$2"
|
|
else
|
|
echo "$1"
|
|
fi
|
|
}
|
|
|
|
wait_for_host()
|
|
{
|
|
local host=$1
|
|
|
|
echo "Waiting for $host to come online..."
|
|
|
|
while ! ping -c 1 -W 1 "$host" > /dev/null 2>&1; do
|
|
sleep 1
|
|
done
|
|
}
|
|
|
|
main()
|
|
{
|
|
local host
|
|
|
|
host=$(get_host "$@")
|
|
wait_for_host "$host"
|
|
|
|
if [ "$1" = "ssh" ]; then
|
|
shift 1
|
|
else
|
|
shift
|
|
fi
|
|
|
|
"$@"
|
|
}
|
|
|
|
main "$@"
|