【发布时间】:2017-02-01 20:33:14
【问题描述】:
我一直在尝试自动创建用户和配置 ssh 访问。
到目前为止,我创建了一个访问主机并通过expect创建新用户的脚本,如下:
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 root@000.00.00.00 ""
couldn't read file "<ssh .pub key content> ...
我也试过了:
cat $ssh_key | ssh $user@$ip "cat >> /home/$new_user/.ssh/authorized_keys"
没有成功,我只得到密码查询闪烁,我无法用最后一种方法连接期望。
【问题讨论】:
-
您可能会养成使用shellcheck.net 的习惯——它会发现您的错误。