0
我一直在尝试自动创建用户和配置ssh访问。如何通过bash脚本向主机添加ssh密钥
到目前为止,我创建了一个访问主机的脚本,并创建希望通过新的用户,如下所示:
expect -c '
spawn ssh '$user'@'$ip';
expect "assword: ";
send "'$passwd'\r";
expect "prompt\n";
send "adduser '$new_user'\r";
...
send "mkdir /home/'$new_user'/.ssh\r";
expect "prompt\n";
send "exit\r";
'
这工作得很好,在那之后我需要在关键的.pub文件添加到授权密钥文件在主机中,那里是开始的地方。
我想:
ssh_key='/home/.../key.pub'
content=$(cat $ssh_key)
expect -c '
spawn ssh '$user'@'$ip' "echo '$content' >> /home/'$new_user'/.ssh/authorized_keys;
expect "password:";
...
'
,并得到:
missing "
while executing
"spawn ssh [email protected] ""
couldn't read file "<ssh .pub key content> ...
我也试过:
cat $ssh_key | ssh [email protected]$ip "cat >> /home/$new_user/.ssh/authorized_keys"
没有成功,我只得到密码查询闪烁,我不能将期望与最后一种方法联系起来。
你可能会习惯使用http://shellcheck.net/ - 它会抓住你的bug。 –