Shell 脚本 grep rsa 密钥(来自authorized_keys)

发布于 2024-12-19 12:43:59 字数 712 浏览 0 评论 0原文

此 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 地址作为命令行传递争论。

This shell command isn't working:

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

$KEY contains my public RSA key. What I'm trying to do is to check if my key has been added to the authorized_keys file, if not, then to add it. IP of course is replaced with a real ip address.
Any idea what I'm doing wrong?

Edit:
In case anyone is curious, this is what I was doing:

#!/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

The idea was to ssh into a machine (using this script) and not have to enter the password in the next time I log in. The IP address is passed as a command line argument.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-12-26 12:43:59

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

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

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

You expand $() thingie on the client side. (at least).

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

looks like a shorter way to do the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文