【发布时间】: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 地址作为命令行参数传递。
【问题讨论】: