Skip to content

Instantly share code, notes, and snippets.

@andkirby
Created August 19, 2020 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andkirby/658d94acb03b52ded957be1a157dd760 to your computer and use it in GitHub Desktop.
Save andkirby/658d94acb03b52ded957be1a157dd760 to your computer and use it in GitHub Desktop.
Synchronize .ssh files from Windows file system to Ubuntu WSL
#!/usr/bin/env bash
##################################################################
# Synchronize .ssh files from Windows file system to Ubuntu WSL #
##################################################################
set -o pipefail
set -o errexit
set -o nounset
#set -o xtrace
# Set magic variables for current file & dir
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
readonly __dir __file
# colors
c_green="\e[92m"
c_yellow="\e[93m"
c_red="\e[91m"
c_reset="\e[m"
user_home=$(find /mnt/c/Users/* -maxdepth 0 -type d -perm -u+w | grep -v '/Public')
# echo -e "${c_yellow}Found ${user_home} user directory.${c_reset}\n"
cp ${user_home}/.ssh/{id_*,config} ~/.ssh
# Set unix paths
sed -r -i.bak 's|D:/|/mnt/d/|g' ~/.ssh/config
sed -r -i.bak 's|C:/|/mnt/c/|g' ~/.ssh/config
sed -r -i.bak 's| /([cd])/| /mnt/\1/|g' ~/.ssh/config
# copy non-ssh-directory file to set correct permissions
# command "echo '{}'" removes hidden characters
cat ~/.ssh/config | grep IdentityFile | grep -v .ssh | awk '{print $2}' | grep -Eo '^[A-Za-z/:_.-]+'\
| xargs -i echo '{}' | sort | uniq | while read id_file; do
if ! cp ${id_file} /dev/null; then
# echo -e "${c_red}notice: File ${id_file} not found.${c_reset}" > /dev/stderr
# echo -e "Replaced path with '/dev/null'" > /dev/stderr
sed -r -i.bak 's|'${id_file}'|/dev/null|g' ~/.ssh/config
continue
fi
local_id_file=${id_file//\//_}
local_id_file=${HOME}'/.ssh/'${local_id_file#"_"}
cp --dereference ${id_file} ${local_id_file}
sed -r -i.bak 's| '${id_file}'| '${local_id_file}'|g' ~/.ssh/config
done
# set proper permissions
chmod og-rwx,u-x,u+rwX -R ~/.ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment