Skip to content

Instantly share code, notes, and snippets.

@duebbert
Last active September 3, 2019 15:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duebbert/6a152ad2030e8dcb6d8608027588e4a8 to your computer and use it in GitHub Desktop.
Save duebbert/6a152ad2030e8dcb6d8608027588e4a8 to your computer and use it in GitHub Desktop.
Configuring PyCharm, Bash on Ubuntu on Windows 10 & WinSCP to work with SSH keys in Keepass using the KeeAgent Plugin

Configuring PyCharm, Bash on Ubuntu on Windows 10 & WinSCP to work with SSH keys in Keepass using the KeeAgent Plugin

Description

This is a small guide on how to set up PyCharm, Bash on Ubuntu on Windows & WinSCP on Windows to use SSH keys which are saved in KeePass. This significantly improves the user experience while keeping the keys save. It also alleviates any issues with Cygwin which prompted me to set this all up (see setup with Babun before https://gist.github.com/duebbert/1cd30115cf8ade37aa5b619080154e85)

Installation

Configure KeeAgent

  1. Start KeePass
  2. Tools -> Options -> KeeAgent
  3. Activate "Create mSys compatible socket file" and set path to a file like C:\Users\<USERNAME>\keeagent_msys.socket
  4. Add SSH keys to an entry in KeePass. You can just add them as attachments and then activate them in the tab "KeeAgent".

Set up Bash on Ubuntu on Windows 10

  1. Start Bash on Ubuntu on Windows
  2. Get python script to bridge between WSL and Windows from https://gist.github.com/duebbert/4298b5f4eb7cc064b09e9d865dd490c9: wget https://gist.githubusercontent.com/duebbert/4298b5f4eb7cc064b09e9d865dd490c9/raw/2d47ef8e88087d79dbf77b64117f3b5a13d6598a/msysgit2unix-socket.py
  3. Create a startup script ~/start_keepass_agent.sh which will also be used by a batch file later:
#!/bin/sh

export KEEAGENT_SOCK="/mnt/c/Users/kduebbert/keeagent_msys.socket"

ssh_auth_tmpdir=`mktemp --tmpdir --directory keeagent-ssh.XXXXXXXXXX`
export SSH_AUTH_SOCK="${ssh_auth_tmpdir}/agent.$$"
PIDFILE="${ssh_auth_tmpdir}/pid.$$"

python3 ~/msysgit2unix-socket.py --pidfile $PIDFILE $KEEAGENT_SOCK:$SSH_AUTH_SOCK
  1. Add the file to .bashrc so that you can use SSH in Bash seamlessly: source ~/start_keepass_agent.sh

Create batch file to use git from Bash

  1. Create batch file in e.g. d:\git.bat: https://gist.github.com/duebbert/c4f26c9d0691b7d4a0e932e7d9f36271
@echo off
SETLOCAL enableextensions enabledelayedexpansion

:: Thanks: 
::     - https://stackoverflow.com/questions/37000166/getting-pycharm-to-recognize-python-on-the-windows-linux-subsystem-bash-on-wind


:: ###################################################################################
:: Set up the current directory, so that we can change into it before calling git
:: ###################################################################################

:: Extract the drive letter
SET "DIR=%cd%"

:: Convert drive letter to lower case and add /mnt/
FOR %%i IN ("A:\=/mnt/a/" "B:\=/mnt/b/" "C:\=/mnt/c/" "D:\=/mnt/d/" "E:\=/mnt/e/" "F:\=/mnt/f/" "G:\=/mnt/g/" "H:\=/mnt/h/" "I:\=/mnt/i/" "J:\=/mnt/j/" "K:\=/mnt/k/" "L:\=/mnt/l/" "M:\=/mnt/m/" "N:\=/mnt/n/" "O:\=/mnt/o/" "P:\=/mnt/p/" "Q:\=/mnt/q/" "R:\=/mnt/r/" "S:\=/mnt/s/" "T:\=/mnt/t/" "U:\=/mnt/u/" "V:\=/mnt/v/" "W:\=/mnt/w/" "X:\=/mnt/x/" "Y:\=/mnt/y/" "Z:\=/mnt/z/") DO CALL SET "DIR=%%DIR:%%~i%%"

:: Change backslash to forward slash. Must run after converting the drive letter.
SET "DIR=%DIR:\=/%"



:: ###################################################################################
:: Fix the arguments
:: ###################################################################################

SET ARGS=%*

:: Convert drive letter to lower case and add /mnt/
FOR %%i IN ("A:\=/mnt/a/" "B:\=/mnt/b/" "C:\=/mnt/c/" "D:\=/mnt/d/" "E:\=/mnt/e/" "F:\=/mnt/f/" "G:\=/mnt/g/" "H:\=/mnt/h/" "I:\=/mnt/i/" "J:\=/mnt/j/" "K:\=/mnt/k/" "L:\=/mnt/l/" "M:\=/mnt/m/" "N:\=/mnt/n/" "O:\=/mnt/o/" "P:\=/mnt/p/" "Q:\=/mnt/q/" "R:\=/mnt/r/" "S:\=/mnt/s/" "T:\=/mnt/t/" "U:\=/mnt/u/" "V:\=/mnt/v/" "W:\=/mnt/w/" "X:\=/mnt/x/" "Y:\=/mnt/y/" "Z:\=/mnt/z/") DO CALL SET "ARGS=%%ARGS:%%~i%%"

:: Backslash to forward slash
SET ARGS=!ARGS:\=/!


:: ###################################################################################
:: Execute it!!!
:: ###################################################################################

If %PROCESSOR_ARCHITECTURE% == x86 (
    C:\Windows\sysnative\bash.exe --login -c '. ~/start_keepass_agent.sh; cd "%DIR%"; git %ARGS%'
) Else (
    bash.exe --login -c '. ~/start_keepass_agent.sh; cd "%DIR%"; git %ARGS%'
)
  1. Save script

Configure PyCharm

  1. Start PyCharm
  2. Go to Settings -> Version Control -> Git: Path to Git executable: D:\git.bat SSH executable: Native
  3. Try out to clone a Git project or update one

Configure WinSCP

WinSCP should work out of the box. Enter the server name, port and user name and connect to a server.

Copy link

ghost commented Feb 18, 2018

This doesn't work for me, putty and winscp use keeagent fine, but keeagent never reports bash trying to access the key. Any ideas how to debug?
Update: apparently it fails the first time, and works after that. The first time I issue a git command I am prompted for a password. I ctrl-c out of that, repeat the command, then it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment