You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.0 KiB
75 lines
2.0 KiB
# common useful functions |
|
|
|
w3m_safe() { |
|
url="$1" |
|
|
|
output=$(/opt/homebrew/bin/w3m "$url" -M -cols 400 -graph -dump 2>&1) |
|
|
|
if echo "$output" | grep -q "Can't load"; then |
|
return 2 |
|
else |
|
echo "$output" |
|
return 0 |
|
fi |
|
} |
|
|
|
|
|
# Returns: |
|
# 0 = server reachable & command ran |
|
# 1 = network/host-not-up-yet |
|
# 2 = SSH auth failure (likely passphrase/key issue) -> caller should abort |
|
is_server_up() { |
|
host=$1 |
|
|
|
# Capture only stderr (order of redirs matters: 2>&1 >/dev/null) |
|
err=$(/usr/bin/ssh \ |
|
-o BatchMode=yes \ |
|
-o PreferredAuthentications=publickey \ |
|
-o ConnectTimeout=10 \ |
|
-o ServerAliveInterval=20 \ |
|
-o ServerAliveCountMax=3 \ |
|
-o ConnectionAttempts=1 \ |
|
-o StrictHostKeyChecking=no \ |
|
-o UserKnownHostsFile=/dev/null \ |
|
-o LogLevel=ERROR \ |
|
-n -q "$host" 'uname -n || hostname' 2>&1 >/dev/null) |
|
st=$? |
|
|
|
[ $st -eq 0 ] && return 0 |
|
|
|
# Normalize to lowercase for robust matching |
|
err_lc=$(printf '%s' "$err" | tr '[:upper:]' '[:lower:]') |
|
|
|
case $err_lc in |
|
*"permission denied"*|\ |
|
*"no supported authentication methods available"*|\ |
|
*"too many authentication failures"*|\ |
|
*"agent admitted failure"*|\ |
|
*"publickey authentication failed"*) |
|
printf 'FATAL: SSH authentication failed for "%s": %s\n' "$host" "$err" >&2 |
|
return 2 |
|
;; |
|
esac |
|
|
|
# Anything else (timeout, connection refused, kex read/reset, etc.) |
|
return 1 |
|
} |
|
|
|
wait_for_server_to_boot_up() { |
|
server=$1 |
|
while :; do |
|
if is_server_up "$server"; then |
|
printf '"%s" is up and running.\n' "$server" |
|
return 0 |
|
fi |
|
rc=$? |
|
if [ $rc -eq 2 ]; then |
|
printf 'Aborting: SSH authentication failed for "%s" (likely passphrase needed or wrong key).\n' "$server" >&2 |
|
exit 2 |
|
fi |
|
printf 'Server "%s" is not up\n' "$server" |
|
sleep 10 |
|
done |
|
} |
|
|
|
# vim: set ts=4 sw=4 tw=0 noet ft=bash:
|
|
|