Files
server/opt/psa/admin/sbin/modules/git/git_ssh
2026-01-07 20:52:11 +01:00

73 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# Copyright 1999-2016. Parallels IP Holdings GmbH. All Rights Reserved.
usage()
{
echo "Usage: $0 [-p <port>] <hostname> [<args>...]" >&2
exit 2
}
update_known_hosts()
{
local port= server= offset=0
if [ "$1" = "-o" ]; then
offset=2
fi
option_arg_num=$((offset+1))
if [ "${!option_arg_num}" = "-p" ]; then
port_offset=$((offset+2))
server_offset=$((offset+3))
port="${!port_offset}"
server="${!server_offset}"
else
server_offset=$((offset+1))
server="${!server_offset}"
fi
local hostname="`echo "$server" | cut -d@ -f2`"
local server_addr="$hostname${port:+:$port}"
KNOWN_HOSTS="`readlink -m ~/.ssh/git_known_hosts${port:+_with_port_$port}`"
if [ -f "$KNOWN_HOSTS" ] && [ -n "`ssh-keygen -F "$hostname" -f "$KNOWN_HOSTS"`" ]; then
echo "Public key for the server at '$server_addr' is already known in '$KNOWN_HOSTS'." >&2
return 0
fi
echo "Server at '$server_addr' is seen for the first time." >&2
echo "Adding its public key to the list of known hosts in '$KNOWN_HOSTS'." >&2
local key="`ssh-keyscan ${port:+-p $port} -H "$hostname"`"
[ -n "$key" ] || {
echo "Failed to gather public SSH host key for the '$server_addr'." >&2
return 1
}
mkdir -p -m0700 "`dirname "$KNOWN_HOSTS"`"
echo "$key" >> "$KNOWN_HOSTS" || {
echo "Failed to add public SSH host key for the '$server_addr' into '$KNOWN_HOSTS'." >&2
return 1
}
return 0
}
[ $# -ge 1 ] || usage
KNOWN_HOSTS=
update_known_hosts "$@"
[ -f "$KNOWN_HOSTS" ] || {
echo "Known hosts file '$KNOWN_HOSTS' doesn't exist" >&2
exit 1
}
if [ -z "$PLESK_SSH_KEY_PATH" ]
then
ssh -o UserKnownHostsFile="$KNOWN_HOSTS" -o StrictHostKeyChecking=yes -o HashKnownHosts=yes -o BatchMode=yes "$@"
else
ssh -i "$PLESK_SSH_KEY_PATH" -o UserKnownHostsFile="$KNOWN_HOSTS" -o StrictHostKeyChecking=yes -o HashKnownHosts=yes -o BatchMode=yes "$@"
fi