expectによる自動スイッチ(AIX)

パスワードの入力なしで、ユーザのスイッチをしたい場合

 

以下サンプルコードでシェルを作成し、そのシェルを実行すれば、設定ユーザにスイッチ可能

 

#!/usr/bin/expect
log_user 0
set timeout 5
set fileID [ open /tmp/passfile ] ←パスワードの入力されているファイル
set pass [ gets $fileID ]
spawn su - xxxx               ←xxxx:スイッチさせたいユーザ名
expect -regexp "xxxx.*:"  ←パスワード入力のプロンプト(プロンプト例:xxxxのパスワード:)
send "$pass\n"
log_user 1
interact