Myself

仕事、プライベート、その他色々
Apr 21
Permalink

対話型コマンドを自動で実行する

expectが狂おしく便利です。
cronとかその他もろもろ自動で実行したいコマンドがあって、そのコマンドが
対話式の場合、stdinの入力待ちで止まってうっとおしいですが、
expectで以下の様に記述すると、全部自動でできて便利~。
sshで自動ログインしたい場合もいちいちパスワード無しのキーを生成しなくてすみますね。

#!/usr/bin/expect

set timeout 30
spawn ssh -i PRIVATE_KEY xxxx@mensoul.jp
expect {
“Are you sure you want to continue connecting” { send “yes\n”; exp_continue; }
“passphrase for key” { send “PASSWORD\n” }
}
interact
Comments (View)
blog comments powered by Disqus