-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrestart-sshd.sh
More file actions
executable file
·123 lines (112 loc) · 3.6 KB
/
restart-sshd.sh
File metadata and controls
executable file
·123 lines (112 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/sh
########################################################################
# restart-sshd.sh: Restart SSH Daemon
#
# Description:
# This script restarts the SSH daemon. It supports both macOS (using
# launchctl) and Linux systems (using systemctl).
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# ./restart-sshd.sh
#
# Requirements:
# - sudo(8)
# - awk(1) for usage() header extraction
# - macOS: /System/Library/LaunchDaemons/ssh.plist with launchctl(1)
# - Linux: systemctl(1)
#
# Version History:
# v2.0 2025-08-04
# Fix OS detection by matching 'Darwin' explicitly instead of using wildcard.
# v1.9 2025-06-23
# Unified usage output to display full script header and support common help/version options.
# v1.8 2025-04-13
# Unify log level formatting using [INFO], [WARN], and [ERROR] tags.
# v1.7 2025-03-22
# Unify usage information by extracting help text from header comments.
# v1.6 2025-03-17
# Encapsulated all logic into functions and introduced main function.
# v1.5 2025-03-13
# Redirected error messages to stderr for better logging and debugging.
# v1.4 2025-03-05
# Added sudo privilege check when --sudo option is specified.
# v1.3 2025-01-03
# Added existence check for launchctl command in macOS environment
# to improve error handling and reliability.
# v1.2 2023-12-23
# Refactored for POSIX compliance. Replaced Bash-specific syntax
# with POSIX standard commands and structures. Enhanced portability
# and compatibility across different UNIX-like systems.
# v1.1 2023-12-06
# Improved system environment check and added command/file existence verification.
# v1.0 2022-09-13
# Initial release.
#
########################################################################
# Display full script header information extracted from the top comment block
usage() {
awk '
BEGIN { in_header = 0 }
/^#{10,}$/ { if (!in_header) { in_header = 1; next } else exit }
in_header && /^# ?/ { print substr($0, 3) }
' "$0"
exit 0
}
# Check if the user has sudo privileges (password may be required)
check_sudo() {
if ! sudo -v 2>/dev/null; then
echo "[ERROR] This script requires sudo privileges. Please run as a user with sudo access." >&2
exit 1
fi
}
# Check for necessary commands and files before execution
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Restart SSH daemon on macOS
restart_macos_sshd() {
SSH_PLIST="/System/Library/LaunchDaemons/ssh.plist"
if ! command_exists launchctl; then
echo "[ERROR] launchctl command not found. Unable to restart SSH on macOS." >&2
exit 1
fi
if [ -f "$SSH_PLIST" ]; then
sudo launchctl unload -w "$SSH_PLIST" && sudo launchctl load -w "$SSH_PLIST"
else
echo "[ERROR] SSH plist file not found: $SSH_PLIST" >&2
exit 1
fi
}
# Restart SSH daemon on Linux
restart_linux_sshd() {
if command_exists systemctl; then
sudo systemctl restart ssh.service
else
echo "[ERROR] systemctl not found. Unable to restart SSH." >&2
exit 1
fi
}
# Main entry point of the script
main() {
case "$1" in
-h|--help|-v|--version) usage ;;
esac
check_sudo
UNAME=$(uname)
case "$UNAME" in
Darwin)
restart_macos_sshd
;;
*)
restart_linux_sshd
;;
esac
return 0
}
# Execute main function
main "$@"