VCE Configuration Scripts for Linux SQL Server Reservation

  Henry E. Schaffer
Professor Emeritus of Genetics and Biomathematics
Coordinator of Special IT Projects & Faculty Collaboration/OIT

NC State University

  Start with a server reservation (this has been tested with Ubuntu 18.04 with LAMP installed - specifically "Ubuntu 18.04 LAMP hes") and copy and paste each portion into a file in your (V or I) directory - using the the name given here. Be sure you don't inadvertently split lines. All the lines are less than 80 characters, but some text editors may split/wrap at 72 characters. (For vim users, automatic indentation can mess up the indentation of these scripts. This can be overcome in a couple of ways. One is to use :set paste )
Make the Perl scripts executable if you wish to directly run them.

Configure Linux user directories for use with MySQL

This script reads the user-id-list file (one userid/line) and is invoked by:

$ ./subdir.pl < user-id-list  
[Note: this is done after Linux user accounts have been set up via the VCL User Group.]

                        subdir.pl

#!/usr/bin/perl -w
  use strict;
# This script has three functions - 
# a) change home directory permissions to 711
#    add a sub-directory mysql_space in /home/id/ with permissions 770
#    and owner:group set as user-id:mysql
#    so mysql can use it for infile/outfile from within mysql
# b) add a sub-directory named   private
#    Permissions are  700   and  owner:group are  id:root
# c) remove the examples.desktop file as it isn't needed
# If only one function is needed, comment out the other ones
# account ids are in the input file - one id per line

my ($id);
while (<>) {
  $id = $_; chomp $id;

# This section carries out (a) from above
  system("sudo chmod 711 /home/$id");
  system("sudo mkdir -m 771 /home/$id/mysql_space");
  system("sudo chown $id:mysql /home/$id/mysql_space");

# This section carries out (b) from above
  $id = $_; chomp $id;
  system("sudo mkdir -m 700  '/home/'$id'/private'");
  system("sudo chown $id':'root  '/home/'$id'/private'"); 

# The section carries out (c) from above
  system("sudo rm /home/$id/examples.desktop");
}

Output sql commands to set up/configure user accounts in MySQL

This script reads the user-id-list file (one userid/line) and is invoked by:

$ ./sql-acc.pl < user-id-list > sql-account-command-file
                        sql-acc.pl
#!/usr/bin/perl -w
  use strict;
# Reads in a list of userids, one per line, generates a password for each
# Then outputs an sql statement to set up an account for that user
# Those statements are put into a file, and then can be executed in mysql

my ($length, $alphac, @letter, $pass, $i);
$length = 5; # length of password - could be any length
srand; # initialize the rng
$alphac = # a string of comma separated characters
"a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z" .  #omit "l"
"A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";   #omit "O"
# whatever characters desired - here just uc/lc alphabet
@letter = split(/,/, $alphac);
while (<>) {
  chomp ($_);
  $pass = ""; # Now generate random password
  for ($i = 1; $i <= $length; $i++) { 
    $pass = $pass . $letter[rand @letter];
  }

  print "CREATE USER '$_'\@'localhost' IDENTIFIED by '$pass';\n";

# Each user password will be at the end of the line - so you can
# copy and give to each user - or better use the list-acc.pl script.
}
print "flush privileges; \n"; # to activate the above commands


After the above script is run to produce the sql-account-command-file,
That file needs executed as root from within mysql (using database
mysql), which can be done via  

mysql> source fully-qualified-file-name;
"fully-qualified-file-name" means something like
/home/johndoe/sql-account-command-file
(where you use your account rather than "johndoe" :-)

Output sql commands to grant privilege to MySQL user accounts

This script reads the user-id-list file (one userid/line) and is invoked by:
$ ./sql-grant.pl < user-id-list > sql-grant-command-file
                        sql-grant.pl
#!/usr/bin/perl
# Reads in a list of userids, one per line, 
# Then outputs an sql statement to grant privileges for that user
# With those statements in a file, it can be executed in mysql

while (<>) {
  chomp ($_);

  print "GRANT ALL ON `$_\_%` . * TO '$_'\@'localhost';\n";
} # Note two "back ticks" are used in the above command.
print "flush privileges; \n"; # to activate the above commands
This needs to be followed by executing the sql-grant-command-file as root from within mysql, using database mysql. It can be done via
mysql> source fully-qualified-file-name

Input sql-account-command-file and output .csv list of ids/passwords to give to students

This script makes it easier to distribute the mysql passwords to users. It reads the user-id-list file (one userid/line) and is invoked by:
$ ./list-acc.pl < sql-account-command-file > id-pw-file
                        list-acc.pl
#!/usr/bin/perl
# This script will input the file of account making commands from the current
# directory and produce a .csv file of ids with their associated passwords
# call by ./list-acc.pl < sql-account-command-file > id-pw-file
# Assumes *exact* format of sql-account-command-file - exact spacing
# Assumes ids and passwords are alphabetical characters only

while (<>) {
  unless ( /flush privileges/) { # skip last line
  /.{13}([a-zA-Z]+).{29}([a-zA-Z]+)/;
  print "$1,$2\n";
  }
}

Input id-file and put a given file in each directory

This script makes it easy to put copies of a file (e.g. an assignment) into the directory of each user. The name of the file to be distributed must be entered into the script. It reads the user-id-list file (one userid/line) and is invoked by:
$ ./put-file.pl < user-id-list
                        put-file.pl
#!/usr/bin/perl
# This script will input a list of ids from the current directory
# and copy a given file into all of them.
# You must put the file name into this script in quotes
# E.g. $file_name="assignment1";

$file_name=""; # insert the file name inside these quotes

while (<>) {
  $id = $_; chomp $id;
  system("sudo cp $file_name /home/$id/.");
# Probaby should change the ownership 
# system("chown $id /home/$id/$file_name");   
}

Copyright 2016, 2021 by Henry E. Schaffer     Comments and suggestions are welcome, and should go to hes@ncsu.edu
Last modified 8/19/2021
Disclaimer - Information is provided for your use. No endorsement is implied.