设置SSH身份在的.bashrc [英] Set SSH Identities In Bashrc

查看:636
本文介绍了设置SSH身份在的.bashrc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.bashrc文件,开始我SSH代理(从这里)拍摄。

每当我开始我的机器我一直有我的身份添加到它(使用 SSH-添加的〜/ .ssh my_private键)。

不知道bash脚本编程了,什么(在这里)我调整这个脚本在我的身份加入?

  SSH_ENV =$ HOME /的.ssh /环境#启动的ssh-agent
功能start_agent {
    回声初始化新的SSH代理...
    #产卵的ssh-agent
    SSH代理| SED的/ ^回声/#回声/'> $ SSH_ENV
    回声成功
    搭配chmod 600$ SSH_ENV
    。 $ SSH_ENV>的/ dev / null的
    SSH-ADD
}对于身份测试#
功能test_identities {
    #测试标准的身份是否已经被添加到代理已经
    一次ssh-add -l | grep的代理没有身份>的/ dev / null的
    如果[$? -eq 0];然后
        SSH-ADD
        #$ SSH_AUTH_SOCK打破,所以我们开始一个新的合适的代理
        如果[$? -eq 2];然后
            start_agent
        科幻
    科幻
}#检查正在运行的ssh-agent适当$ SSH_AGENT_PID
如果[-n$ SSH_AGENT_PID];然后
    PS -ef | grep的$ SSH_AGENT_PID| grep的SSH-剂>的/ dev / null的
    如果[$? -eq 0];然后
    test_identities
    科幻
#如果$ SSH_AGENT_PID设置不正确,我们也许能够加载一个来自
#$ SSH_ENV
其他
    如果[-f$ SSH_ENV];然后
    。 $ SSH_ENV>的/ dev / null的
    科幻
    PS -ef | grep的$ SSH_AGENT_PID| grep的-v grep的| grep的SSH-剂>的/ dev / null的
    如果[$? -eq 0];然后
        test_identities
    其他
        start_agent
    科幻
科幻


解决方案

明白了,有一个资深的时刻。

在test_identities功能,只需完成了使用ssh-ADD开头的行。

I have a .bashrc file to start my ssh agent (taken from here).

Whenever I start my machine I keep having to add my identities to it (using ssh-add ~/.ssh my_private key).
Not knowing bash scripting much, what (and where) do I adjust this script to add in my identities?

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 -v grep | grep ssh-agent > /dev/null
    if [ $? -eq 0 ]; then
        test_identities
    else
        start_agent
    fi
fi

解决方案

Got it, having a senior moment.
In the test_identities function, just completed the line that starts with ssh-add.

这篇关于设置SSH身份在的.bashrc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆