4
4

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 5 years have passed since last update.

スクリプトでLinuxの利用者登録

Last updated at Posted at 2016-02-29

#ファイルを準備

EXCELで下の内容のファイルを作成(ユーザデータ)

uxxx1:pxxx1::users::/home/uxxx1:/bin/bash
uxxx2:pxxx2::users::/home/uxxx2:/bin/bash
uxxx2:pxxx2::users::/home/uxxx2:/bin/bash
シェルコマンド(main)

#!/bin/sh
/usr/sbin/newusers $1
awk -F":" '{print $1":"$2 }' $1 |/usr/sbin/chpasswd
./skel2 $1
php newdb.php <$1
シェルコマンド(skel2)

#!/bin/sh
for rec in cat $1
do
IFS_OLD=IFS
IFS=":"
set -- $rec
user=$1
group=$4
IFS=IFS_OLD
echo $user
(cd /etc/skel/;tar cf - .)|(cd /home/$user;tar xf -)
chmod 755 /home/$user
chown -R $user /home/$user
chgrp -R $group /home/$user
touch /home/$user
done
PHPスクリプト(newdb.php)

<?php
$dsn = "mysql:host=127.0.0.1";
$user = 'root';
$pass = 'パスワード';
try {
$dbh = new PDO($dsn,$user,$pass);
} catch(PDOException $e) {
echo 'Connection faild: '.$e->getMessage();
}
$fp = fopen("php://stdin","r");
while($rec = fgets($fp)) {
list($user,$pass) = explode(":",$rec);
$sql = "CREATE USER {$user}@localhost IDENTIFIED BY '{$pass}'";
$dbh->query($sql);
$sql = "GRANT USAGE ON * . * TO '{$user}'@'localhost' IDENTIFIED BY '{$pass}' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0";
$dbh->query($sql);
$sql = "CREATE DATABASE IF NOT EXISTS {$user}" ;
$dbh->query($sql);
$sql = "GRANT ALL PRIVILEGES ON  {$user} . * TO  '{$user}'@'localhost'";
$dbh->query($sql);
print "$user $pass\n";
}
fclose($fp);
#実行 ./main ユーザデータ skel2はテンプレート(/etc/skel)をコピーします。 newdb.phpはMySQLのデータベースを個々の利用者に1つ作成します。
4
4
1

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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?