-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrestart-sshd.sh
executable file
·111 lines (100 loc) · 3.05 KB
/
restart-sshd.sh
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
#!/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
#
# Version History:
# 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.
#
# Usage:
# ./restart-sshd.sh
#
########################################################################
# Display script usage information
usage() {
awk '
BEGIN { in_usage = 0 }
/^# Usage:/ { in_usage = 1; print substr($0, 4); next }
/^#{10}/ { if (in_usage) exit }
in_usage && /^#/ { print substr($0, 4) }
' "$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 "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 "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 "systemctl not found. Unable to restart SSH." >&2
exit 1
fi
}
# Main function to execute the script
main() {
case "$1" in
-h|--help) usage ;;
esac
check_sudo
UNAME=$(uname)
case $UNAME in
Darwin*)
restart_macos_sshd
;;
*)
restart_linux_sshd
;;
esac
}
# Execute main function
main "$@"