yano3nora
7/2/2017 - 11:44 AM

[linux: expect] バッチとかで対話自動化するやつ。 #linux

[linux: expect] バッチとかで対話自動化するやつ。 #linux

#!/bin/sh

PW="password"

DB="app_db"
DBUSER="app"
DBPWD="password2"

expect -c "
  set timeout 100  # 100 秒間まってやる!なので重い処理のときは -1 とかにして永遠に後続処理を待ってもらう必要あり
  spawn /bin/sh -c \"sudo mysql_secure_installation\"
  expect \"Enter current password for root (enter for none):\"
  send \"\n\"
  expect \"Set root password?\"
  send \"y\n\"
  expect \"New password:\"
  send \"${PW}\n\"
  expect \"Re-enter new password:\"
  send \"${PW}\n\"
  expect \"Remove anonymous users?\"
  send \"y\n\"
  expect \"Disallow root login remotely?\"
  send \"y\n\"
  expect \"Remove test database and access to it?\"
  send \"y\n\"
  expect \"Reload privilege tables now?\"
  send \"y\n\"
  expect eof exit 0
"

mysql -u root --password=${PW} <<EOF
SET storage_engine=INNODB;
CREATE DATABASE IF NOT EXISTS ${DB} DEFAULT CHARACTER SET utf8;
GRANT ALL PRIVILEGES ON ${DB}.* TO '${DBUSER}'@'localhost' IDENTIFIED BY '${DBPWD}';
SET PASSWORD FOR '${DBUSER}'@'localhost' = PASSWORD('${DBPWD}');
FLUSH PRIVILEGES;
USE ${DB};
# SOURCE /var/www/.migration/db_migration.sql;
EOF