Files
linux-bash/login-info/install_login-info.sh
2026-04-04 13:24:58 +08:00

162 lines
4.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
#===============================================================================
# login-info 安装脚本
# 作用:
# 1) 安装命令: /usr/local/bin/login-info
# 2) 写入登录启动脚本: /etc/profile.d/smy-login-info.sh
#
# 用法:
# sudo bash login-info/install_login-info.sh
# sudo bash login-info/install_login-info.sh --mode full
# sudo bash login-info/install_login-info.sh --all
#
# 禁用(用户级):
# touch ~/.login-info-disable
# 或: export LOGIN_INFO_DISABLE=1
#
# 禁用(全局):
# sudo mkdir -p /etc/login-info
# sudo touch /etc/login-info/disable
#===============================================================================
log() { printf '[login-info-安装] %s\n' "$*" >&2; }
fail() { log "错误: $*"; exit 1; }
require_root() {
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
fail "请使用 root 权限运行 (sudo bash login-info/install_login-info.sh)"
fi
}
main() {
local default_mode="summary"
local ssh_only="1"
while [ $# -gt 0 ]; do
case "$1" in
--mode)
shift
[ $# -gt 0 ] || fail "--mode 需要参数: summary|full"
default_mode=$1
;;
--mode=*)
default_mode=${1#*=}
;;
--ssh-only)
ssh_only="1"
;;
--all)
ssh_only="0"
;;
-h|--help)
cat <<'EOF'
用法:
sudo bash login-info/install_login-info.sh [--mode summary|full] [--ssh-only|--all]
EOF
return 0
;;
*)
fail "未知参数: $1"
;;
esac
shift
done
case "$default_mode" in
summary|full) ;;
*) fail "--mode 仅支持: summary|full" ;;
esac
require_root
local script_dir script_src bin_path profile_hook
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
script_src="${script_dir}/login-info.sh"
bin_path="/usr/local/bin/login-info"
profile_hook="/etc/profile.d/smy-login-info.sh"
[ -f "$script_src" ] || fail "未找到脚本: $script_src"
mkdir -p "$(dirname "$bin_path")"
mkdir -p "$(dirname "$profile_hook")"
log "安装命令到: $bin_path"
if command -v install >/dev/null 2>&1; then
install -m 0755 "$script_src" "$bin_path"
else
cp "$script_src" "$bin_path"
chmod 0755 "$bin_path"
fi
log "写入登录启动脚本: $profile_hook"
cat >"$profile_hook" <<EOF
# /etc/profile.d/smy-login-info.sh
# 由 login-info 安装脚本生成SSH 登录后自动展示服务器信息
#
# 禁用:
# - export LOGIN_INFO_DISABLE=1
# - touch ~/.login-info-disable
# - touch /etc/login-info/disable
#
# 覆盖默认行为:
# - export LOGIN_INFO_MODE=summary|full
# - export LOGIN_INFO_SSH_ONLY=1|0
# 仅在交互式且有 TTY 时运行(避免 scp/sftp 等非交互场景)
case "\$-" in
*i*) ;;
*) return 0 2>/dev/null || exit 0 ;;
esac
if ! [ -t 1 ]; then
return 0 2>/dev/null || exit 0
fi
# 避免重复输出
if [ -n "\${LOGIN_INFO_SHOWN:-}" ]; then
return 0 2>/dev/null || exit 0
fi
# 禁用开关
if [ -n "\${LOGIN_INFO_DISABLE:-}" ]; then
return 0 2>/dev/null || exit 0
fi
if [ -f "/etc/login-info/disable" ]; then
return 0 2>/dev/null || exit 0
fi
if [ -n "\${HOME:-}" ] && [ -f "\${HOME}/.login-info-disable" ]; then
return 0 2>/dev/null || exit 0
fi
# SSH-only默认开启可通过环境变量覆盖
__smy_login_info_ssh_only="\${LOGIN_INFO_SSH_ONLY:-${ssh_only}}"
if [ "\$__smy_login_info_ssh_only" = "1" ]; then
if [ -z "\${SSH_CONNECTION:-}" ] && [ -z "\${SSH_TTY:-}" ]; then
return 0 2>/dev/null || exit 0
fi
fi
export LOGIN_INFO_SHOWN=1
__smy_login_info_mode="\${LOGIN_INFO_MODE:-${default_mode}}"
if command -v login-info >/dev/null 2>&1; then
if [ "\$__smy_login_info_mode" = "full" ]; then
login-info --full || true
else
login-info --summary || true
fi
fi
EOF
chmod 0644 "$profile_hook"
log "安装完成"
log "提示: 重新登录 SSH 即可看到信息;或手动运行: login-info --full"
}
main "$@"