Bash/Shell-脚本整理(长期更新)
轮询检测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-脚本整理(长期更新)的更多相关文章
- java 调用bash shell脚本阻塞的小问题的解决
java 调用bash shell脚本阻塞的小问题的解决 背景 使用java实现的web端,web端相应用户的界面操作,使用java调用bash实现的shell脚本进行实际的操作,操作完成返回执行结 ...
- 系统管理中 bash shell 脚本常用方法总结
在日常系统管理工作中,需要编写脚本来完成特定的功能,编写shell脚本是一个基本功了!在编写的过程中,掌握一些常用的技巧和语法就可以完成大部分功能了,也就是2/8原则 1. 单引号和双引号的区别 单引 ...
- Linux Shell——bash shell 脚本简介
bash shell 脚本简介 shell 运行环境 如果你运行的是 Unix 或 Linux 系统,例如 Ubuntu,Red Hat,SUSE Linux,还有macOS,都是内置了 bash s ...
- linux shell 脚本 svn自动更新项目并且打包 、发布、备份
这里先准备一个配置文件,用于保存svn地址.目的路径.用户名跟密码 配置文件名问:toolConfig.properties #svn地址 svnAddress=https://192.168.1.2 ...
- Linux bash shell脚本语法入门
1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这个脚本 # //shell脚本注释符号 2.变量和使用 HOME= ...
- (转)Linux bash shell脚本语法入门
http://www.linuxsky.org/doc/newbie/201004/389.html 1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这 ...
- 常用shell脚本(持续更新中)
1. 写一个shell脚本来得到当前的日期,时间,用户名和当前工作目录. 答案 : 输出用户名,当前日期和时间,以及当前工作目录的命令就是logname,date,who i am和pwd. #!/b ...
- shell脚本练习题(更新中...)
练习题(这里贴的是自己写的代码, 网上给的题目代码我会附加在最下面) 1. 编写shell脚本,计算1-100的和: #!/bin/bash #caculate the to `; do sum=$[ ...
- bash shell脚本之使用expr运算
bash shell中的数学运算 cat test7: #!/bin/bash # An example of using the expr command var1= var2= var3=`exp ...
随机推荐
- 拖拽文件实现无刷新上传,支持2G文件
客户端 用HTML5:jQuery File Upload http://blueimp.github.io/jQuery-File-Upload/basic-plus.html API https: ...
- 【Linux】zlib安装
zlib简介 zlib是提供数据压缩用的函式库,由Jean-loup Gailly与Mark Adler所开发,初版0.9版在1995年5月1日发表.zlib使用DEFLATE算法,最初是为libpn ...
- MySQL正则表达式 REGEXP详解
在开始这个话题之前我们首先来做一个小实验,比较一下REGEXP和Like他们两个哪个效率高,如果效率太低,我们就没有必要做过多的研究了,实验的代码如下:<?phpRequire("co ...
- centos7 安装网卡
1.虚拟机测试,先开启命令行 su systemctl set-default multi-user.target reboot 2.编辑网卡 虚拟机网络设置成桥接模式 vi /etc/sysconf ...
- MySQL批量修改表前缀
error_reporting(0); $old_pre = 'tdr_'; // 原表前缀 $new_pre = 'db_'; // 新表前缀 // 配置连接 $db = new mysqli('1 ...
- linux 安装 ORACLE JDK 8
1.卸载默认的OPENJDK 查看 open jdk 的安装 rpm -qa | grep java 卸载 openjdk rpm -e --nodeps java-1.7.0-openjdk-1.7 ...
- user表中存在多条相同user不同host用户信息时MySQL该匹配哪条记录登录?
问题: 当用户名相同,但主机名不同的多条记录.用户由不同主机登录时,选择使用那条记录来验证,数据库版本为:5.6.25 如:IP为192.168.141.241 hostname为vhost02主机上 ...
- winform 可拖动无边框窗体解决办法
方法一:通过重载消息处理实现. 鼠标的拖动只对窗体本身有效,不能在窗体上的控件区域点击拖动 /// <summary> /// 通过重载消息处理实现.重写窗口过程(WndProc),处理一 ...
- (转)本地搭建环境wamp下提示不支持GD库的解决方法
转自:http://www.zzdp.net/local-wamp-gd GD库是什么?GD库,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片. ...
- Forward团队-爬虫豆瓣top250项目-设计文档
组长地址:http://www.cnblogs.com/mazhuangmz/p/7603594.html 成员:马壮,李志宇,刘子轩,年光宇,邢云淇,张良 设计方案: 1.能分析HTML语言: 2. ...