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

#!/bin/bash

shell_user="root"
shell_domain="apache" shell_list="/root/ip_list"
shell_row=`cat $shell_list |wc -l` function trans_text(){
text=$ curl 'https://oapi.dingtalk.com/robot/send?access_token=b4fcf5862088a1bc7f2bf66a' -H'Content-Type: application/json' -d'{      #指定钉钉机器人hook地址
"msgtype": "text",
"text": {
"content": "'"$text"'"
},
}'
} function apache_check_80(){
ip=$
URL="http://$ip/index.html"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` if [ $HTTP_CODE != ]
then
trans_text "
=================================================================
\n $ip Apache 服务器状态异常,网页返回码: '"$HTTP_CODE"' 请及时处理 ! \n
================================================================= \n"
fi
} while true
do shell_list="/root/ip_list"
shell_row=`cat $shell_list |wc -l`
for temp in `seq $shell_row`
do
Ip_Addr=`cat $shell_list |head -n $temp |tail -n `
apache_check_80 $Ip_Addr
done sleep
done

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

#!/bin/bash

FSMAX=""
remote_user='root'
remote_ip=(IP地址列表)
ip_num='' while [ "$ip_num" -le "$(expr ${#remote_ip[@]} -l)"]
do
read_num=''
ssh "$remote_user"@"${remote_ip[$ip_num]}" df -h > /tmp/diskcheck_tmp
grep '^/dev/*' /tmp/diskcheck_tmp | awk '{print $5}'|sed 's/\%//g' > /tmp/diskcheck_num_tmp while [ "$read_num" -le $(wc -l < /tmp/diskcheck_num_tmp) ]
do
size=$(sed -n "$read_num" 'p' /tmp/diskcheck_num_tmp)
if [ "size" -gt "$FSMAX" ]
then
$(grep '^/dev/*' /tmp/diskcheck_tmp |sed -n $read_num'p' > /tmp/disk_check_mail)
$(echo ${remote_ip[$ip_num]}) >> /tmp/disk_check_mail)
$(mail -s "diskcheck_alert" admin < /tmp/disk_check_mail)
fi read_num=$(expr $read_num + )
done ip_num=$(expr $ip_num + )
done

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

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

自动ftp上传

#! /bin/bash

ftp -n << END_FTP
open 192.168.1.22
user test testing //用户名test 密码:testing
binary
prompt off //关闭提示
mput files //上传files文件
close
bye
END_FTP

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

#!/bin/bash

DAY=`date +%Y%m%d`
SIZE=`du -sh /var/lib/mysql`
echo "Date: $DAY" >> /tmp/dbinfo.txt
echo "Data Size: $SIZE" >> /tmp/dbinfo.txt
cd /opt/dbbak &> /dev/null || mkdir /opt/dbbak
tar zcf /opt/dbbak/mysqlbak-${DAY}.tar.gz /var/lib/mysql /tmp/dbinfo.txt &> /dev/null
rm -f /tmp/dbinfo.txt crontab-e
*/ * * /opt/dbbak/dbbak.sh

打印彩虹

declare -a ary

for i in `seq  `
do ary[$i]=" "
echo -en "\e[$i;5m ${ary[@]}\e[;0m" done declare -a ary
for s in `seq `
do
for i in `seq `
do
ary[$i]=" "
echo -en "\e[$i;5m ${ary[@]}\e[;0m"
done
done

打印菱形

#!/bin/bash

for (( i = ; i < ; i++))
do
if [[ $i -le ]]
then
for ((j = $((-i)); j > i; j--))
do
echo -n " "
done for ((m = ; m <= $((*i-)); m++))
do
echo -n "* "
done
echo ""
#*****************************************************************************
elif [[ $i -gt ]]
then
n=$((-i))
for ((j = $((-n)); j > n; j--))
do
echo -n " "
done for ((m = ; m <= $((*n-)); m++))
do
echo -n "* "
done
echo ""
fi done

expect实现远程登陆自动交互

#!/usr/bin/expect -f

set ipaddress [lindex $argv ]

set passwd [lindex $argv ]

set timeout 

spawn ssh-copy-id root@$ipaddress

expect {

"yes/no" { send "yes\r";exp_continue }

"password:" { send "$passwd\r" }

}

#expect "*from*"

#send "mkdir -p ./tmp/testfile\r"

#send "exit\r"

#expect "#" #i# 命令运行完, 你要期待一个结果, 结果就是返回shell提示符了(是# 或者$)

http心跳检测

URL="http://192.168.22.191/index.html"

THHP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`

if [ $HTTP_CODE !=  ]
then
echo -e "apache code:"$HTTP_CODE""
fi

PV过量自动实现防火墙封IP

#!/bin/bash

log=/tmp/tmp.log

[ -f $log ] || touch $log

function add_iptales()
{
while read line
do
ip=`echo $line |awk '{print $2}'`
count=`echo $line |awk '{print $1}'`
if [ $count -gt ] && [ `iptables -L -n |grep "$ip" |wc -l` -lt ]
then
iptables -I INPUT -s $ip -j DROP
echo -e "$list isdropped">>/tmp/droplist.log
fi done<$log
} function main()
{
while true
do
netstat -an|grep "EST" |awk -F '[:]+' '{print $6}'|sort |uniq -c >$log
add_iptales
sleep
done } main

shell实现自动安装

#!/bin/bash

function MyInstall
{
if ! rpm -qa |grep -q "^$1"
then yum install $
if [ $? -eq ]
then
echo -e "$i install is ok\n"
else
echo -e "$1 install no\n"
fi
else
echo -e "yi an zhuang ! \n"
fi
} for ins in mysql php httpd
do
MyInstall $ins
done

shell实现插入排序

#!/bin/bash

declare -a array

for i in `seq  `
do
array[$i]=$RANDOM done echo -e "Array_1: ${array[@]}" for (( x=;x<=;x++ ))
do
for(( y=;y<=;y++ ))
do
if [ ${array[$y]} -gt ${array[$y+]} ]
then
temp=${array[$y]}
array[$y]=${array[$y+]}
array[$y+]=$temp
fi done done echo -e "Array_2: ${array[@]}"

bash实现动态进度条

#!/bin/bash
i=
bar=''
index=
arr=( "|" "/" "-" "\\" ) while [ $i -le ]
do
let index=index%
printf "[%-100s][%d%%][\e[43;46;1m%c\e[0m]\r" "$bar" "$i" "${arr[$index]}"
let i++
let index++
usleep
bar+='#'
clear
done printf "\n"

根据文件内容创建账号

#!/bin/bash

for Uname in `cat /root/useradd.txt |gawk '{print $1}'`
do id $Uname &> /dev/null
if [ $? -eq ]
then
echo -e "这个账号已存在!"
continue
fi
for Upasswd in `cat /root/useradd.txt |gawk '{print $2}'`
do
useradd $Uname &> /dev/null
echo "$Upasswd" |passwd --stdin $Uname &> /dev/null
if [ $? -eq ]
then
echo -e "账号创建成功!"
else
echo -e "创建失败!"
fi done done

红色进度条

#!/bin/bash

declare -a ary

for i in `seq  `
do ary[$i]=" "
echo -en "\e[41;5m ${ary[@]}\e[;0m"
sleep done

监控服务器网卡流量

#!/bin/bash
#network
#Mike.Xu
while : ; do
speedtime='date +%m"-"%d" "%k":"%M'
speedday='date +%m"-"%d'
speedrx_before='ifconfig eth0|sed -n "8"p|awk '{print $}'|cut -c7-'
speedtx_before='ifconfig eth0|sed -n "8"p|awk '{print $}'|cut -c7-'
sleep
speedrx_after='ifconfig eth0|sed -n "8"p|awk '{print $}'|cut -c7-'
speedtx_after='ifconfig eth0|sed -n "8"p|awk '{print $}'|cut -c7-'
speedrx_result=$[(speedrx_after-speedrx_before)/]
speedtx_result=$[(speedtx_after-speedtx_before)/]
echo"$speedday$speedtime Now_In_Speed: "$speedrx_result"kbps Now_OUt_Speed: "$speedtx_result"kbps"
sleep
done

检测CPU剩余百分比

#!/bin/bash

#Inspect CPU

#Sun Jul  :: CST 

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin
export PATH TERM=linux
export TERM CpuResult=$(top -bn | grep "Cpu" | awk '{print $5}' | sed 's/\..*$//g') if [[ $CpuResult < ]];then
echo "CPU WARNING : $CpuResult" > /service/script/.cpu_in.txt
top -bn >> /service/script./cpu_in.txt
mail -s "Inspcet CPU" wl < /service/script/.cpu_in.txt
fi

检测磁盘剩余空间

#!/bin/bash

#Insepct Harddisk , If the remaining space is more than %, the message is sent to the wl

#Tue Aug   :: CST 

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin

export PATH

for RemainingSpace in $(df -h | awk '{print $5}' | grep -v 'Use' | sed -e 's/[%]//g')
do
if [[ $RemainingSpace > ]];then
echo -e "$RemainingSpace"
echo -e "$(df -h | grep $RemainingSpace)" > /service/script/.HarddiskWarning
mail -s "disk Warning" wl < /service/script/.HarddiskWarning
fi
done

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

#!/bin/bash

function trans_text(){
text=$
curl 'https://oapi.dingtalk.com/robot/send?access_token=b4fcf5862088a1bc7f2bf66aea051869e62ff5879fa0e0fddb0db9b1494781c2' -H'Content-Type: application/json' -d'
{
"msgtype": "text",
"text": {
"content": "'"$text"'"
},
}'
} function desk_check(){ dftype=$
shell_row=`df |wc -l` for i in `seq $shell_row`
do temp=(`df -h |head -n $i |tail -n |awk '{print $5 "\t" $6}'`)
disk="`echo ${temp[0]} |cut -d "%" -f 1`"
name="${temp[1]}"
hostname=`hostname`
IP=`ifconfig |grep -v "127.0.0.1" |grep "inet addr:" |sed 's/^.*inet addr://g'|sed 's/ Bcas..*$//g'`
#echo -e "$disk $name"
Dat=`date "+%F %T"` if [ $disk -ge $dftype ]
then
echo "
======================== \n
>磁盘分区异常< \n
主机名: $hostname \n
IP地址: $IP \n
分区名: $name \n
使用率: $disk %\n
发生时间: $Dat \n
========================= \n"
fi
done
} function apache_check(){
url=$
URL="http://$url/"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"` if [ $HTTP_CODE != ]
then
echo "
======================== \n
>Apache服务异常<
主机名: $hostname \n
IP地址: $IP \n
返回代码: $HTTP_CODE \n
发生时间: $Dat \n
========================= \n"
fi
} while true
do
desk_check
apache_check 127.0.0.1 sleep
done

内存检测

#!/bin/bash

#Inspect Memory : If the memory is less than  , then send mail to wl

#Tue Aug   :: CST 

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin

export PATH

MEM=$(free -m | grep "Mem" | awk '{print $4}')

if [[ MEM <  ]];then
echo -e "Memory Warning : Memory free $MEM" > /service/script/.MemoryWarning
mail -s "Memory Warning" wl < /service/script/.MemoryWarning
fi

剩余inode检测

#!/bin/bash

#Inspcet Inode : If the free INODE is less than , the message is sent to the wl

#Tue Aug   :: CST 

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/wl/bin

export PATH

for FreeInode in $(df -i | grep -v "Filesystem" | awk '{print $4}')
do
if [[ $FreeInode < ]];then
echo -e "$(df -i | grep "$FreeInode")" > /service/script/.FreeInode
mail -s "FreeInode Warning" wl < /service/script/.FreeInode
fi
done

判断哪些用户登陆了系统

#!/bin/bash

declare -i count=

while true;do

        if who |grep -q -E "^wang"
then
echo -e "用户wang 登陆了系统\n 这是第$count 次!"
break
else
let count++
fi sleep
done
~ 示例:找出UID为偶数的所有用户,显示其用户名和ID号; #!/bin/bash
while read line; do
userid=$(echo $line | cut -d: -f3)
if [ $[$userid%] -eq ]; then
echo $line | cut -d: -f1,
fi
done < /etc/passwd

批量创建账号

#!/bin/bash

sum=

while [ $sum -le  ]
do
if [ $sum -le ]
then
user="user_0$sum"
else
user="user_$sum"
fi useradd $user
echo "" |passwd --stdin $user
chage -d $user
let sum=sum+ done

批量扫面存活

#!/bin/bash
#By:lyshark #nmap 192.168.22.0/>ip MAC=`cat ip |awk '$1 == "MAC" && $NF == "(VMware)"{print $3}'` for i in `seq ` do temp=`echo ${MAC[@]} |awk '{print $i}'` IP=`cat /ip |grep -B5 $temp |grep "Nmap scan"|awk '{print $5}'` echo $IP |awk '{print $1}'
done

正则匹配IP

^[-]{,}|^[-]{,}|^[-]{,}

 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})$"

([-]{,}|[-]{,}|[-]{,})
([-]{,}|[-]{,}|[-]{,})
([-]{,}|[-]{,}|[-]{,})
([-]{,}|[-]{,}|[-]{,}) 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])))" ls |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])$))"

正则匹配邮箱

egrep "^[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

ls |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])$"

实现布片效果

#!/bin/bash

function ary_go
{
$ $ for (( i=;i<=$;i++ ))
do
for (( s=;s<=$;s++ ))
do
if [ $[$i%] == ]
then if [ $[$s%] == ]
then
echo -en " "
else
echo -en "\e[;44m \e[;m"
fi
else
if [ $[$s%] == ]
then
echo -en "\e[;42m \e[;m"
else
echo -en " "
fi fi done
echo done } ary_go

剔除白名单以外的用户

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

Bash/Shell-脚本整理(长期更新)的更多相关文章

  1. java 调用bash shell脚本阻塞的小问题的解决

    java  调用bash shell脚本阻塞的小问题的解决 背景 使用java实现的web端,web端相应用户的界面操作,使用java调用bash实现的shell脚本进行实际的操作,操作完成返回执行结 ...

  2. 系统管理中 bash shell 脚本常用方法总结

    在日常系统管理工作中,需要编写脚本来完成特定的功能,编写shell脚本是一个基本功了!在编写的过程中,掌握一些常用的技巧和语法就可以完成大部分功能了,也就是2/8原则 1. 单引号和双引号的区别 单引 ...

  3. Linux Shell——bash shell 脚本简介

    bash shell 脚本简介 shell 运行环境 如果你运行的是 Unix 或 Linux 系统,例如 Ubuntu,Red Hat,SUSE Linux,还有macOS,都是内置了 bash s ...

  4. linux shell 脚本 svn自动更新项目并且打包 、发布、备份

    这里先准备一个配置文件,用于保存svn地址.目的路径.用户名跟密码 配置文件名问:toolConfig.properties #svn地址 svnAddress=https://192.168.1.2 ...

  5. Linux bash shell脚本语法入门

    1.基础 #!/bin/bash   //bash脚本第一句都是这个,他会让系统指定以bash来解释这个脚本 #                 //shell脚本注释符号 2.变量和使用 HOME= ...

  6. (转)Linux bash shell脚本语法入门

    http://www.linuxsky.org/doc/newbie/201004/389.html 1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这 ...

  7. 常用shell脚本(持续更新中)

    1. 写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. 答案 : 输出用户名,当前日期和时间,以及当前工作目录的命令就是logname,date,who i am和pwd. #!/b ...

  8. shell脚本练习题(更新中...)

    练习题(这里贴的是自己写的代码, 网上给的题目代码我会附加在最下面) 1. 编写shell脚本,计算1-100的和: #!/bin/bash #caculate the to `; do sum=$[ ...

  9. bash shell脚本之使用expr运算

    bash shell中的数学运算 cat test7: #!/bin/bash # An example of using the expr command var1= var2= var3=`exp ...

随机推荐

  1. Oracal 学习之用户角色创建分配表空间 给角色分配权限

    //创建角色inspur 密码为inspur,默认的表空间为USERS create user inspur identified by inspur default tablespace USERS ...

  2. ueditor 上传图片

    ueditor在配置图片,附件上传  首先,是以web项目为基础的,需要安装好eclipse以及tomcat 其次,需要下载ueditor(可去百度官网下载 http://ueditor.baidu. ...

  3. 各种 on事件触发js代码

    [转]各种 on事件触发js代码 1.onmouseenter:当鼠标进入选区执行代码 <div style="background-color:red" onmouseen ...

  4. 用rpm命令安装定时器crontab

    crontab -l  command not found 准备以下安装包: ls -l总用量 1004-rw-r--r-- 1 root root  76296 10月  9 16:01 croni ...

  5. 01.Java 开发简单的计算器

    难度为一般,适合具有 Java 基础和 Swing 组件编程知识的用户学习一. 实验介绍1.1 实验内容本次实验利用Java开发一个可以进行简单的四则运算的图形化计算器,会使用到 Java Swing ...

  6. 2019.01.21 bzoj1758: [Wc2010]重建计划(01分数规划+长链剖分+线段树)

    传送门 长链剖分好题. 题意简述:给一棵树,问边数在[L,R][L,R][L,R]之间的路径权值和与边数之比的最大值. 思路: 用脚指头想都知道要01分数规划. 考虑怎么checkcheckcheck ...

  7. C++IO cin

    cin cin.get() 每次只读缓冲区一个字符,不能接收空格 cin.getline() 读缓冲区一行,能够接收空格 cin.ignore(2) 忽略缓冲器2个字节 int i = cin.pee ...

  8. ubuntu16.04 编译安卓4.2

    1. root@ge-Lenovo:/usr/lib/jvm# cd /home/material/install/jdk/    jdk-6u29-linux-x64.bin  jdk-6u45-l ...

  9. Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'

    Mac OS X的升级或其他原因可能会导致MySQL启动或开机自动运行时   在MySQL操作面板上会提示“Warning:The /usr/local/mysql/data directory is ...

  10. leetcode - [6]Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...