shell脚本知识点7

Shell-day07

1. 昨日回顾


1. case语句的场景示例

2. 系统管理工具箱脚本

3. 实现跳板机的脚本 

4. 多级菜单脚本  

5. Shell循环语句  

6. for   while   until

7. For循环基本概述 语法  

8. For循环的取值方式  \'

9. C语言风格和Shell语言的循环区别

10. 通过文件进行创建用户和设置密码 

11. 批量创建用户脚本

2. 批量创建用户脚本


1. 必须是root超级管理员才能执行这个脚本

2. 设置24位随机密码,最后将用户和密码信息保存到一个文件中  User: xxx   Pass: xxxxx

3. 密码文件权限只有管理员可读,其他人没有任何权限


[root@shell /service/scripts/day07]# cat for-1.sh
#!/bin/bash
#判断用户是否为超级管理员root用户
if [ $USER != "root"  -o  $UID -ne  0  ];then
    echo "当前用户${USER}对此脚本${0}没有权限执行!"
    exit
fi
#提示用户输入创建用户的前缀
read -p "请输入你要创建用户的前缀[前缀必须是字母组成的]:" Qz
#判断用户输入的前缀是否为字母组成的
if [[ ! $Qz =~ ^[a-Z]+$ ]];then
    echo "你输入的不符合要求!前缀必须是由字母组成的!"
    exit
fi
#提示用户输入创建用户后缀,即数量
read -p "请输入你要创建用户的数量:" Num
#判断用户输入的数量是否为数字
if [[ ! $Num =~ ^[0-9]+$ ]];then
    echo "你输入的不符合要求!创建用户的数量必须为正整数!"
    exit
fi
#提示用户接下来要创建的用户列表
echo "接下来你要创建的用户为:${Qz}1..${Qz}${Num}"
read -p "你是否确定要进行创建这些用户[y/n]:" Confirm
#编写case语句
case $Confirm in
    y|Y|yes|Yes)
        echo "你选择了要进行创建以上用户!"
        for i in $(seq $Num)
        do
            #将用户的前缀和后缀组合在一起
            User=${Qz}${i}
            #判断用户是否存在
            id $User &>/dev/null
            if [ $? -eq 0 ];then
                echo "用户${User}已经存在!无需再次进行创建!"
            else
                Pass=$(mkpasswd  -l 24 -s 6 -d 6 -c 6 -C 6)
                useradd $User &>/dev/null && echo $Pass | passwd --stdin $User &>/dev/null
                if [ $? -eq 0 ];then
                    echo "用户${User}创建成功!密码设置成功!密码文件为:user_pass.txt"
                    echo -e "User: $User\tPass: $Pass" >> user_pass.txt
                else
                    echo "用户${User}创建失败!"
                fi
            fi
        done
        ;;
    n|N|No|no)
        echo "你选择不进行创建这些用户!脚本程序退出!"
        exit
        ;;
    *)
        echo "你输入不符合要求!请重新输入!"
        exit
esac
#给密码文件设置只有管理员可读权限
chmod 400  user_pass.txt  
[root@shell /service/scripts/day07]# sh for-1.sh 
请输入你要创建用户的前缀[前缀必须是字母组成的]:ccc
请输入你要创建用户的数量:5
接下来你要创建的用户为:ccc1..ccc5
你是否确定要进行创建这些用户[y/n]:y
你选择了要进行创建以上用户!
用户ccc1创建成功!密码设置成功!密码文件为:user_pass.txt
用户ccc2创建成功!密码设置成功!密码文件为:user_pass.txt
用户ccc3创建成功!密码设置成功!密码文件为:user_pass.txt
用户ccc4创建成功!密码设置成功!密码文件为:user_pass.txt
用户ccc5创建成功!密码设置成功!密码文件为:user_pass.txt
[root@shell /service/scripts/day07]# cat user_pass.txt
User: ccc1	Pass: 99W?~2aI66[}3cDIfxf?=eTJ
User: ccc2	Pass: !@080lHTuv-quG08j1O:;'YQ
User: ccc3	Pass: l&FL'99~7OAc5svueQ{6G9&&
User: ccc4	Pass: @0DlXR>^3se;jU5_WQa46i!9
User: ccc5	Pass: 0*GtwJY1<72So&>Z72~dVjj$

[root@shell ~]# su - www
Last login: Fri Apr 24 09:23:06 CST 2020 on pts/1
[www@shell ~]$ sh /service/scripts/day07/for-1.sh 
当前用户www对此脚本/service/scripts/day07/for-1.sh没有权限执行!


3. 批量删除用户脚本


[root@shell /service/scripts/day07]# cat for-2.sh
#!/bin/bash
#判断用户是否为超级管理员root用户
if [ $USER != "root"  -o  $UID -ne  0  ];then
    echo "当前用户${USER}对此脚本${0}没有权限执行!"
    exit
fi
#提示用户输入删除用户的前缀
read -p "请输入你要删除用户的前缀[前缀必须是字母组成的]:" Qz
#判断用户输入的前缀是否为字母组成的
if [[ ! $Qz =~ ^[a-Z]+$ ]];then
    echo "你输入的不符合要求!前缀必须是由字母组成的!"
    exit
fi
#提示用户输入删除用户后缀,即数量
read -p "请输入你要删除用户的数量:" Num
#判断用户输入的数量是否为数字
if [[ ! $Num =~ ^[0-9]+$ ]];then
    echo "你输入的不符合要求!删除用户的数量必须为正整数!"
    exit
fi
#提示用户接下来要删除哪些用户
echo "接下来要进行删除的用户为:${Qz}1..${Qz}${Num}"
read -p "你是否确认要删除这些用户[y|n]:" Confirm
#编写case语句
case $Confirm in
    y|Y|Yes|yes)
        echo "你选择了要进行删除这些用户!"
        for i in $(seq $Num) 
        do
            #将用户的前缀和后缀组合在一起
            User=${Qz}${i}
            #判断用户是否存在
            id $User &>/dev/null
            if [ $? -ne 0 ];then
                echo "用户${User}不存在!"
            else
                userdel -r  $User &>/dev/null
                if [ $? -eq 0 ];then
                    echo "用户${User}删除成功!"
                else
                    echo "用户${User}删除失败!"
                fi
            fi
        done
        ;;
    n|N|no|No)
        echo "你选择了不删除这些用户!你可以手动删除!"
        exit
        ;;
    *)
        echo "你输入的不符合要求!请重新输入!"
        exit
esac

4. 批量探测10.0.0.0/24网段主机


批量的进行探测10.0.0.0/24网段内的所有主机的存活状态  

存活的主机,在进行判断远程连接端口是否开放

1. 使用循环进行批量探测   254 1 ..254 

2. 怎么进行探测  ping  通则说明主机存活  不通说明主机不存活 

3. 存活的主机再进行探测远程连接端口是否开放 

4. 将探测结果进行返回


[root@shell /service/scripts/day07]# cat for-3.sh 
#!/bin/bash
#引用函数
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
>ip.log
>port.log
#进行批量探测
for i in $(seq 254)
do
    #批量执行循环体
    {
    Ip=10.0.0.$i
    #进行测试主机是否通畅
    ping -c1 -W1 $Ip  &>/dev/null
    if [ $? -eq 0 ];then
        action "IP地址${Ip}主机是存活的....." /bin/true
        echo "$Ip 主机是存活的!" >> ip.log
    fi
}&
sleep 0.05
done
wait  #等待 等之前的命令执行完成之后,再去执行等待后面的命令
echo "IP地址扫描完毕!"
echo "开始端口扫描...."
for j in $(awk '{print $1}' ip.log)
do
    {
    Status=$(nmap  -p22 $j | awk '/22/{print $2}')
    if [ $Status == "open" ];then
        action "IP地址${j}远程连接端口是开放的....." /bin/true
        echo "$j 主机远程连接端口是开放的!" >>port.log
    else
        action "IP地址${j}远程连接端口是关闭的....." /bin/false
    fi
}&
sleep 0.05
done
wait


[root@shell /service/scripts/day07]# sh for-3.sh
IP地址10.0.0.81主机是存活的.....                           [  OK  ]
IP地址10.0.0.170主机是存活的.....                          [  OK  ]
IP地址10.0.0.254主机是存活的.....                          [  OK  ]
IP地址扫描完毕!
开始端口扫描....
IP地址10.0.0.81远程连接端口是开放的.....                   [  OK  ]
IP地址10.0.0.170远程连接端口是开放的.....                  [  OK  ]
IP地址10.0.0.254远程连接端口是关闭的.....                  [FAILED]
[root@shell /service/scripts/day07]# cat ip.log 
10.0.0.81 主机是存活的!
10.0.0.170 主机是存活的!
10.0.0.254 主机是存活的!
[root@shell /service/scripts/day07]# cat port.log 
10.0.0.81 主机远程连接端口是开放的!
10.0.0.170 主机远程连接端口是开放的!



[root@shell /service/scripts/day07]# cat for-3.sh
#!/bin/bash
#引用函数
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
>ip.log
>port.log
#进行批量探测
for i in $(seq 254)
do
    #批量执行循环体
    {
    Ip=10.0.0.$i
    #进行测试主机是否通畅
    ping -c1 -W1 $Ip  &>/dev/null
    if [ $? -eq 0 ];then
        action "IP地址${Ip}主机是存活的....." /bin/true
        echo "$Ip 主机是存活的!" >> ip.log
        Status=$(nmap  -p22 $Ip | awk '/22/{print $2}')
        if [ $Status == "open" ];then
            action "IP地址${Ip}远程连接端口是开放的....." /bin/true
            echo "$j 主机远程连接端口是开放的!" >>port.log
        else
            action "IP地址${Ip}远程连接端口是关闭的....." /bin/false
        fi
    fi
}&
sleep 0.05
done
wait  #等待 等之前的命令执行完成之后,再去执行等待后面的命令
[root@shell /service/scripts/day07]# sh for-3.sh
IP地址10.0.0.81主机是存活的.....                           [  OK  ]
IP地址10.0.0.81远程连接端口是开放的.....                   [  OK  ]
IP地址10.0.0.170主机是存活的.....                          [  OK  ]
IP地址10.0.0.170远程连接端口是开放的.....                  [  OK  ]
IP地址10.0.0.254主机是存活的.....                          [  OK  ]
IP地址10.0.0.254远程连接端口是关闭的.....                  [FAILED]


5. 随机点名脚本


1. 要有名单  

2. 循环打印这些名单,打印多少次

3. 要有循环的数字  随机的数

4. 这个随机数不能大于人员的总数  也不能等于0 

5. 显示出打印的这些名单

6. 打印出最后一个出现人员名单  

[root@shell /service/scripts/day07]# cat for-4.sh
#!/bin/bash
Count=$(grep -c '.*' student.txt)
read -p "请输入你要循环这些名单多少次:" Num
#判断输出的次数是否为正整数
if [[ ! $Num =~ ^[0-9]+$ ]];then
    echo "你输入的次数不是正整数!请重新输入!"
    exit
fi
for i in $(seq $Num)
do
    #生成一个随机数,不能大于文件的总行数和0
    Ran=$(( $RANDOM % $Count + 1 ))
    #循环打印人员名单
    sed -n  "${Ran}p"  student.txt
    sleep 0.5
done
#将最后一次循环取值到人名赋值给变量
Name=$(sed -n  "${Ran}p"  student.txt)
#将最后一个人员进行输出
echo -e "天玄子:\033[32m$Name\033[0m"

6. 批量创建不同日期的文件


使用for循环创建10个不同日期的文件

18 19 20 .. 27   

文件名为以当天日期为名  2020-04-27.txt    文件的创建时间和名称上面的时间是一致的


[root@shell /service/scripts/day07]# cat for-5.sh
#!/bin/bash
for i in {18..27}
do
    date -s 2020/04/$i  &>/dev/null && touch  $(date +%F).txt
    if [ $? -eq 0 ];then
        echo "$(date +%F)-.txt创建成功!"
    else
        echo "$(date +%F)-.txt创建失败!"
    fi
done
ntpdate ntp.aliyun.com  &>/dev/null
[root@shell /service/scripts/day07]# sh for-5.sh
2020-04-18-.txt创建成功!
2020-04-19-.txt创建成功!
2020-04-20-.txt创建成功!
2020-04-21-.txt创建成功!
2020-04-22-.txt创建成功!
2020-04-23-.txt创建成功!
2020-04-24-.txt创建成功!
2020-04-25-.txt创建成功!
2020-04-26-.txt创建成功!
2020-04-27-.txt创建成功!
[root@shell /service/scripts/day07]# date
Mon Apr 27 14:36:29 CST 2020
[root@shell /service/scripts/day07]# ll *.txt
-rw-r--r-- 1 root root   0 2020-04-18 00:00 2020-04-18.txt
-rw-r--r-- 1 root root   0 2020-04-19 00:00 2020-04-19.txt
-rw-r--r-- 1 root root   0 2020-04-20 00:00 2020-04-20.txt
-rw-r--r-- 1 root root   0 2020-04-21 00:00 2020-04-21.txt
-rw-r--r-- 1 root root   0 2020-04-22 00:00 2020-04-22.txt
-rw-r--r-- 1 root root   0 2020-04-23 00:00 2020-04-23.txt
-rw-r--r-- 1 root root   0 2020-04-24 00:00 2020-04-24.txt
-rw-r--r-- 1 root root   0 2020-04-25 00:00 2020-04-25.txt
-rw-r--r-- 1 root root   0 2020-04-26 00:00 2020-04-26.txt
-rw-r--r-- 1 root root   0 2020-04-27 00:00 2020-04-27.txt


7. 数据库的分库分表备份


1. 数据库如何备份

mysqldump -uroot -pqls.123 --single-transaction -R  -B world >world.sql

2. 如何备份数据库表

mysqldump -uroot -pqls.123 --single-transaction -R  wordpress  wp_users >wordpress_wp_users.sql

3. 备份到哪里  

/data/backup/datebases  每个库的目录  

4. 备份周期  定时任务  

5. 不能手动的输入数据库名,如何获取到所有的数据库名称   表的名称  

mysql -uroot -pqls.123  -e "show databases;" | sed 1d | grep -v '.*_schema'

mysql -uroot -pqls.123  -e "use world; show tables;"  | sed 1d


[root@shell /service/scripts/day07]# cat for-6.sh
#!/bin/bash
#引用函数
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#定义变量
Date=$(date +%F)
Db_User=root
Db_Pass=qls.123
#编写循环
for Db_Name in $(mysql -u$Db_User -p$Db_Pass  -e "show databases;" | sed 1d | grep -v '.*_schema')
do
    #准备备份目录
    Db_Dir=/data/backup/$Db_Name
    [ -d $Db_Dir ] || mkdir -p $Db_Dir
    #备份数据库
    mysqldump -u$Db_User -p$Db_Pass --single-transaction -R -B $Db_Name  >$Db_Dir/${Date}_${Db_Name}.sql 
    if [ $? -eq 0 ];then
        action "数据库${Db_Name}备份成功...." /bin/true
    else
        action "数据库${Db_Name}备份失败...." /bin/false
    fi
    #备份数据库中的表
    for Table_Name in $(mysql -u$Db_User -p$Db_Pass  -e "use $Db_Name;show tables;" | sed 1d)
    do
        #备份表
        mysqldump -u$Db_User -p$Db_Pass --single-transaction -R $Db_Name $Table_Name >$Db_Dir/${Date}_${Db_Name}_${Table_Name}.sql
        if [ $? -eq 0 ];then
            action "数据库${Db_Name}中的${Table_Name}表备份成功...." /bin/true
        else
            action "数据库${Db_Name}中的${Table_Name}表备份失败...." /bin/false
        fi
    done
done
[root@shell /service/scripts/day07]# sh for-6.sh
数据库mysql备份成功....                                    [  OK  ]
数据库mysql中的columns_priv表备份成功....                  [  OK  ]
数据库mysql中的db表备份成功....                            [  OK  ]
数据库mysql中的event表备份成功....                         [  OK  ]
数据库mysql中的func表备份成功....                          [  OK  ]
数据库mysql中的general_log表备份成功....                   [  OK  ]
数据库mysql中的help_category表备份成功....                 [  OK  ]
数据库mysql中的help_keyword表备份成功....                  [  OK  ]
数据库mysql中的help_relation表备份成功....                 [  OK  ]
数据库mysql中的help_topic表备份成功....                    [  OK  ]
数据库mysql中的host表备份成功....                          [  OK  ]
数据库mysql中的ndb_binlog_index表备份成功....              [  OK  ]
数据库mysql中的plugin表备份成功....                        [  OK  ]
数据库mysql中的proc表备份成功....                          [  OK  ]
数据库mysql中的procs_priv表备份成功....                    [  OK  ]
数据库mysql中的proxies_priv表备份成功....                  [  OK  ]
数据库mysql中的servers表备份成功....                       [  OK  ]
数据库mysql中的slow_log表备份成功....                      [  OK  ]
数据库mysql中的tables_priv表备份成功....                   [  OK  ]
数据库mysql中的time_zone表备份成功....                     [  OK  ]
数据库mysql中的time_zone_leap_second表备份成功....         [  OK  ]
数据库mysql中的time_zone_name表备份成功....                [  OK  ]
数据库mysql中的time_zone_transition表备份成功....          [  OK  ]
数据库mysql中的time_zone_transition_type表备份成功....     [  OK  ]
数据库mysql中的user表备份成功....                          [  OK  ]
数据库test备份成功....                                     [  OK  ]
数据库wordpress备份成功....                                [  OK  ]
数据库wordpress中的wp_commentmeta表备份成功....            [  OK  ]
数据库wordpress中的wp_comments表备份成功....               [  OK  ]
数据库wordpress中的wp_links表备份成功....                  [  OK  ]
数据库wordpress中的wp_options表备份成功....                [  OK  ]
数据库wordpress中的wp_postmeta表备份成功....               [  OK  ]
数据库wordpress中的wp_posts表备份成功....                  [  OK  ]
数据库wordpress中的wp_term_relationships表备份成功....     [  OK  ]
数据库wordpress中的wp_term_taxonomy表备份成功....          [  OK  ]
数据库wordpress中的wp_termmeta表备份成功....               [  OK  ]
数据库wordpress中的wp_terms表备份成功....                  [  OK  ]
数据库wordpress中的wp_usermeta表备份成功....               [  OK  ]
数据库wordpress中的wp_users表备份成功....                  [  OK  ]
数据库world备份成功....                                    [  OK  ]
数据库world中的city表备份成功....                          [  OK  ]
数据库world中的country表备份成功....                       [  OK  ]
数据库world中的countrylanguage表备份成功....               [  OK  ]
[root@shell /service/scripts/day07]# ll /data/backup/
total 8
drwxr-xr-x 2 root root 4096 2020-04-27 15:37 mysql
drwxr-xr-x 2 root root   33 2020-04-27 15:37 test
drwxr-xr-x 2 root root 4096 2020-04-27 15:37 wordpress
drwxr-xr-x 2 root root  147 2020-04-27 15:37 world
[root@shell /service/scripts/day07]# ll /data/backup/mysql/
total 1076
-rw-r--r-- 1 root root   2422 2020-04-27 15:37 2020-04-27_mysql_columns_priv.sql
-rw-r--r-- 1 root root   3636 2020-04-27 15:37 2020-04-27_mysql_db.sql
-rw-r--r-- 1 root root   3709 2020-04-27 15:37 2020-04-27_mysql_event.sql
-rw-r--r-- 1 root root   2071 2020-04-27 15:37 2020-04-27_mysql_func.sql
-rw-r--r-- 1 root root   2097 2020-04-27 15:37 2020-04-27_mysql_general_log.sql
-rw-r--r-- 1 root root   3297 2020-04-27 15:37 2020-04-27_mysql_help_category.sql
-rw-r--r-- 1 root root   9648 2020-04-27 15:37 2020-04-27_mysql_help_keyword.sql
-rw-r--r-- 1 root root  11879 2020-04-27 15:37 2020-04-27_mysql_help_relation.sql
-rw-r--r-- 1 root root 467210 2020-04-27 15:37 2020-04-27_mysql_help_topic.sql
-rw-r--r-- 1 root root   3311 2020-04-27 15:37 2020-04-27_mysql_host.sql
-rw-r--r-- 1 root root   2174 2020-04-27 15:37 2020-04-27_mysql_ndb_binlog_index.sql
-rw-r--r-- 1 root root   1923 2020-04-27 15:37 2020-04-27_mysql_plugin.sql
-rw-r--r-- 1 root root   2508 2020-04-27 15:37 2020-04-27_mysql_procs_priv.sql
-rw-r--r-- 1 root root   3597 2020-04-27 15:37 2020-04-27_mysql_proc.sql
-rw-r--r-- 1 root root   2547 2020-04-27 15:37 2020-04-27_mysql_proxies_priv.sql
-rw-r--r-- 1 root root   2237 2020-04-27 15:37 2020-04-27_mysql_servers.sql
-rw-r--r-- 1 root root   2237 2020-04-27 15:37 2020-04-27_mysql_slow_log.sql
-rw-r--r-- 1 root root 514853 2020-04-27 15:37 2020-04-27_mysql.sql
-rw-r--r-- 1 root root   2610 2020-04-27 15:37 2020-04-27_mysql_tables_priv.sql
-rw-r--r-- 1 root root   2056 2020-04-27 15:37 2020-04-27_mysql_time_zone_leap_second.sql
-rw-r--r-- 1 root root   1970 2020-04-27 15:37 2020-04-27_mysql_time_zone_name.sql
-rw-r--r-- 1 root root   1982 2020-04-27 15:37 2020-04-27_mysql_time_zone.sql
-rw-r--r-- 1 root root   2107 2020-04-27 15:37 2020-04-27_mysql_time_zone_transition.sql
-rw-r--r-- 1 root root   2249 2020-04-27 15:37 2020-04-27_mysql_time_zone_transition_type.sql
-rw-r--r-- 1 root root   5750 2020-04-27 15:37 2020-04-27_mysql_user.sql
[root@shell /service/scripts/day07]# ll /data/backup/wordpress/
total 940
-rw-r--r-- 1 root root 462582 2020-04-27 15:37 2020-04-27_wordpress.sql
-rw-r--r-- 1 root root   2133 2020-04-27 15:37 2020-04-27_wordpress_wp_commentmeta.sql
-rw-r--r-- 1 root root   3353 2020-04-27 15:37 2020-04-27_wordpress_wp_comments.sql
-rw-r--r-- 1 root root   2535 2020-04-27 15:37 2020-04-27_wordpress_wp_links.sql
-rw-r--r-- 1 root root  19451 2020-04-27 15:37 2020-04-27_wordpress_wp_options.sql
-rw-r--r-- 1 root root  38303 2020-04-27 15:37 2020-04-27_wordpress_wp_postmeta.sql
-rw-r--r-- 1 root root 394442 2020-04-27 15:37 2020-04-27_wordpress_wp_posts.sql
-rw-r--r-- 1 root root   2103 2020-04-27 15:37 2020-04-27_wordpress_wp_termmeta.sql
-rw-r--r-- 1 root root   2260 2020-04-27 15:37 2020-04-27_wordpress_wp_term_relationships.sql
-rw-r--r-- 1 root root   2252 2020-04-27 15:37 2020-04-27_wordpress_wp_terms.sql
-rw-r--r-- 1 root root   2418 2020-04-27 15:37 2020-04-27_wordpress_wp_term_taxonomy.sql
-rw-r--r-- 1 root root   4795 2020-04-27 15:37 2020-04-27_wordpress_wp_usermeta.sql
-rw-r--r-- 1 root root   2883 2020-04-27 15:37 2020-04-27_wordpress_wp_users.sql


posted @   zbzSH  阅读(33)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示