Created
January 19, 2022 06:06
-
-
Save deardooley/89eff321f90ca0d573b611389680a3ca to your computer and use it in GitHub Desktop.
Simple bash script to format ssh config files in place.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# format-ssh-config | |
# | |
# author: [email protected] | |
# | |
# Simple utility to format a ssh config. | |
# - Commented out lines are ignored. | |
# - Option and values are column aligned | |
# - Most frequently used options are formatted. Uncomment any others you need and adjust column spacing accordingly. | |
if [[ -f "$1" ]]; then | |
ts=$(date +%s) | |
tmpDir=$(dirname $1) | |
pushd "$tmpDir" 1>/dev/null | |
tmpFile=".tmp-$(basename $1)-$ts" | |
cp "$1" "$tmpFile" | |
cat "$tmpFile" | \ | |
sed -e 's/^[ \t]*//g' | \ | |
sed -e 's/^Hostname[ \t]*\(.*\)$/ Hostname \1/g' | \ | |
sed -e 's/^HostName[ \t]*\(.*\)$/ Hostname \1/g' | \ | |
sed -e 's/^IdentityFile[ \t]*\(.*\)$/ IdentityFile \1/g' | \ | |
sed -e 's/^UserKnownHostsFile[ \t]*\(.*\)$/ UserKnownHostsFile \1/g' | \ | |
sed -e 's/^StrictHostKeyChecking[ \t]*\(.*\)$/ StrictHostKeyChecking \1/g' | \ | |
sed -e 's/^ProxyJump[ \t]*\(.*\)$/ ProxyJump \1/g' | \ | |
sed -e 's/^Port[ \t]*\(.*\)$/ Port \1/g' | \ | |
sed -e 's/^ProxyCommand[ \t]*\(.*\)$/ ProxyCommand \1/g' | \ | |
sed -e 's/^User[ \t ][ \t]*\(.*\)$/ User \1/g' | \ | |
sed -e 's/^ControlMaster[ \t]*\(.*\)$/ ControlMaster \1/g' > "$1" | |
# ed -e 's/^AcceptEnv[ \t]*(.*)$/ AcceptEnv 1/g' | \ | |
# sed -e 's/^AddKeysToAgent[ \t]*(.*)$/ AddKeysToAgent 1/g' | \ | |
# sed -e 's/^ADDRESS[ \t]*(.*)$/ ADDRESS 1/g' | \ | |
# sed -e 's/^AddressFamily[ \t]*(.*)$/ AddressFamily 1/g' | \ | |
# sed -e 's/^BatchMode[ \t]*(.*)$/ BatchMode 1/g' | \ | |
# sed -e 's/^BindAddress[ \t]*(.*)$/ BindAddress 1/g' | \ | |
# sed -e 's/^BindInterface[ \t]*(.*)$/ BindInterface 1/g' | \ | |
# sed -e 's/^CanonicalDomains[ \t]*(.*)$/ CanonicalDomains 1/g' | \ | |
# sed -e 's/^CanonicalizeFallbackLocal[ \t]*(.*)$/ CanonicalizeFallbackLocal 1/g' | \ | |
# sed -e 's/^CanonicalizeHostname[ \t]*(.*)$/ CanonicalizeHostname 1/g' | \ | |
# sed -e 's/^CanonicalizeMaxDots[ \t]*(.*)$/ CanonicalizeMaxDots 1/g' | \ | |
# sed -e 's/^CanonicalizePermittedCNAMEs[ \t]*(.*)$/ CanonicalizePermittedCNAMEs 1/g' | \ | |
# sed -e 's/^CASignatureAlgorithms[ \t]*(.*)$/ CASignatureAlgorithms 1/g' | \ | |
# sed -e 's/^CertificateFile[ \t]*(.*)$/ CertificateFile 1/g' | \ | |
# sed -e 's/^ChallengeResponseAuthentication[ \t]*(.*)$/ ChallengeResponseAuthentication 1/g' | \ | |
# sed -e 's/^CheckHostIP[ \t]*(.*)$/ CheckHostIP 1/g' | \ | |
# sed -e 's/^Ciphers[ \t]*(.*)$/ Ciphers 1/g' | \ | |
# sed -e 's/^ClearAllForwardings[ \t]*(.*)$/ ClearAllForwardings 1/g' | \ | |
# sed -e 's/^Compression[ \t]*(.*)$/ Compression 1/g' | \ | |
# sed -e 's/^confirm[ \t]*(.*)$/ confirm 1/g' | \ | |
# sed -e 's/^ConnectionAttempts[ \t]*(.*)$/ ConnectionAttempts 1/g' | \ | |
# sed -e 's/^ConnectTimeout[ \t]*(.*)$/ ConnectTimeout 1/g' | \ | |
# sed -e 's/^ControlMaster[ \t]*(.*)$/ ControlMaster 1/g' | \ | |
# sed -e 's/^ControlPath[ \t]*(.*)$/ ControlPath 1/g' | \ | |
# sed -e 's/^ControlPersist[ \t]*(.*)$/ ControlPersist 1/g' | \ | |
# sed -e 's/^DynamicForward[ \t]*(.*)$/ DynamicForward 1/g' | \ | |
# sed -e 's/^EnableSSHKeysign[ \t]*(.*)$/ EnableSSHKeysign 1/g' | \ | |
# sed -e 's/^EscapeChar[ \t]*(.*)$/ EscapeChar 1/g' | \ | |
# sed -e 's/^ExitOnForwardFailure[ \t]*(.*)$/ ExitOnForwardFailure 1/g' | \ | |
# sed -e 's/^FingerprintHash[ \t]*(.*)$/ FingerprintHash 1/g' | \ | |
# sed -e 's/^ForkAfterAuthentication[ \t]*(.*)$/ ForkAfterAuthentication 1/g' | \ | |
# sed -e 's/^ForwardAgent[ \t]*(.*)$/ ForwardAgent 1/g' | \ | |
# sed -e 's/^ForwardX11[ \t]*(.*)$/ ForwardX11 1/g' | \ | |
# sed -e 's/^ForwardX11Timeout[ \t]*(.*)$/ ForwardX11Timeout 1/g' | \ | |
# sed -e 's/^ForwardX11Trusted[ \t]*(.*)$/ ForwardX11Trusted 1/g' | \ | |
# sed -e 's/^GatewayPorts[ \t]*(.*)$/ GatewayPorts 1/g' | \ | |
# sed -e 's/^GlobalKnownHostsFile[ \t]*(.*)$/ GlobalKnownHostsFile 1/g' | \ | |
# sed -e 's/^GSSAPIAuthentication[ \t]*(.*)$/ GSSAPIAuthentication 1/g' | \ | |
# sed -e 's/^GSSAPIDelegateCredentials[ \t]*(.*)$/ GSSAPIDelegateCredentials 1/g' | \ | |
# sed -e 's/^HashKnownHosts[ \t]*(.*)$/ HashKnownHosts 1/g' | \ | |
# sed -e 's/^Host[ \t]*(.*)$/ Host 1/g' | \ | |
# sed -e 's/^HostbasedAcceptedAlgorithms[ \t]*(.*)$/ HostbasedAcceptedAlgorithms 1/g' | \ | |
# sed -e 's/^HostbasedAuthentication[ \t]*(.*)$/ HostbasedAuthentication 1/g' | \ | |
# sed -e 's/^HostKeyAlgorithms[ \t]*(.*)$/ HostKeyAlgorithms 1/g' | \ | |
# sed -e 's/^HostKeyAlias[ \t]*(.*)$/ HostKeyAlias 1/g' | \ | |
# sed -e 's/^Hostname[ \t]*(.*)$/ Hostname 1/g' | \ | |
# sed -e 's/^IdentitiesOnly[ \t]*(.*)$/ IdentitiesOnly 1/g' | \ | |
# sed -e 's/^IdentityAgent[ \t]*(.*)$/ IdentityAgent 1/g' | \ | |
# sed -e 's/^IdentityFile[ \t]*(.*)$/ IdentityFile 1/g' | \ | |
# sed -e 's/^IgnoreUnknown[ \t]*(.*)$/ IgnoreUnknown 1/g' | \ | |
# sed -e 's/^Include[ \t]*(.*)$/ Include 1/g' | \ | |
# sed -e 's/^KbdInteractiveAuthentication[ \t]*(.*)$/ KbdInteractiveAuthentication 1/g' | \ | |
# sed -e 's/^KbdInteractiveDevices[ \t]*(.*)$/ KbdInteractiveDevices 1/g' | \ | |
# sed -e 's/^KexAlgorithms[ \t]*(.*)$/ KexAlgorithms 1/g' | \ | |
# sed -e 's/^KnownHostsCommand[ \t]*(.*)$/ KnownHostsCommand 1/g' | \ | |
# sed -e 's/^LocalCommand[ \t]*(.*)$/ LocalCommand 1/g' | \ | |
# sed -e 's/^LocalForward[ \t]*(.*)$/ LocalForward 1/g' | \ | |
# sed -e 's/^localhost[ \t]*(.*)$/ localhost 1/g' | \ | |
# sed -e 's/^localuser[ \t]*(.*)$/ localuser 1/g' | \ | |
# sed -e 's/^LogLevel[ \t]*(.*)$/ LogLevel 1/g' | \ | |
# sed -e 's/^LogVerbose[ \t]*(.*)$/ LogVerbose 1/g' | \ | |
# sed -e 's/^MACs[ \t]*(.*)$/ MACs 1/g' | \ | |
# sed -e 's/^Match[ \t]*(.*)$/ Match 1/g' | \ | |
# sed -e 's/^NoHostAuthenticationForLocalhost[ \t]*(.*)$/ NoHostAuthenticationForLocalhost 1/g' | \ | |
# sed -e 's/^NumberOfPasswordPrompts[ \t]*(.*)$/ NumberOfPasswordPrompts 1/g' | \ | |
# sed -e 's/^PasswordAuthentication[ \t]*(.*)$/ PasswordAuthentication 1/g' | \ | |
# sed -e 's/^PermitLocalCommand[ \t]*(.*)$/ PermitLocalCommand 1/g' | \ | |
# sed -e 's/^PermitRemoteOpen[ \t]*(.*)$/ PermitRemoteOpen 1/g' | \ | |
# sed -e 's/^PKCS11Provider[ \t]*(.*)$/ PKCS11Provider 1/g' | \ | |
# sed -e 's/^Port[ \t]*(.*)$/ Port 1/g' | \ | |
# sed -e 's/^PreferredAuthentications[ \t]*(.*)$/ PreferredAuthentications 1/g' | \ | |
# sed -e 's/^ProxyCommand[ \t]*(.*)$/ ProxyCommand 1/g' | \ | |
# sed -e 's/^ProxyJump[ \t]*(.*)$/ ProxyJump 1/g' | \ | |
# sed -e 's/^ProxyUseFdpass[ \t]*(.*)$/ ProxyUseFdpass 1/g' | \ | |
# sed -e 's/^PubkeyAcceptedAlgorithms[ \t]*(.*)$/ PubkeyAcceptedAlgorithms 1/g' | \ | |
# sed -e 's/^PubkeyAuthentication[ \t]*(.*)$/ PubkeyAuthentication 1/g' | \ | |
# sed -e 's/^RekeyLimit[ \t]*(.*)$/ RekeyLimit 1/g' | \ | |
# sed -e 's/^RemoteCommand[ \t]*(.*)$/ RemoteCommand 1/g' | \ | |
# sed -e 's/^RemoteForward[ \t]*(.*)$/ RemoteForward 1/g' | \ | |
# sed -e 's/^RequestTTY[ \t]*(.*)$/ RequestTTY 1/g' | \ | |
# sed -e 's/^RevokedHostKeys[ \t]*(.*)$/ RevokedHostKeys 1/g' | \ | |
# sed -e 's/^SecurityKeyProvider[ \t]*(.*)$/ SecurityKeyProvider 1/g' | \ | |
# sed -e 's/^SendEnv[ \t]*(.*)$/ SendEnv 1/g' | \ | |
# sed -e 's/^ServerAliveCountMax[ \t]*(.*)$/ ServerAliveCountMax 1/g' | \ | |
# sed -e 's/^ServerAliveInterval[ \t]*(.*)$/ ServerAliveInterval 1/g' | \ | |
# sed -e 's/^SessionType[ \t]*(.*)$/ SessionType 1/g' | \ | |
# sed -e 's/^SetEnv[ \t]*(.*)$/ SetEnv 1/g' | \ | |
# sed -e 's/^StdinNull[ \t]*(.*)$/ StdinNull 1/g' | \ | |
# sed -e 's/^StreamLocalBindMask[ \t]*(.*)$/ StreamLocalBindMask 1/g' | \ | |
# sed -e 's/^StreamLocalBindUnlink[ \t]*(.*)$/ StreamLocalBindUnlink 1/g' | \ | |
# sed -e 's/^StrictHostKeyChecking[ \t]*(.*)$/ StrictHostKeyChecking 1/g' | \ | |
# sed -e 's/^subsystem[ \t]*(.*)$/ subsystem 1/g' | \ | |
# sed -e 's/^SyslogFacility[ \t]*(.*)$/ SyslogFacility 1/g' | \ | |
# sed -e 's/^TCPKeepAlive[ \t]*(.*)$/ TCPKeepAlive 1/g' | \ | |
# sed -e 's/^Tunnel[ \t]*(.*)$/ Tunnel 1/g' | \ | |
# sed -e 's/^UpdateHostKeys[ \t]*(.*)$/ UpdateHostKeys 1/g' | \ | |
# sed -e 's/^User[ \t]*(.*)$/ User 1/g' | \ | |
# sed -e 's/^UserKnownHostsFile[ \t]*(.*)$/ UserKnownHostsFile 1/g' | \ | |
# sed -e 's/^VerifyHostKeyDNS[ \t]*(.*)$/ VerifyHostKeyDNS 1/g' | \ | |
# sed -e 's/^VisualHostKey[ \t]*(.*)$/ VisualHostKey 1/g' | \ | |
# sed -e 's/^XAuthLocation[ \t]*(.*)$/ XAuthLocation 1/g' | \ | |
# | |
rm "$tmpFile" | |
popd 1>/dev/null | |
else | |
echo "Path '$i' is not a file" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment