Archive forProgramming

Create Users And Change Passwords With A Bash Script

First create a file which contains all the user name. Something like this:

nurealam
nayeem
mrahman
farid
rubi
sankar

Save the file as userlist.txt.
Now create the following bash file:

#!/bin/sh
for i in `more userlist.txt `
do
echo $i
adduser $i
doneSave the file and exit.

chmod 755 userlist.txt

Now run the file:

./userlist.txt

This will add all the users to the system. Now we have to change the passwords. Let’s say we want username123 as password. So for user nayeem the password will be nayeem123, rubi123 for user rubi and so on.

Create another bash file as follows:

#!/bin/sh
for i in `more userlist.txt `
do
echo $i
echo $i”123″ | passwd –-stdin “$i”
echo; echo “User $username’s password changed!”
done

Run the file.
All the passwords are changed.

Comments off

Livehelper




Comments off

/home and MySQL backup script

#!/bin/sh
# System + MySQL backup script
# ———————————————————————

### System Setup ###
DIRS=”/etc /home”
DAY=$(date +”%a”)
BACKUP=/dir.backup
EMAILID=yabba@dabba.com

### MySQL Setup ###
MUSER=”username”
MPASS=”password”
MHOST=”localhost”

### FTP server Setup ###
FTPU=”FTPusername”
FTPP=”FTPpassword”
FTPS=”IP.IP.IP.IP”

# Make that them there directory.
if [ ! -d $BACKUP ] ; then
mkdir $BACKUP
fi

### Start MySQL Backup ###
FILE=$BACKUP/drj_all_db.sql.$DAY.gz
mysqldump -u $MUSER -h $MHOST -p$MPASS –all-databases | gzip -9 > $FILE

### Dump backup using FTP ###
lftp -u “$FTPU”,”$FTPP” $FTPS< lcd $BACKUP
put $FILE
quit
EOF
# Comment this out. It will overwrite $? when run and not allow checking
# on the next if statement.
#echo "$?"
### Find out if ftp backup failed or not ###
T=/tmp/backup.fail
if [ "$?" != "0" ]; then
echo "Date: $(date)">$T
echo “Hostname: $(hostname)” >>$T
echo “Remote backup failed! Please investigate! Thank you!” >>$T
mail -s “Backup Failed: Ugg ” “$EMAILID” <$T
rm -f $T
fi

Comments off

If you need to make sure a script is run as root

If you need to make sure a script is run as root, add the following to the start of the script:

if [[ $UID -ne 0 ]]; then
echo “$0 must be run as root”
exit 1
fi

Comments

chown-fix using users from /etc/passwd

#!/bin/bash

users=`cat /etc/passwd`
for i in $users ; do
user=”"
dir=”"

user=`echo $i | cut -f1 -d: | awk ‘{print $1}’`
dir=`echo $i | cut -f6 -d: | awk ‘{print $1}’`

check=`expr substr “$dir” 0 14`

# if [[ $check == "/home/mailuser" && -d $dir ]] ; then
echo $user
chown -R $user /home/mailuser/$user
# fi
done

Comments

find tips…

[root@home ~]# find /etc -type f -exec grep -Hil sonny {} \;
/etc/shadow
/etc/group
/etc/passwd-
/etc/shadow-
/etc/gshadow
/etc/passwd

Comments off

Network Restart

This script is used to make onboard nic eth0 and addon gig card eth1

If allowed to boot w/o this; it boots PCI slots before onboard…

#!/usr/bin/perl

$KERNEL=`uname -r`;
chop($KERNEL);

system(”/etc/rc.d/init.d/network stop”);
system(”/sbin/rmmod e1000″);
system(”/sbin/rmmod tg3″);

$file1=”/lib/modules/$KERNEL/kernel/drivers/net/tg3.ko”;
$file2=”/lib/modules/$KERNEL/kernel/drivers/net/e1000/e1000.ko”;
system(”/sbin/insmod $file1″);
system(”/sbin/insmod $file2″);
system(”/etc/rc.d/init.d/network start”);

Comments off