#ファイルを準備
- 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);