1.用户建立脚本
执行 users_create.sh userlist passlist
建立 userlist 列表中的用户
设定 userlist 列表中的密码为 passlist 列表中的密码
当脚本后面跟的文件个数不足两时,报错
当文件行数不一致时报错
当文件不存在时报错
当用户存在时报错
#!/bin/bash
############test part##########
if
[ "$#" -ne "2" ]
then
echo "ERROR1: please input userlist and passlist follow script!!"
exit 1
elif
[ ! -e "$1" ]
then
echo "ERROR2: $1 is not exist!!"
exit 1
elif
[ ! -e "$2" ]
then
echo "ERROR3: $2 is not exist!!"
exit 1
elif
USERLIST_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
PASSLIST_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $2`
[ "$USERLIST_LINE" -ne "$PASSLIST_LINE" ]
then
echo "ERROR4: $1's line is different from $2's"
exit
fi
##########CREATE USER###############
for LINE_NUM in `seq 1 $USERLIST_LINE`
do
USERNAME=`sed -n ${LINE_NUM}p $1`
PASSWORD=`sed -n ${LINE_NUM}p $2`
useradd $USERNAME
[ "$?" -eq "0" ] &&{
echo $PASSWORD | passwd --stdin $USERNAME &>/dev/null && echo $USERNAME created!!
}
done
2.数据库备份
执行 db_dump.sh westos( 数据库密码 )
脚本执行后会备份数据库中的所有库到 /mnt/mysqldump 目录
中
备份文件名称为 “库名称 .sql” 当此文件存在时报错并询问动
作
输入“ S” 跳过备份,当输入“ B” 时备份“库名称 .sql” 文件
为“库名称 _backup.sql”, 当输入“ O” 时,覆盖源文件
#!/bin/bash
ACTION_CMD(){
read -p "
[S]kip [B]ackup [O]verwrite
please input action: " ACTION
ACTION=`echo $ACTION|tr 'A-Z' 'a-z'`
}
mkdir -p /mnt/mysqldump
DATABASE_MESSAGE=`mysql -uroot -pwestos -EN -e "show databases;"|grep -E "^\*|schema$" -v`
echo $DATABASE_MESSAGE > /mnt/myaqldump/$DATABASE_MESSAGE.sql
[ -e "$DATABASE_MESSAGE.sql" ]&&{
case $ACTION in
s)
;;
b)
cp /mnt/mysqldump/$DATABASE_MESSAGE.sql /mnt/mysqldump/$DATABASE_MESSAGE_backup.sql
;;
o)
cp /mnt/mysqldump/$DATABASE_MESSAGE.sql /mnt/mysqldump/$DATABASE_MESSAGE.sql
;;
exit)
echo bye
exit 0
;;
*)
echo "error: please input [S] [B] [O]"
ACTION_CMD $DATABASE_MESSAGE.sql
esac
}
3.批处理脚本
•
检测教室中开启的所有主机,并抓取所有主机的值机名称和 ip
的对应列表,把列表保存在 /mnt/ip_host.list 文件中
#!/bin/bash
Auto_Connect(){
/usr/bin/expect <<EOF
set timeout 10
spawn ssh root@172.25.254.$IP hostname
expect{
"yes/no" { send "yes\r";exp_continue }
"password" { send "westos\r"}
}
expect eof
EOF
}
for IP in {5..9}
do
ping -c1 -w1 172.25.254.$IP &> /dev/null &&{
Host_Name=`Auto_Connect|grep -E "authenticity|fingerprint|connecting|password|spawn|Warning" -v`
}
echo $Host_Name 172.25.254.$IP |sed 's/\r//g' >> /mnt/ip_host.list
done