私は、ユーザーの作成と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"
を成功せず、私はパスワードのみのクエリは、私がすることはできません、点滅しますexpectをこの最後のメソッドに接続します。
あなたはhttp://shellcheck.net/を使用する習慣をつくるかもしれません。あなたのバグに遭遇したでしょう。 –