0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

expectコマンドを使ったadduserの自動化

Last updated at Posted at 2022-01-31

EC2のユーザデータでadduserコマンドを使いたいが対話モードで入力が必要。そこでexpectコマンドを使って自動で対話モード入力を行ってみることにした。expectコマンドを使ったスクリプトscript.shを作成する。script.shを作成した後、chmodで実行権限を付与するのを忘れないこと。

chmod +x script.sh
./script.sh

script.shの中身は以下の通り。以下の例ではユーザ名が「test123」、パスワードが「test」としている。パスワード入力後に少し待たないとうまくいかないので、set timeout 3を入れている。

script.sh
# !/bin/bash

expect -c "
spawn sudo adduser test123
expect \"New password:\"
send \"test\n\"
expect \"Retype new password:\"
send \"test\n\"
set timeout 3
expect \"Full Name []:\"
send \"\n\"
expect \"Room Number []:\"
send \"\n\"
expect \"Work Phone []:\"
send \"\n\"
expect \"Home Phone []:\"
send \"\n\"
expect \"Other []:\"
send \"\n\"
expect \"Is the information correct? \[Y\/n\]\"
send \"Y\n\"
interact
"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?