这28个值得收藏的shell脚本能让你每天摸鱼近7个小时!

请添加图片描述

前言:

在日常工作中,但凡你要跟服务器打交道,一定离不开的神器便是shell脚本,shell脚本可以极大的提高工程师的工作效率,避免一些认为因素导致的手误。那么今天圈圈就给大家分享28个shell脚本,希望对大家有帮助,脚本比较多比较长,一时间记不住可以先收藏,用到的时候及时拿出来比对一下即可!

1. 轮询检测Apache状态并启用钉钉报警

1#!/bin/bash 2 3shell_user="root" 4shell_domain="apache" 5 6shell_list="/root/ip_list" 7shell_row=`cat $shell_list |wc -l` 8 9 10function trans_text(){ 11text=$1 12 13curl 'https://oapi.dingtalk.com/robot/send?access_token=b4fcf5862088a1bc7f2bf66a' -H'Content-Type: application/json' -d'{ #指定钉钉机器人hook地址 14 "msgtype": "text", 15 "text": { 16 "content": "'"$text"'" 17 }, 18}' 19} 20 21 22 23function apache_check_80(){ 24 ip=$1 25 URL="http://$ip/index.html" 26 HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` 27 28 if [ $HTTP_CODE != 200 ] 29 then 30 trans_text " 31 ================================================================= 32 \n $ip Apache 服务器状态异常,网页返回码: '"$HTTP_CODE"' 请及时处理 ! \n 33 ================================================================= \n" 34 fi 35} 36 37while true 38do 39 40shell_list="/root/ip_list" 41shell_row=`cat $shell_list |wc -l` 42 for temp in `seq 1 $shell_row` 43 do 44 Ip_Addr=`cat $shell_list |head -n $temp |tail -n 1` 45 apache_check_80 $Ip_Addr 46 done 47 48 sleep 10 49done

2. 一台监控主机,一台被监控主机。被监控主机分区使用率大于80%,就发告警邮件。放到crontab里面,每10分钟执行一次

1#!/bin/bash 2 3FSMAX="80" 4remote_user='root' 5remote_ip=(IP地址列表) 6ip_num='0' 7 8while [ "$ip_num" -le "$(expr ${#remote_ip[@]} -l)"] 9do 10read_num='1' 11 ssh "$remote_user"@"${remote_ip[$ip_num]}" df -h > /tmp/diskcheck_tmp 12 grep '^/dev/*' /tmp/diskcheck_tmp | awk '{print $5}'|sed 's/\%//g' > /tmp/diskcheck_num_tmp 13 14 while [ "$read_num" -le $(wc -l < /tmp/diskcheck_num_tmp) ] 15 do 16 size=$(sed -n "$read_num" 'p' /tmp/diskcheck_num_tmp) 17 if [ "size" -gt "$FSMAX" ] 18 then 19 $(grep '^/dev/*' /tmp/diskcheck_tmp |sed -n $read_num'p' > /tmp/disk_check_mail) 20 $(echo ${remote_ip[$ip_num]}) >> /tmp/disk_check_mail) 21 $(mail -s "diskcheck_alert" admin < /tmp/disk_check_mail) 22 fi 23 24 read_num=$(expr $read_num + 1) 25 done 26 27 ip_num=$(expr $ip_num + 1) 28done

3.监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告

1#!/bin/bash 2#monitor available disk space 3#提取本服务器的IP地址信息 4IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` 5SPACE=` df -hP | awk '{print int($5)}'` 6if [ $SPACE -ge 90 ] 7then 8 echo "$IP 服务器 磁盘空间 使用率已经超过90%,请及时处理。"|mail -s "$IP 服务器硬盘告警, 9 公众号:Geek安全" fty89@163.com 10fi

4. 自动ftp上传

1#! /bin/bash 2 3ftp -n << END_FTP 4open 192.168.1.22 5user test testing //用户名test 密码:testing 6binary 7prompt off //关闭提示 8mput files //上传files文件 9close 10bye 11END_FTP

5.mysqlbak.sh备份数据库目录脚本

1#!/bin/bash 2 3DAY=`date +%Y%m%d` 4SIZE=`du -sh /var/lib/mysql` 5echo "Date: $DAY" >> /tmp/dbinfo.txt 6echo "Data Size: $SIZE" >> /tmp/dbinfo.txt 7cd /opt/dbbak &> /dev/null || mkdir /opt/dbbak 8tar zcf /opt/dbbak/mysqlbak-${DAY}.tar.gz /var/lib/mysql /tmp/dbinfo.txt &> /dev/null 9rm -f /tmp/dbinfo.txt 10 11crontab-e 1255 23 */3 * * /opt/dbbak/dbbak.sh

在这里插入图片描述

6.打印彩虹

1declare -a ary 2 3 4for i in `seq 40 49` 5do 6 7 ary[$i]=" " 8 echo -en "\e[$i;5m ${ary[@]}\e[;0m" 9 10done 11 12 13declare -a ary 14for s in `seq 1 10000` 15do 16 for i in `seq 40 49` 17 do 18 ary[$i]=" " 19 echo -en "\e[$i;5m ${ary[@]}\e[;0m" 20 done 21done

7.打印菱形

1#!/bin/bash 2 3for (( i = 1; i < 12; i++)) 4do 5 if [[ $i -le 6 ]] 6 then 7 for ((j = $((12-i)); j > i; j--)) 8 do 9 echo -n " " 10 done 11 12 for ((m = 1; m <= $((2*i-1)); m++)) 13 do 14 echo -n "* " 15 done 16 echo "" 17#***************************************************************************** 18 elif [[ $i -gt 6 ]] 19 then 20 n=$((12-i)) 21 for ((j = $((12-n)); j > n; j--)) 22 do 23 echo -n " " 24 done 25 26 for ((m = 1; m <= $((2*n-1)); m++)) 27 do 28 echo -n "* " 29 done 30 echo "" 31 fi 32 33done

8.expect实现远程登陆自动交互

1#!/usr/bin/expect -f 2 3set ipaddress [lindex $argv 0] 4 5set passwd [lindex $argv 1] 6 7set timeout 30 8 9spawn ssh-copy-id root@$ipaddress 10 11expect { 12 13"yes/no" { send "yes\r";exp_continue } 14 15"password:" { send "$passwd\r" } 16 17} 18 19 20 21#expect "*from*" 22 23#send "mkdir -p ./tmp/testfile\r" 24 25#send "exit\r" 26 27#expect "#" #i# 命令运行完, 你要期待一个结果, 结果就是返回shell提示符了(是# 或者$)

9.http心跳检测

1#!/bin/bash 2 3 4 5function MyInstall 6{ 7 if ! rpm -qa |grep -q "^$1" 8 then 9 10 yum install $1 11 if [ $? -eq 0 ] 12 then 13 echo -e "$i install is ok\n" 14 else 15 echo -e "$1 install no\n" 16 fi 17 else 18 echo -e "yi an zhuang ! \n" 19 fi 20} 21 22 23for ins in mysql php httpd 24do 25 MyInstall $ins 26done

12.shell实现插入排序

1#!/bin/bash 2 3 4declare -a array 5 6 7for i in `seq 1 10` 8do 9 array[$i]=$RANDOM 10 11done 12 13echo -e "Array_1: ${array[@]}" 14 15 16 17for (( x=1;x<=9;x++ )) 18do 19 for(( y=1;y<=9;y++ )) 20 do 21 if [ ${array[$y]} -gt ${array[$y+1]} ] 22 then 23 temp=${array[$y]} 24 array[$y]=${array[$y+1]} 25 array[$y+1]=$temp 26 fi 27 28 done 29 30done 31 32 33echo -e "Array_2: ${array[@]}"

13.bash实现动态进度条

1#!/bin/bash 2i=0 3bar='' 4index=0 5arr=( "|" "/" "-" "\\" ) 6 7while [ $i -le 100 ] 8do 9 let index=index%4 10 printf "[%-100s][%d%%][\e[43;46;1m%c\e[0m]\r" "$bar" "$i" "${arr[$index]}" 11 let i++ 12 let index++ 13 usleep 30000 14 bar+='#' 15 clear 16done 17 18printf "\n"

14. 根据文件内容创建账号

1#!/bin/bash 2 3 4for Uname in `cat /root/useradd.txt |gawk '{print $1}'` 5do 6 7 id $Uname &> /dev/null 8 if [ $? -eq 0 ] 9 then 10 echo -e "这个账号已存在!来源:微信公众号【网络技术干货圈】" 11 continue 12 fi 13 for Upasswd in `cat /root/useradd.txt |gawk '{print $2}'` 14 do 15 useradd $Uname &> /dev/null 16 echo "$Upasswd" |passwd --stdin $Uname &> /dev/null 17 if [ $? -eq 0 ] 18 then 19 echo -e "账号创建成功!" 20 else 21 echo -e "创建失败!" 22 fi 23 24 done 25 26done

15. 红色进度条

1#!/bin/bash 2 3 4declare -a ary 5 6 7for i in `seq 0 20` 8do 9 10 ary[$i]=" " 11 echo -en "\e[41;5m ${ary[@]}\e[;0m" 12 sleep 1 13 14done

16.监控服务器网卡流量

1#!/bin/bash 2#network 3#Mike.Xu 4while : ; do 5speedtime='date +%m"-"%d" "%k":"%M' 6speedday='date +%m"-"%d' 7speedrx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-' 8speedtx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-' 9sleep 2 10speedrx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-' 11speedtx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-' 12speedrx_result=$[(speedrx_after-speedrx_before)/256] 13speedtx_result=$[(speedtx_after-speedtx_before)/256] 14echo"$speedday$speedtime Now_In_Speed: "$speedrx_result"kbps Now_OUt_Speed: "$speedtx_result"kbps" 15sleep 2 16done

17. 检测CPU剩余百分比

1#!/bin/bash 2 3#Inspect CPU 4 5#Sun Jul 31 17:25:41 CST 2016 6 7PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin 8export PATH 9 10TERM=linux 11export TERM 12 13CpuResult=$(top -bn 1 | grep "Cpu" | awk '{print $5}' | sed 's/\..*$//g') 14 15if [[ $CpuResult < 20 ]];then 16 echo "CPU WARNING : $CpuResult" > /service/script/.cpu_in.txt 17 top -bn 1 >> /service/script./cpu_in.txt 18 mail -s "Inspcet CPU" wl < /service/script/.cpu_in.txt 19fi

18.检测磁盘剩余空间

1#!/bin/bash 2 3#Insepct Harddisk , If the remaining space is more than 80%, the message is sent to the wl 4 5#Tue Aug 2 09:45:56 CST 2016 6 7PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin 8 9export PATH 10 11for RemainingSpace in $(df -h | awk '{print $5}' | grep -v 'Use' | sed -e 's/[%]//g') 12do 13 if [[ $RemainingSpace > 80 ]];then 14 echo -e "$RemainingSpace" 15 echo -e "$(df -h | grep $RemainingSpace)" > /service/script/.HarddiskWarning 16 mail -s "disk Warning" wl < /service/script/.HarddiskWarning 17 fi 18done

19. bash-实现检测apache状态并钉钉报警

1#!/bin/bash 2 3 4function trans_text(){ 5 text=$1 6curl 'https://oapi.dingtalk.com/robot/send?access_token=b4fcf5862088a1bc7f2bf66aea051869e62ff5879fa0e0fddb0db9b1494781c2' -H'Content-Type: application/json' -d' 7{ 8 "msgtype": "text", 9 "text": { 10 "content": "'"$text"'" 11 }, 12}' 13} 14 15 16function desk_check(){ 17 18 dftype=$1 19 shell_row=`df |wc -l` 20 21for i in `seq 2 $shell_row` 22do 23 24 temp=(`df -h |head -n $i |tail -n 1 |awk '{print $5 "\t" $6}'`) 25 disk="`echo ${temp[0]} |cut -d "%" -f 1`" 26 name="${temp[1]}" 27 hostname=`hostname` 28 IP=`ifconfig |grep -v "127.0.0.1" |grep "inet addr:" |sed 's/^.*inet addr://g'|sed 's/ Bcas..*$//g'` 29 #echo -e "$disk $name" 30 Dat=`date "+%F %T"` 31 32 if [ $disk -ge $dftype ] 33 then 34 echo " 35 ======================== \n 36 >磁盘分区异常< \n 37 主机名: $hostname \n 38 IP地址: $IP \n 39 分区名: $name \n 40 使用率: $disk %\n 41 发生时间: $Dat \n 42 ========================= \n" 43 fi 44done 45} 46 47function apache_check(){ 48 url=$1 49URL="http://$url/" 50HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` 51 52if [ $HTTP_CODE != 200 ] 53 then 54 echo " 55 ======================== \n 56 >Apache服务异常< 57 主机名: $hostname \n 58 IP地址: $IP \n 59 返回代码: $HTTP_CODE \n 60 发生时间: $Dat \n 61 ========================= \n" 62fi 63} 64 65while true 66do 67 desk_check 10 68 apache_check 127.0.0.1 69 70 sleep 10 71done

20.内存检测

1#!/bin/bash 2 3#Inspect Memory : If the memory is less than 500 , then send mail to wl 4 5#Tue Aug 2 09:13:43 CST 2016 6 7 8PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin 9 10export PATH 11 12 13MEM=$(free -m | grep "Mem" | awk '{print $4}') 14 15if [[ MEM < 500 ]];then 16 echo -e "Memory Warning : Memory free $MEM" > /service/script/.MemoryWarning 17 mail -s "Memory Warning" wl < /service/script/.MemoryWarning 18fi

21.剩余inode检测

1#!/bin/bash 2 3#Inspcet Inode : If the free INODE is less than 200, the message is sent to the wl 4 5#Tue Aug 2 10:21:29 CST 2016 6 7PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin 8 9export PATH 10 11for FreeInode in $(df -i | grep -v "Filesystem" | awk '{print $4}') 12do 13 if [[ $FreeInode < 200 ]];then 14 echo -e "$(df -i | grep "$FreeInode")" > /service/script/.FreeInode 15 mail -s "FreeInode Warning" wl < /service/script/.FreeInode 16 fi 17done

22.判断哪些用户登陆了系统

1#!/bin/bash 2 3declare -i count=0 4 5while true;do 6 7 if who |grep -q -E "^wang" 8 then 9 echo -e "用户wang 登陆了系统\n 这是第$count 次!威信公众浩:wljsghq" 10 break 11 else 12 let count++ 13 fi 14 15 sleep 3 16done 17~ 18 19 20 21示例:找出UID为偶数的所有用户,显示其用户名和ID号; 22 23#!/bin/bash 24while read line; do 25 userid=$(echo $line | cut -d: -f3) 26 if [ $[$userid%2] -eq 0 ]; then 27echo $line | cut -d: -f1,3 28 fi 29done < /etc/passwd

23.批量创建账号

1#!/bin/bash 2 3sum=1 4 5while [ $sum -le 30 ] 6do 7 if [ $sum -le 9 ] 8 then 9 user="user_0$sum" 10 else 11 user="user_$sum" 12 fi 13 14 useradd $user 15 echo "123456" |passwd --stdin $user 16 chage -d 0 $user 17 let sum=sum+1 18 19done

24.批量扫面存活

1#!/bin/bash 2#By:lyshark 3 4#nmap 192.168.22.0/24>ip 5 6 7MAC=`cat ip |awk '$1 == "MAC" && $NF == "(VMware)"{print $3}'` 8 9for i in `seq 1 20` 10 11do 12 13temp=`echo ${MAC[@]} |awk '{print $i}'` 14 15IP=`cat /ip |grep -B5 $temp |grep "Nmap scan"|awk '{print $5}'` 16 17 18 echo $IP |awk '{print $1}' 19done

25.正则匹配IP

1^[0-9]{0,2}|^1[0-9]{0,2}|^2[0-5]{0,2} 2 3 egrep "(^[0-9]{1,2}|^1[0-9]{0,2}|^2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})\.([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2})$" 4 5 6 7 8 9([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2}) 10([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2}) 11([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2}) 12([0-9]{1,2}|1[0-9]{0,2}|2[0-5]{0,2}) 13 14 15 16egrep "((25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])))\.){3}(25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])))" 17 18 19 20ls |egrep "((25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])))\.){3}(25[0-5]|2[0-4][0-9]|((1[0-9]{2})|([1-9]?[0-9])$))"

26.正则匹配邮箱

1egrep "^[0-9a-zA-Z][0-9a-zA-Z_]{1,16}[0-9a-zA-Z]\@[0-9a-zA-Z-]*([0-9a-zA-Z])?\.(com|com.cn|net|org|cn)$" rui 2 3 4ls |egrep "^(([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$"

27.实现布片效果

1#!/bin/bash 2 3 4function ary_go 5{ 6 $1 $2 7 8 for (( i=0;i<=$1;i++ )) 9 do 10 for (( s=0;s<=$2;s++ )) 11 do 12 if [ $[$i%2] == 0 ] 13 then 14 15 if [ $[$s%2] == 0 ] 16 then 17 echo -en " " 18 else 19 echo -en "\e[;44m \e[;m" 20 fi 21 else 22 if [ $[$s%2] == 0 ] 23 then 24 echo -en "\e[;42m \e[;m" 25 else 26 echo -en " " 27 fi 28 29 30 31 fi 32 33 done 34 echo 35 36 done 37 38} 39 40 41ary_go 25 50

28.剔除白名单以外的用户

1#!/bin/bash 2w | awk 'NR>=3 {printf $1 "\t" $2 "\t" $3 "\n"}' > /tmp/who.txt 3for i in $(awk '{printf $1}' /tmp/bai.txt) 4do 5 k=$(egrep -v "$i" /tmp/who.txt | awk '{printf $2} "\n"' | awk '{printf $2 "\n"}') 6 for j in $k 7 do 8 pkill -9 -t "$j" 9 done 10done

福利分享

1.GitHub上星标100k+面试攻略 2.网络安全所有方向的学习路线 3.60多个视频教程 4.100多个小项目 5.300多本电子书 6.渗透测试知识点汇总 7.CTF资料库

【资料领取】

在这里插入图片描述

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Shell在日常工作中的应用实践

作为一名测试开发工程师,在与linux服务器交互过程中,大都遇到过许多问题,shell脚本小巧且功能强大,本篇文章给大家分享了些日常使用到的shell脚本,帮助我们提高工作效率

0615 shell编程1

0615shell编程1一、shell脚本介绍shell是一种脚本语言和传统的开发语言比较,会比较简单shell有自己的语法;可以使用逻辑判断、循环等语法可以自定义函数,目的就是为了减少重复的代码shell是系统命令的集合

Linux下shell脚本监控Tomcat的状态并实现自动启动

最近公司需要在Linux下监控tomcat的服务,一旦tomcat服务存在异常或者宕机,重启tomcat保证服务的正常运行,由于Linux下有Shell脚本可以实现此效果,下面是Linux下shell脚本监控Tomcat的状态并实现自动启动的步骤。1.编写Shell脚本monitor.sh!/bin/sh\func:自动监控tomcat脚本