GithubHelp home page GithubHelp logo

bahamas10 / windows-bash-ssh-agent Goto Github PK

View Code? Open in Web Editor NEW
31.0 3.0 14.0 4 KB

Scripts to persist ssh-agent on Bash on Ubuntu on Windows

Home Page: http://daveeddy.com/2017/10/18/persistent-sshagent-on-bash-on-ubuntu-on-windows/

Visual Basic 8.18% Shell 89.56% Batchfile 2.26%

windows-bash-ssh-agent's Introduction

ssh-agent on Bash on Ubuntu on Windows

Scripts to use to persist ssh-agent on Bash on Windows on Ubuntu

How To

Read the blog post here

http://daveeddy.com/2017/10/18/persistent-sshagent-on-bash-on-ubuntu-on-windows/

Install

Put the scripts into place

mkdir -p ~/bin
cp -vf ./start-ssh-agent ~/bin

Create a hidden scheduled task on Windows to then start this script at login

powershell -noprofile -windowstyle hidden -command "c:\windows\system32\bash.exe -c "~/bin/start-ssh-agent""

Deprecated Process

NOTE: This is the old way of installing this - the new process is explained in the blog post and way more simple.

The above blog post illustrates all of the steps required to make this work. You can run the included install script to put the script files into place.

$ cd old
$ ./install
mkdir: created directory '/home/dave/bin'
'/home/dave/userprofile' -> '/mnt/c/Users/dave'
mkdir: created directory '/home/dave/userprofile/Documents/scripts'
'/home/dave/userprofile/Documents/scripts/start-hidden.vbs' -> '/home/dave/dev/windows-bash-ssh-agent/start-hidden.vbs'
'/home/dave/userprofile/Documents/scripts/start-ssh-agent.bat' -> '/home/dave/dev/windows-bash-ssh-agent/start-ssh-agent.bat'
'/home/dave/bin/start-ssh-agent' -> '/home/dave/dev/windows-bash-ssh-agent/start-ssh-agent'

The Windows username can be supplied as the first argument - $USER is assumed.

License

MIT License

windows-bash-ssh-agent's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

windows-bash-ssh-agent's Issues

ssh-add git push

Hello to all I have an issue with this command. And I don't know what is the issue.
I have a script on a server Windows 10 git version 2.23.0.windows.1
In the remote server I have a script that does a push like this:

ssh-agent bash -c 'ssh-add $POLARION_GIT_SSH_KEY; git remote | xargs -L1 -I R git push R develop'

The log from the local machine is:
Log_no_branch.txt

And when I do a normal push git push silvia2 develop I successfully download the branch.
Log_branch.txt

what can be the issue with the configuration ? or the command.

thanks in advance.

Alternative to windows scheduler

To avoid using windows scheduler, I did the following:

  • ~/bin/start-ssh-agent as per this git
  • vbs script to run powershell in the background
  • .bash_profile snippet to launch ssh-agent
  • In combination with ssh config for AddKeysToAgent and ControlMaster

Advantages:

  • Only runs ssh-agent when you start using WSL
  • Isn't tied to a specific ssh key (your solution isn't, but others are)
  • AddKeysToAgent means that initial login to server prompts for key password if required and adds to ssh-agent

Details:

VBS script:

Dim shell,command
command = "powershell.exe -nologo -noprofile -command ""c:\windows\system32\bash.exe -c ""~/bin/start-ssh-agent"""""
Set shell = CreateObject("WScript.Shell")
shell.Run command,0

.bash_profile:

envfile=~/.ssh/environment
START_SSH_AGENT_SCRIPT="C:\Users/XXX\Documents\power-shell-hidden.vbs"

function ssh_agent_conn_ok() {
    ssh-add -l &> /dev/null
    if [ "$?" == 2 ]; then
        # Not ready
        return 1
    else
        return 0
    fi
}

function ssh_agent_setup_env() {
    test -r "$envfile" && \
        eval "$(<$envfile)" > /dev/null
}


function ssh_agent_setup() {
    if ! ssh_agent_conn_ok; then
        ssh_agent_setup_env

        if ! ssh_agent_conn_ok; then
            echo "Starting SSH Agent"

            pushd /mnt/c > /dev/null
            cmd.exe /c "$START_SSH_AGENT_SCRIPT"
            popd > /dev/null

            local x=0
            while [ "$x" -lt 100 ]; do
                # NB: File is created and then appended to so can't just wait for file
                ssh_agent_setup_env
                if ssh_agent_conn_ok; then
                    break
                fi

                x=$((x+1))
                sleep .1
            done

            if ssh_agent_conn_ok; then
                echo "SSH Agent started"
            else
                echo "SSH agent not found within time limit!"
            fi
        fi
    fi
}

ssh_agent_setup

example ssh config

~$ cat .ssh/config
Host foo-server
  ControlPath ~/.ssh/masters/cm-%r@%h:%p
  ControlMaster auto
  ControlPersist 8h
  IdentityFile ~/.ssh/foo_id_rsa
  AddKeysToAgent yes

ssh asks for passphrase every time

I use Windows 10 (latest public build) on a Surface Pro 6; I run the script manually in a cmd window as follows:
c:\windows\system32\bash.exe -c "~/bin/start-ssh-agent"
Its output is something like Agent pid 4

I have a source ~/.ssh/environment line in my .bashrc.

Still, every time I call ssh , I'm prompted to enter my id_rsa passphrase, even in the same WSL session.
What could be the reason?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.