#!/bin/sh # # Description: Set or get SSH port # Usage: port [|auto] # # shellcheck source=./includes LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes" jail_name="${1:?}" port="${2:-}" if [ ! -n "${jail_name}" ]; then show_help && exit 1 fi jail_path=$(jail_path "${jail_name}") test -d "${jail_path}" || error "${jail_name}: jail not found" 2 jail_sshd_config="${jail_path}/${SSHD_CONFIG}" if [ -z "${port}" ]; then grep -E "Port [0-9]+" "${jail_sshd_config}"|grep -oE "[0-9]+" else if [ "${port}" = "auto" ]; then port=$(grep -h Port "${JAILDIR}"/*/"${SSHD_CONFIG}" 2>/dev/null | grep -Eo "[0-9]+" | sort -n | tail -1) port=$((port+1)) [ "${port}" -le 1 ] && port=2222 fi sed --follow-symlinks --in-place "s/^Port .*/Port ${port}/" "${jail_sshd_config}" notice "Update SSH port \`${port}' for jail \`${jail_name}' : OK" "${LIBDIR}/bkctld-reload" "${jail_name}" "${LIBDIR}/bkctld-firewall" "${jail_name}" fi