【问题标题】:Shell script grep rsa key from authorized_keys来自authorized_keys 的Shell 脚本grep rsa 密钥
【发布时间】:2011-12-04 06:40:37
【问题描述】:

此 shell 命令不起作用:

ssh root@IP "if '$(cat ~/.ssh/authorized_keys | grep $KEY)' == '';then;echo $KEY >> ~/.ssh/authorized_keys;done"

$KEY 包含我的公共 RSA 密钥。我要做的是检查我的密钥是否已添加到 authorized_keys 文件中,如果没有,则添加它。 IP当然被替换为真实的IP地址。 知道我做错了什么吗?

编辑: 如果有人好奇,这就是我正在做的事情:

#!/bin/sh

# Get your RSA key.
KEY=""
for line in $(cat ~/.ssh/id_rsa.pub)
do
    KEY="$KEY $line"
done

# Add your RSA key to the machine's authorized_keys if it's not already there.
ssh root@$1 "grep -q '$KEY' ~/.ssh/authorized_keys || echo '$KEY' >> ~/.ssh/authorized_keys"

# Connect to the machine.
ssh root@$1

这个想法是通过 ssh 进入一台机器(使用这个脚本),下次登录时不必输入密码。IP 地址作为命令行参数传递。

【问题讨论】:

    标签: bash shell ssh rsa


    【解决方案1】:

    您在客户端扩展 $()thingie。 (至少)。

    ssh root@IP "grep -q '$KEY' .ssh/authorized_keys || echo '$KEY' >>.ssh/authorized_keys"
    

    看起来是一种更短的方法来做同样的事情。

    【讨论】:

    • 你的命令的$(cat ~/.ssh/authorized_key | grep $KEY)部分在你的本地机器上执行,而不是你ssh到的机器上。
    • 你也忘了告诉这个命令到底出了什么问题。
    • 那行得通。谢谢。抱歉,我没有提到它在做什么。它是说 `if '' == '' 附近有语法错误
    • 哦,是的,我没有注意到您的比较周围没有[ ]。但无论如何它都行不通,所以不用担心:)
    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多