Creación de cuentas para lx
Posted by jgreyna on May 09, 2004
Hoy domingo, en lugar de salirme a divertir o hacer otra cosa, estube haciendo un pequeño scripcito para dar de alta las cuentas de los alumnos en lx, ahora decidí usar Perl para dicho script.
Aquí el código.
#!/usr/bin/perl
#############################################
# Name: makecounts
# Author: Jesús Reyna (jgreyna@lx.itnl.edu.mx)
# Description: This script make two files to create
# acounts in the server:
#
# cuentas.sh: Bash script to create acounts
# using useradd whit random password.
# alumnos-password.txt: Information to user and
# password generate
#
# The script need a file with the control numbrer
# and namde to alumn, this file is put whit
# parameter to script
#############################################
# Check the path to file exist, this path is a parameter
# of the script
if ($ARGV[0] eq "")
{
print "\nError de sintaxis..\n Ej. makeacounts \n\n";
exit;
}
# Vars whith the name of the files
$file_data = $ARGV[0];
$file_script = "cuentas.sh";
$file_password = "alumnos-password.txt";
# Open the files
open(FILE_DATA,"$file_data") || die { print " Error al abrir el archivo: $!\n"};
open (FILE_SCRIPT,"> $file_script");
open(FILE_PASSWORD,"> $file_password");
# Print the line to call the bash script
print FILE_SCRIPT "#!/bin/bash\n\n\n";
while()
{
# Use chomp to delete the end line
chomp;
# Value of the line of the file is a var $_
# Use split to get the control numbre and name
# the array @user contain the values (control number and name)
@user = split(/,/,$_);
# The var $username contain the acount to create and
# $name containt the name of alumn
$username = "al".$user[0];
$name = $user[1];
# Get the password from command line using makepasswd
# and chop to delete the end line
$pass = `makepasswd --crypt-md5 --chars 5`;
chop $pass;
# Use split to get the password and crypt password
@pwd = split(/ /,$pass);
# The var $password contanin the password in text plain and
# the var $pwdcrypt contain the crypt password
$password = $pwd[0];
$pwdcrypt = $pwd[1];
# Print the line on the script file and send message to standar output
print FILE_SCRIPT "useradd -c \"$name\" -p '$pwdcrypt' -s /bin/bash -m $username;\n";
print FILE_SCRIPT "echo \"Cuenta $username creada.\";\n\n\n";
# Print the line on the alumnos-password file
print FILE_PASSWORD "$name, $username, $password\n";
# Send message to standar output
print "Agregando $username\n"
}
# Make executable a script file
chmod 755, $file_script;
# Print the end message
print "\nTerminada la creación de cuentas\n\n";
Le puse los comentarios en inglés, pués para practicarlo, aunque la verdad no se si esten bien escrito, pero bueno, mas que todo los puse para mamonear
Saludos (y)



