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]
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 $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]
请输入你要创建用户的前缀[前缀必须是字母组成的]: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]
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 ~]
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]
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 $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]
[ -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]
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]
10.0.0.81 主机是存活的!
10.0.0.170 主机是存活的!
10.0.0.254 主机是存活的!
[root@shell /service/scripts/day07]
10.0.0.81 主机远程连接端口是开放的!
10.0.0.170 主机远程连接端口是开放的!
[root@shell /service/scripts/day07]
[ -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]
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]
Count=$(grep -c '.*' student.txt)
read -p "请输入你要循环这些名单多少次:" Num
if [[ ! $Num =~ ^[0-9]+$ ]];then
echo "你输入的次数不是正整数!请重新输入!"
exit
fi
for i in $(seq $Num)
do
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]
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]
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]
Mon Apr 27 14:36:29 CST 2020
[root@shell /service/scripts/day07]
-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]
[ -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]
数据库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]
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]
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]
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现