轮询检测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. python文件打包

    python文件打包 先安装 pip3 install --upgrade pyinstaller 进入到文件的当前目录 ,在命令行中输入: pyinstaller -F -w[文件名].py 其中- ...

  2. Oracle服务器修改IP后

    机房有两套网络,一套办公网,一套机房的内网,办公网可以通过vpn在其他地方访问,内网只能在公司办公室访问.团队有同事去外地办公,开发的时候需要通过客户端直连数据库,于是把数据库服务器的网线换到办公网的 ...

  3. 2018.10.24 NOIP模拟 小 C 的序列(链表+数论)

    传送门 考虑到a[l],gcd(a[l],a[l+1]),gcd(a[l],a[l+1],a[l+2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l+1]),gcd(a[ ...

  4. git常规命令

    $ mkdir filename 创建一个空目录 $ git init 把这个目录变成Git可以管理的仓库 $ pwd 用于显示当前目录 $ cat <file> 查看文件内容 $ git ...

  5. C++STL 容器比较

    Vector的使用场景:比如软件历史操作记录的存储,我们经常要查看历史记录,比如上一次的记录,上上次的记录,但却不会去删除记录,因为记录是事实的描述. deque的使用场景:比如排队购票系统,对排队者 ...

  6. ibatis注意要点

    一.ibatis的关键字like查询 select * from t_student where s_name '%张%'; 这种like语句在ibatis中怎么写,他们现在的项目是用ibatis作为 ...

  7. 实习番外篇:解决C语言使用Makefile无法实现更好的持续集成问题

    工作中遇见的一个问题,提供项目源代码的情况下,希望对项目进行持续集成,达到一个C项目增量编译的效果.原本第一天是想通过模拟Makefile执行步骤来实现整个过程的,但是事实上发现整个Makefile显 ...

  8. c#文件比较Code

    我想我们很多时候想比较一个文件里面是否有改动,比如一个dll库是新加了一个方法或修改了其中的方法实现,不能通过可视化的工具来比较的时候,可以用这个小工具来比较, 以下是比较文件的代码. using S ...

  9. 第23章:MongoDB-聚合操作--聚合命令

    ①count() 范例:统计students表中的数据量 db.students.count(); 范例:模糊查询 db.students.count("name":/张/i); ...

  10. VB网络编程中Winsock的使用

    原文链接:http://tech.163.com/06/0407/14/2E46BB930009159S.html 如同上面的内容所描述的,不论您使用UDP协议或是TCP协议,Winsock控件都可以 ...