-
Notifications
You must be signed in to change notification settings - Fork 4
/
restart-sshd.sh
executable file
·64 lines (58 loc) · 1.72 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
#!/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: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# 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
#
########################################################################
# Check for necessary commands and files before execution
command_exists() {
command -v "$1" >/dev/null 2>&1
}
restart_macos_sshd() {
SSH_PLIST="/System/Library/LaunchDaemons/ssh.plist"
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"
exit 1
fi
}
restart_linux_sshd() {
if command_exists systemctl; then
sudo systemctl restart ssh.service
else
echo "systemctl not found. Unable to restart SSH."
exit 1
fi
}
# Determine the operating system and restart SSH accordingly
UNAME=$(uname)
case $UNAME in
Darwin*)
restart_macos_sshd
;;
*)
restart_linux_sshd
;;
esac