163 lines
3.6 KiB
Bash
163 lines
3.6 KiB
Bash
# vim: syn=sh ts=4 sts=4 sw=4 autoindent smartindent expandtab ff=unix
|
|
|
|
ssh-init-host() {
|
|
scp -r ${HOME}/.ssh $1:.ssh
|
|
ssh $1 << EOF
|
|
for i in \$(find .ssh); do
|
|
if [ -d "\$i" ]; then
|
|
chmod 0700 "\$i"
|
|
else
|
|
chmod 0600 "\$i"
|
|
fi
|
|
done
|
|
exit
|
|
EOF
|
|
}
|
|
|
|
ssh-wait() {
|
|
local usage="ssh-wait [-h] |[-n <RETRIES> -t <TIMEOUT>] USER@HOST"
|
|
local usage="ssh-wait [-h] |[-n <RETRIES> -t <TIMEOUT>] USER@HOST"
|
|
local n=0
|
|
local timeout=5
|
|
local args=`getopt "hn:t:" $*`
|
|
if [ $? -ne 0 ]; then
|
|
echo "USAGE: $usage"
|
|
return 2
|
|
fi
|
|
set -- $args
|
|
|
|
while :; do
|
|
case "$1" in
|
|
-h)
|
|
cat << EOF
|
|
ssh-wait
|
|
|
|
Wait for an ssh connection to establish.
|
|
|
|
SYNOPSIS
|
|
$usage
|
|
|
|
OPTIONS
|
|
-h Print this help an exit
|
|
-n RETRIES Number of retries. (default: 0)
|
|
Set to "0" for infinite retries.
|
|
-t TIMEOUT Timeout in seconds between retries. (default: 5)
|
|
EOF
|
|
return 0
|
|
;;
|
|
-n)
|
|
local n=$2
|
|
shift; shift
|
|
;;
|
|
-t)
|
|
local timeout=$2
|
|
shift; shift
|
|
;;
|
|
--)
|
|
shift
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
if [ $# -lt 1 ]; then
|
|
echo "No HOST to connect given!" >&2
|
|
return 2
|
|
fi
|
|
if [ $# -gt 1 ]; then
|
|
echo "WARNING too many hosts to connect given! Using only the first one!" >&2
|
|
fi
|
|
|
|
local count=0
|
|
echo "Trying to connect to \"$1\" ..."
|
|
while [ $n -eq 0 -o $count -lt $n ]; do
|
|
local count=$(( $count + 1))
|
|
|
|
echo "exit" | ssh "$1"
|
|
if [ $? -eq 0 ]; then
|
|
echo "CONNECTION ESTABLISHED!"
|
|
return 0
|
|
fi
|
|
|
|
echo "CONNECTING FAILED!"
|
|
if [ $n -eq 0 -o $n -lt $count ]; then
|
|
echo "Retrying to connect ..."
|
|
sleep $timeout
|
|
fi
|
|
done
|
|
return 1
|
|
}
|
|
|
|
|
|
ssh-wait-connect() {
|
|
local usage="ssh-wait-connect [-h] |[-n <RETRIES> -t <TIMEOUT>] USER@HOST"
|
|
local n=0
|
|
local timeout=5
|
|
local args=`getopt "hn:t:" $*`
|
|
if [ $? -ne 0 ]; then
|
|
echo "USAGE: $usage"
|
|
return 2
|
|
fi
|
|
set -- $args
|
|
|
|
while :; do
|
|
case "$1" in
|
|
-h)
|
|
cat << EOF
|
|
ssh-wait
|
|
|
|
Wait for an ssh connection to establish.
|
|
|
|
SYNOPSIS
|
|
$usage
|
|
|
|
OPTIONS
|
|
-h Print this help an exit
|
|
-n RETRIES Number of retries. (default: 0)
|
|
Set to "0" for infinite retries.
|
|
-t TIMEOUT Timeout in seconds between retries. (default: 5)
|
|
EOF
|
|
return 0
|
|
;;
|
|
-n)
|
|
local n=$2
|
|
shift; shift
|
|
;;
|
|
-t)
|
|
local timeout=$2
|
|
shift; shift
|
|
;;
|
|
--)
|
|
shift
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
if [ $# -lt 1 ]; then
|
|
echo "No HOST to connect given!" >&2
|
|
return 2
|
|
fi
|
|
if [ $# -gt 1 ]; then
|
|
echo "WARNING too many hosts to connect given! Using only the first one!" >&2
|
|
fi
|
|
|
|
local count=0
|
|
echo "Trying to connect to \"$1\" ..."
|
|
while [ $n -eq 0 -o $count -lt $n ]; do
|
|
local count=$(( $count + 1))
|
|
|
|
ssh "$1"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
echo "CONNECTING FAILED!"
|
|
if [ $n -eq 0 -o $n -lt $count ]; then
|
|
echo "Retrying to connect ..."
|
|
sleep $timeout
|
|
fi
|
|
else
|
|
return 0
|
|
fi
|
|
done
|
|
return 1
|
|
}
|
|
|