-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathreset-ssh-configuration.zsh
More file actions
executable file
·66 lines (63 loc) · 1.98 KB
/
Copy pathreset-ssh-configuration.zsh
File metadata and controls
executable file
·66 lines (63 loc) · 1.98 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
#! /bin/zsh
setopt pipefail
if (( ${EUID} )) {
print -Pr -- "%F{red}${0:t} must be run using sudo.%f"
exit 1
}
print -Pr -- "%F{yellow}\
WARNING: Continuing will overwrite the existing SSH configuration
files in /etc/ssh with the macOS default configuration.
Only host keys (files named \`ssh_host_*_key*\`) will be
preserved.%f"
print -Prn -- "%F{cyan}\
Do you wish to reset SSH to the macOS defaults? [ (y)es / (n)o ]%f "
read -r
case ${(L)REPLY:-no} in
y|yes)
;;
*)
print -Pr -- "%F{cyan}Cancelled." \
"Leaving existing configuration intact.%f"
exit 2
;;
esac
tarball="/usr/share/openssh/default-configuration.tar.bz2"
tmpdir=$(mktemp -d "/var/tmp/sshconfig.XXXXXX")
if (( ${rc::=$?} )) {
print -Pru2 -- "%F{red}Could not create temporary directory.%f"
exit ${rc}
}
backup="${tmpdir}/previous-configuration.tar.bz2"
tar -C / -jcf ${backup} private/etc/ssh
if (( ${rc::=$?} )) {
print -Pru2 -- "%F{red}Could not backup existing configuration.%f"
exit ${rc}
}
print -Pr -- "%F{cyan}Preserved existing configuration at:%f"
print -Pr -- " ${backup}"
tar -tf ${backup} private/etc/ssh | \
egrep 'private/etc/ssh/ssh_host_.*_key' > \
${tmpdir}/host_keys
if (( ${rc::=$?} )) {
print -Pru2 -- "%F{red}Could not identify existing host keys.%f"
exit ${rc}
}
mv /private/etc/ssh /private/etc/ssh.$$
if (( ${rc::=$?} )) {
print -Pru2 -- "%F{red}Could not move aside existing configuration.%f"
exit ${rc}
}
rm -rf /private/etc/ssh.$$ # Ignore failures.
tar -C /private/etc --strip-components 2 -xf ${tarball}
if (( ${rc::=$?} )) {
print -Pru2 -- "%F{red}Resetting SSH configuration failed.%f"
exit ${rc}
}
tar -C /private/etc --strip-components 2 -T ${tmpdir}/host_keys -xf ${backup}
if (( ${rc::=$?} )) {
print -Pru2 -- "%F{red}Could not restore previous host keys.%f"
exit ${rc}
}
print -Pr -- "%F{green}\
The SSH configuration has been reset to macOS defaults."
exit 0