expect コマンドを使うときがきた...というお話

 
キックスタートでインストールするパッケージを選択する時に知った expect
これは使える!って思ったけど、いざ社内環境構築用のスクリプト書きだしたら使いどころがやってこなかった expect
 
今日、とうとうexpectを使ってみた。
 

キーペアの作成

#!/bin/bash

expect -c "
set timeout 10
spawn ssh-keygen -t rsa
expect -re \"Enter file in which to save the key\ (.\*\):\"
send \"\n\"
expect \"Enter passphrase \(empty for no passphrase\):\"
send \"<注意>ここに設定するパスワード</注意>\n\"
expect \"Enter same passphrase again:\"
send \"<注意>ここに設定するパスワード</注意>\n\"
expect eof exit 0
"

mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

exit 0

※環境 CentOS6.4
 
 
(使うところがあったのがうれしかっただけ)