Aug 12

mysql — create database with mysql client command line option

mysql client is powerful with its shell like console.what about to create database or mysql user from command line option if you don’t want to log into mysql console.the mysql -e option can help with this.To create database , we can issue the following command:

mysql -u mysql_user -p mysql_pass -e “CREATE DATABASE db1;”

To create mysql user from command line option -e:

mysql -u mysql_user -p mysql_pass -e “GRANT ALL PRIVILEGES ON db1.* TO ‘db1_user’@'localhost’ IDENTIFIED BY ‘db1_pass’;”

with the help of -e option,we can batch create database and mysql user in our script.i think that’s why mysql client program support this option.

Apr 25

bash – shell script to update all user’s password

With the help of the function in my last post,i can update all user’s password with my shell script now.below is the bash shell script code.i have test the shell script with bash shell,it works ok.It will store all new password in a file named updated_ftp.txt of the current working directory.

#/bin/bash
#author george zheng <xinhaozheng@gmail.com>
#date 04 – 22 – 2012
function gen_pass()
{
LENGTH=12
array1=(w e r t y u p a s d f h j k z x c v b m Q W E R T Y U P A D F H J K L Z X C V B N M 2 3 4 7 8 ! @ $ % \# \& \* \= \- \+ \?)
MODNUM=${#array1[*]}
pwd_len=0
while (( “$pwd_len” < “$LENGTH” ))
do
index=$(($RANDOM%$MODNUM))
password=”${password}${array1[$index]}”
pwd_len=$pwd_len+1
done
}
$ftpfile=’updated_ftp.txt’
touch $ftpfile
alluser=`ls -ald /home/*/public_html|grep -v -E ‘/ipp?[0123456789]+(com|co)?/’| awk ‘{print $9}’|cut -d/ -f3`
for user in $alluser
do
password=”;
gen_pass
echo $password | passwd $user –stdin
echo “$user:$password” >> $ftpfile;
done
echo “All user password has been updated successfully!”

Note: you will need to adapt the ‘alluser’ line to meet you needs.like you may only need to change some special user which username start with a prefix or whatever else.And you can also get the user info directly from /etc/passwd file.

Apr 24

bash — get a random password from shell script

As i managed more than ten linux server,i need to update all user account per customer request. i need to generate rand password for each account.here comes the shell script to generate a random password

#/bin/bash
#author george zheng <xinhaozheng@gmail.com>
#date 04 – 22 – 2012

LENGTH=12
array1=(w e r t y u p a s d f h j k z x c v b m Q W E R T Y U P A D F H J K L Z X C V B N M 2 3 4 7 8 ! @ $ % \# \& \* \= \- \+ \?)
MODNUM=${#array1[*]}
pwd_len=0
while (( “$pwd_len” < “$LENGTH” ))
do
index=$(($RANDOM%$MODNUM))
password=”${password}${array1[$index]}”
pwd_len=$pwd_len+1
done
echo $password > passfile

or you may define a function,check the get_pass function below

#/bin/bash
#author george zheng
function gen_pass()
{
LENGTH=12
array1=(w e r t y u p a s d f h j k z x c v b m Q W E R T Y U P A D F H J K L Z X C V B N M 2 3 4 7 8 ! @ $ % \# \& \* \= \- \+ \?)
MODNUM=${#array1[*]}
pwd_len=0
while (( “$pwd_len” < “$LENGTH” ))
do
index=$(($RANDOM%$MODNUM))
password=”${password}${array1[$index]}”
pwd_len=$pwd_len+1
done
}
gen_pass
echo $password

Keep in mind that definition of the function  need to be put before the first call to the function.