• 用户管理脚本
• 倒计时
• 数据库备份
• 服务自动部署示例
• 自动登陆脚本
一.用户建立脚本
• 执行users_create.sh userlist passlist
• 建立userlist列表中的用户
• 设定userlist列表中的密码为passlist列表中的密码
• 当脚本后面跟的文件个数不足两时,报错
• 当文件行数不一致时报错
• 当文件不存在时报错
• 当用户存在时报错
看到此需求,先把要判断的写完,再建立用户
1 #!/bin/bash
2 if [ ! "$USER" = "root" ]
3 then
4 echo -e "\033[31mError:only root run $0 !!\033[0m"
5 exit
6 elif [ "$#" != "2" ] ##判断脚本后所输入的字符串个数是否为两个
7 then
8 echo -e "\033[31mPlease input userlist and passlist file!!\033[0m"
9 exit
10 elif [ ! -e "$1" ]
11 then
12 echo -e "\033[31m$1 is not exist!!\033[0m"
13 exit
14 elif [ ! -e "$2" ]
15 then
16 echo -e "\033[31m$2 is not exist!!\033[0m"
17 exit
18 else
19 User_Line=$(awk 'BEGIN{N=0}{N++}END{print N}' $1)
20 Pass_Line=$(awk 'BEGIN{N=0}{N++}END{print N}' $2)
21 [ "$User_Line" != "$Pass_Line" ]&&{
22 echo -e "\033[31m$1's line is different from $2!!\033[0m"
23 exit
24 }
25 fi
26
27 for LINE in $(seq 1 $User_Line) ##找到两个文件的相同点,以行数来取值,保证账号与密码对应
28 do
29 USERNAME=`sed -n ${LINE}p $1`
30 PASSWORD=`sed -n ${LINE}p $2`
31 id $USERNAME &> /dev/null && {
32 echo -e "\033[31m$USERNAME is exist!!\033[0m"
33 }||{
34 useradd $USERNAME
35 echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && {
36 echo -e "\033[32m$USERNAME is created\033[0m"
37 }
38 }
39 done
二.倒计时
1分10秒倒计时
1 #!/bin/bash
2 MIN=1
3 SEC=10
4 SEC=$[$MIN*60+$SEC] ##计算总秒数,先和在分,循环总的秒数
5 for ((;SEC>=0;SEC--))
6 do
7 ((min=$SEC/60)) ##取整计算显示分钟数
8 ((sec=$SEC%60)) ##取余计算秒数
9 echo -n "After $min:$sec is over " ##不换行,可以man echo
10 echo -ne "\r" ##回车,显示在同一位置
11 sleep 1
12 done
##也可以用判断的方式
1 #!/bin/bash
2 MIN=1
3 SEC=10
4 for ((;SEC>=0;SEC--))
5 do
6 [ "$SEC" = 0 ] && [ "$MIN" = 0 ]&& exit
7 [ "$SEC" = 0 ] && [ "$MIN" -ge 0 ]&&{
8 echo -n "After $MIN:${SEC}s is end "
9 echo -ne "\r"
10 sleep 1
11 ((MIN --))
12 SEC=59
13 }
14 echo -n "After $MIN:${SEC}s is end "
15 echo -ne "\r"
16 sleep 1
17 done
三.数据库备份
• 执行db_dump.sh westos(数据库密码)
• 脚本执行后会备份数据库中的所有库到/mnt/mysqldump目录中
• 备份文件名称为 “库名称.sql”当此文件存在时报错并询问动
作
输入“S”跳过备份,当输入“B"时备份“库名称.sql”文件为
“库名称_backup.sql”,当输入“O”时,覆盖源文件
1 #!/bin/bash
##3.写个函数,并询问动作
2 ACTION()
3 {
4 echo "$DBNAME.sql is exist!!"
5 echo "[o]verwrite | [s]kip | [b]acup"
6 read -p "please input action: " DB_ACTION
7 case $DB_ACTION in
8 S|s)
9 ;;
10 O|o)
11 mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql && echo $DBNAME .sql is dumped!!
12 ;;
13 B|b)
14 mv /mnt/mysqldump/$DBNAME.sql /mnt/mysqldump/${DBNAME}_backup.sql
15 mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql && echo $DBNAME .sql is dumped!!
16 ;;
17 *)
18 echo "wrong ACTION !!"
19 ACTION
20 esac
21 }
##1.先写判断,mariadb是否安装,密码是否输入,程序是否运行
22 rpm -q mariadb-server &>/dev/null
23 if [ ! "$?" = "0" ]
24 then
25 echo "mariadb is not installed"
26 else [ -z "$1" ] && {
27 echo "please input password followong $0"
28 exit
29 }||{
30 mysql -uroot -p$1 -e "SHOW DATABASES;" &> /dev/null ||{
31 systemctl status mariadb | grep running &> /dev/null
32 if [ "$?" = "0" ]
33 then
34 echo "Error:wrong password!!"
35 exit
36 else
37 echo "Error:mariadb is not runing!!"
38 fi
39 }
40 }
41 fi
##2.截取数据库,并判断文件夹是否存在,数据库是否备份
42 for DBNAME in `mysql -uroot -p$1 -NEe "SHOW DATABASES;" | grep -E "\*|schema $" -v`
43 do
44 if [ ! -e "/mnt/mysqldump" ]
45 then
46 mkdir /mnt/mysqldump
47 mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql && echo $DBNA ME.sql is dumped!!
48 elif [ -e "/mnt/mysqldump/$DBNAME.sql" ]
49 then
50 ACTION
51 else
52 mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql && echo $DBNA ME.sql is dumped!!
53 fi
54 done
四.服务自动部署示例
• 执行脚本mysqladmin.sh 8080
• 脚本执行后部署mysql php网页版,并设定apache的网络接口为8080
#!/bin/bash
##首先判断mariadb、httpd安装否
rpm -q mariadb-server &> /dev/null && {
rpm -q httpd &> /dev/null ||{
dnf install httpd -y &> /dev/null ||{
echo "can't install apache"
exit
}
}
}||{
dnf install mariadb-server -y &> /dev/null ||{
echo "can't install mariadb"
exit
}
systemctl enable --now mariadb
mysqladmin password westos && echo "your db's password is westos" ##给root设定数据库密码
}
##判断是否开启
systemctl start mariadb ||{
echo "mariadb is can't running"
exit
} && {
systemctl start httpd || {
echo "httpd is can't running"
exit
}
}
##安装php插件,更改apache端口,搭建php
dnf install php php-mysqlnd -y &> /dev/null || {
echo "can't install php and php-mysqlnd"
exit
}
sed '/^Listen/cListen 8080' -i /etc/httpd/conf/httpd.conf
systemctl restart httpd ||{
echo "can't change port to 8080 ,maybe your selinux is enforcing"
}&&{
cp /mnt/phpMyAdmin-3.4.0-all-languages.tar.bz2 /var/www/html
cd /var/www/html
tar jxf phpMyAdmin-3.4.0-all-languages.tar.bz2
mv phpMyAdmin-3.4.0-all-languages phpmyadmin
cp phpmyadmin/config.sample.inc.php phpmyadmin/config.inc.php
}
五.自动登陆脚本
• 执行auto_ssh.sh 172.25.254.2 dg hostname
172.25.254.2为ip
dg 为密码
hostname
执行脚本后自动登陆172.25.254.100并保持登陆
#!/bin/bash
SSH(){
/usr/bin/expect << EOF
spawn ssh root@$1 $3
expect {
"(yes/no)" { send "yes\r";exp_continue }
"password:" { send "$2\r"}
}
expect eof
EOF
}
ping -c1 -w1 $1 &> /dev/null && {
SSH $1 $2 $3 ##调用的函数,及输入的变量
}