Shell 脚本 grep rsa 密钥(来自authorized_keys)
此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在客户端扩展
$()
事物。 (至少)。看起来是一种更短的方法来完成同样的事情。
You expand
$()
thingie on the client side. (at least).looks like a shorter way to do the same.