Git — SSH 代理不工作

Git — SSH 代理不工作

背景故事——我已经在这台电脑(Windows XP)上使用 Git 大约 8 个月了,没有出现任何问题。上周,突然,当我启动 Git 时,它不再要求我输入用户名和密码。

每次我尝试接触远程分支时它都会询问,而且考虑到我有一个密码而不是密码,这真的很烦人。

.profile我在我的文件中使用来自 GitHub 的代码:

SSH_ENV="$HOME/.ssh/environment"

# start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."
    # spawn ssh-agent
    ssh-agent | sed 's/^echo/#echo/' > "$SSH_ENV"
    echo succeeded
    chmod 600 "$SSH_ENV"
    . "$SSH_ENV" > /dev/null
    ssh-add
}

# test for identities
function test_identities {
    # test whether standard identities have been added to the agent already
    ssh-add -l | grep "The agent has no identities" > /dev/null
    if [ $? -eq 0 ]; then
        ssh-add
        # $SSH_AUTH_SOCK broken so we start a new proper agent
        if [ $? -eq 2 ];then
            start_agent
        fi
    fi
}

# check for running ssh-agent with proper $SSH_AGENT_PID
if [ -n "$SSH_AGENT_PID" ]; then
    ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
    if [ $? -eq 0 ]; then
  test_identities
    fi
# if $SSH_AGENT_PID is not properly set, we might be able to load one from
# $SSH_ENV
else
    if [ -f "$SSH_ENV" ]; then
  . "$SSH_ENV" > /dev/null
    fi
    ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
    if [ $? -eq 0 ]; then
        test_identities
    else
        start_agent
    fi
fi

捕捉到它的线路是:

if [ -n "$SSH_AGENT_PID" ]; then

它认为这是一个有效的条目。不用说,我已经用很多不同的方法重新启动并重新登录我的电脑,结果总是一样。我已经echo删除了进程 ID,它不是我电脑上正在运行的进程(根据任务管理器)。

我已经升级了我的 Git(希望它能解决这个问题),我现在运行的是版本 1.7.11-preview20120620

任何帮助将不胜感激。

答案1

简化一点怎么样?

agent_running() {
    [ "$SSH_AUTH_SOCK" ] && { ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]; }
}

env=~/.ssh/agent.env

if ! agent_running && [ -s "$env" ]; then
    . "$env" >/dev/null
fi

if ! agent_running; then
    ssh-agent >"$env"
    . "$env" >/dev/null
    ssh-add
fi

unset env

答案2

在适用于 Windows 2.x 的 Git 的最新版本中,有一个start-ssh-agent.cmd可以在 git 中运行的,bash还有一个cmd

另请参阅此处:https://github.com/git-for-windows/git/wiki/Auto-launching-ssh-agent-when-git-starts

答案3

Python 版本的脚本。在 Windows Vista + Git SSH 工具上进行了测试。来自这里

#!/usr/bin/env python
"""\
ssh agent wrapper to detect or run agent, set environment
and either show it or execute specified command in this
environment.

usage: ssh-agent.py
       ssh-agent.py <command>

examples:
    ssh-agent.py ssh-add
    ssh-agent.py ssh -A user@host
"""

"""
[x] no agent is running
  [x] run agent

[x] agent is running
  [x] set environment
  [x] show how to set environment manually

  [x] if there is command, run it
"""

__version__ = '1.0'
__author__  = 'anatoly techtonik <[email protected]>'
__license__ = 'Public Domain'

import os
import sys

#--[inline shellrun 2.0 import run, run_capture]
import subprocess

class Result(object):
    def __init__(self, command=None, retcode=None, output=None):
        self.command = command or ''
        self.retcode = retcode
        self.output = output
        self.success = False
        if retcode == 0:
            self.success = True

def run(command):
    process = subprocess.Popen(command, shell=True)
    process.communicate()
    return Result(command=command, retcode=process.returncode)

def run_capture(command):
    outpipe = subprocess.PIPE
    process = subprocess.Popen(command, shell=True, stdout=outpipe,
                                                    stderr=outpipe)
    output, _ = process.communicate()
    return Result(command=command, retcode=process.returncode, output=output)
#--[/inline]


SSH_ENV = os.path.expanduser("~/.ssh/agent.env")


def detected():
    if 'SSH_AUTH_SOCK' not in os.environ:
        if os.path.exists(SSH_ENV):
            agentenv = parse()
            os.environ.update(agentenv)
        if 'SSH_AUTH_SOCK' not in os.environ:
            return False
    r = run_capture('ssh-add -L')
    if r.retcode == 0:    # has keys
        return True 
    elif r.retcode == 1:  # no keys
        return True
    elif r.retcode == 2:  # not running
        return False
    else:
        print(r.output)
        return False

def start():
    run('ssh-agent > "%s"' % SSH_ENV)

def parse():
    params = open(SSH_ENV, 'r').read()
    # SSH_AUTH_SOCK=/tmp/ssh-owYJTz7968/agent.7968; export SSH_AUTH_SOCK;
    # SSH_AGENT_PID=6284; export SSH_AGENT_PID;
    # echo Agent pid 6284;
    params = params.replace('; ', '\n')
    params = params.replace(';', '')
    result = dict()
    for line in params.splitlines():
        if '=' not in line:
            continue
        key, value = line.split('=')
        result[key] = value
    return result


def main():
    if '-h' in sys.argv or '--help' in sys.argv:
        sys.exit(__doc__)

    if not detected():
        print('..agent not detected, starting..')
        start()

    if not detected:
        sys.exit('error starting ssh-agent')
    else:
        if os.name == 'nt':
            print('@rem ..agent is running, set environment as:')
            for k,v in parse().items():
                print('set %s=%s' % (k,v))
        else:
            print('# ..agent is running, set environment as:')
            run('cat "%s"' % SSH_ENV)

        if sys.argv[1:]:
            run(sys.argv[1:])


if __name__ == '__main__':
    main()

相关内容