2013-10-16 shell脚本练习题

回复 收藏
写一个脚本,判断本机的80端口是否开启着,如果开启着什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知你自己。脚本写好后,可以每一分钟执行一次,也可以写一个死循环的脚本,30s检测一次。

{{{密码回复可见}}}
2013-10-16 15:12 举报
已邀请:
0

Hello_Lemon

赞同来自:

#port=80
po=80
#mail
ma=abc@139.com

while :;do
        rs=`netstat -anlp|awk '$4~/:80$/{print $NF}'`
        if [ -z $rs ];then
                echo "The port $po is die"|mail -s "message" $ma
                service httpd start;
        fi
        sleep 30
done
0

chekir

赞同来自:

好久没写shell脚本咯
0

oszhang

赞同来自:

xuxu
0

第六感

赞同来自:

{:4_109:}
0

乐宝儿

赞同来自:

# /bin/bash
while : ; do
if grep -q netstat -lnp |grep "80" ;then
  else service httpd restart
       echo "80meikaiqi" |mail -s "80meikaiqi " 1025781634@qq.com
fi
sleep 30
done

0

good

赞同来自:

看看
0

hkj8808

赞同来自:

#!/bin/bash
while :;do
  a=`netstat -lnp |grep ":80" >/dev/null`
  if [ -z $a ];then
      /etc/init.d/httpd restart
      echo "xxx" |mail -s "xxx" 123@qq.com
  fi
  sleep 30
done
0

不断更新

赞同来自:

#!/bin/bash
while :;do
  a=`netstat -lnp |grep ":80"`
  if [ -z $a ];then
      echo "Http is down" | mail -s baojing 891287870@qq.com
      /etc/init.d/httpd restart
  fi
  sleep 30
done
0

ocean

赞同来自:

[root@OceanV sh]# cat check_httpd.sh
#!/bin/bash
port=80
mail=root@localhost
while true
do
  s=`netstat -natp |grep $port >/dev/null`
  if [ -z $s ];then
      /etc/init.d/httpd restart
      echo "httpd is stopped,now start it" |mail -s "restart httpd" $mail
  fi
  sleep 30
done
0

Budweiser小王子

赞同来自:

练习
0

Agatha-cn

赞同来自:

1
0

轩鹏

赞同来自:

q
0

lidunhuai

赞同来自:

#! /bin/bash
# Monitoring httpd

mail=abc@139.com
if netstat -antp |awk 'NR>=3 {print $4}'|grep -q ':80'
then
     echo "nothing" > /dev/null
else
     /etc/init.d/httpd restart
    echo "httpd restart now" mail -s "httpd restart" $mail
fi
0

黑色的梦

赞同来自:

{:4_91:}
0

齐天大圣

赞同来自:

#!/bin/bash
##httpd restart
while :;
do
  h=`netstat -ntpl |awk '{print $4}'|awk -F: '{print $2}' |grep '80'`
  if [ $h -eq '80' ]; then
     sleep 30
  else
     servicr httpd restart
  fi
done
0

木字当头

赞同来自:

0

zyfeifie

赞同来自:

#!/bin/bash
num=`netstat -lntp | grep “:80” | wc -l`
while true
do
if [ $num -eq 0 ];then
   echo "httpd is stopped" | mail -s "httpd need to restart" abc@feifei.com
   service httpd restart
fi
sleep 30
done
0

泡沫。

赞同来自:

11111111111
0

鸵鸟

赞同来自:

xx
0

Louis

赞同来自:


#!/bin/bash
## This script is for checking if port 80 is on.
## Writed by Louis on 2014/08/29 18:27

while :;do
    port=`netstat -nlpa|grep 'httpd'|grep ':80'`
    port1=`echo $port|awk '{print $4}'|awk -F ':' '{print $4}'`

    if [ -z $port1 ]; then
        service httpd start &> /dev/null
    fi
    sleep 30
done
0

游夜

赞同来自:

本帖最后由 游夜 于 2014-9-4 23:00 编辑

#!/bin/bash
while :;do            
  netstat -an | grep ':80'
        if [ echo $? -ne 0 ];then
                service httpd restart
                echo "restart httpd service."| mail -s "httpd restart" 275235245@qq.com
        fi
         sleep 30
done



0

寒一

赞同来自:

#! /bin/bash

while :;
do
    nmap -sS -vv 192.168.12.107 |grep 80 |tail -n1 > /tmp/nmap.txt  #借鉴论坛里的一个帖子
    rr80=`cat /tmp/nmap.txt`
    if [ "$rr80" = "" ]; then
       service httpd restart && mail -s "rr_port_80" abc@163.com < /tmp/nmap.txt
    fi
    sleep 30
done
0

wyatt88

赞同来自:

#!/bin/bash
while sleep 30
do
if netstat -lnp | grep -q ':80'
then
echo "80 down"
echo "80 down" | mail -s "Alarm" xxx@126.com
/etc/init.d/httpd restart
else
echo "normal" > /dev/mull
fi
done
0

So Long

赞同来自:

#!/bin/bash
port=80
while : ; do
          for i in ` netstat -tln | awk '{print $4}' | egrep -v 'Local | only'|awk -F ':' '{print $0}' | sed 's/.*://g'`; do
          if [ $i -ne $port  ];then
          echo "Httpd service is down!"  | mail -s  chengchengxiyou@126.com
          service httpd restart
          fi
          done
          sleep 30
done


0

nihao426181

赞同来自:

……………………………………………………………………………………6
0

huifeidexiaxia

赞同来自:

1
0

追梦人

赞同来自:

CSDC
0

jade

赞同来自:

参考学习学习 ....
0

王肖强

赞同来自:

  1. #!/bin/bash
  2. # Monitor httpd scripe.
  3. # Writen by Wangxiaoqiang 2014.11.24.

  4. MAIL=xxxxxxxxxxx@xxx.com

  5. while true
  6.   do
  7.     netstat -lnpt | grep 80 > /dev/null
  8.     if [ $? -ne 0 ]
  9.       then
  10.         service httpd restart > /dev/null
  11.         sleep 5
  12.         netstat -lnpt | grep 80 > /dev/null
  13.         if [ $? -ne 0 ]
  14.           then
  15.             echo "$HOSTNAME web is down" | mail -s "Monitor WEB FOR $HOSTNAME" $MAIL
  16.           else
  17.             echo "$HOSTNAME web is running" | mail -s "Monitor WEB FOR $HOSTNAME" $MAIL
  18.         fi
  19.      fi
  20.      sleep 30
  21. done

  22. # End
0

aqi

赞同来自:

本帖最后由 aqi 于 2014-11-24 16:45 编辑

while :
do
netstat -ntapl | grep ":80"
if [[ $? != 0 ]];then
    service httpd restart
    echo | mail -s "`hostname` httpd is warning" xxx@xxx.com
fi
sleep 30
done
0

阳光

赞同来自:

#!/bin/bash

while :; do
        netstat -lntp | grep httpd
        if [ `echo $?` -eq 0 ];then
                sleep 30
        else
                /etc/init.d/httpd restart
                echo "httpd restart" | mail -s "httpd restart" 445043972@qq.com
        fi
done
0

zhangw

赞同来自:

学习下
0

zq13054480665

赞同来自:

看看
0

2422606568

赞同来自:

学习
0

赵海龙

赞同来自:

#! /bin/bash
# 判断本机的80端口是否开启着,如果开启着什么都不做,如果发现端口不存在,那么重启一下httpd服务,并发邮件通知你自己。
# 脚本写好后,可以每一分钟执行一次,也可以写一个死循环的脚本,30s检测一次。

port=80
mail=zhaohailg@163.com
while :; do
        # 查看本机的80端口的连接状态
        result=`netstat -lnp |awk '$4 ~/:'$port'$/ {print $NF}'`;
        #echo "$result";
        #如果80端口没有开启,则开启
        if [ -z $result ]; then
                echo "The port $port is die!";
                /etc/init.d/nginx restart;
        else
                echo "The port $port is work!";

        fi
        #没30s执行检测一次
        sleep 30;
done


0

hehong

赞同来自:

太难,看一下
0

yaabb163

赞同来自:

ddddddddddddd
0

cmzsteven

赞同来自:

#!/bin/bash

while :; do
    open=`netstat -lnp | grep -w  80|wc -l`
    if [ "$open" == "0"]; then
        /usr/local/apache2/bin/apachectl start
        echo "The httpd server was stoped. The server will start right now!" |mail -s "The httpd server was stoped" abc@139.com
    fi
done
0

wuhen

赞同来自:

本帖最后由 wuhen 于 2015-2-18 18:58 编辑
  1. #! /bin/bash
  2. while :;
  3. do
  4. mail=123@123.com
  5. a=`netstat -lnp |grep ':80'|grep -q 'LISTEN'`
  6. if [ $? -ne 0 ]
  7. then
  8.     /etc/init.d/httpd restart >/dev/null 2> /dev/null
  9.     echo "The 80 port is down."|mail -s 'check_80' $mail
  10. else
  11. exit 0
  12. fi
  13. sleep 60
  14. done
或者写入crontab计划周期任务中 */1 * * * *
  1. #! /bin/bash
  2. mail=15620609228@163.com
  3. if netstat -lnp |grep ':80' |grep -q 'LISTEN'; then
  4.     exit
  5. else
  6.     /etc/init.d/httpd restart >/dev/null 2> /dev/null
  7.     echo "The 80 port is down."|mail -s 'check_80' $mail
  8.     n=`ps aux |grep httpd|grep -cv grep`
  9.     if [ $n -eq 0 ]; then
  10.         /etc/init.d/httpd start 2>/tmp/apache_start.err
  11.         mail -s 'apache_start_error' $mail < /tmp/apache_start.err
  12.     fi
  13. fi



0

ayu

赞同来自:

预习
0

GAARA

赞同来自:

{:4_109:}
0

qq20847697

赞同来自:

#!/bin/bash
#
ma=abc@139.com
while :; do
a=`netstat -lnp|grep ':80' `
if [ -z $a ] ; then
echo "The port $po is die"|mail -s "message" $ma
                service httpd restart;
fi
sleep 30
done
0

Wagskun

赞同来自:

看看
0

hangtiangazi

赞同来自:

铭哥。。。
0

wangdi244

赞同来自:

#!/bin/bash
#
while true
do
num=`netstat -tulnp | grep 80 |awk '{print $4}'|sed 's/://g'`
if [[ $num == "80" ]];then
echo "httpd is ok"
else
service httpd restart
echo "httpd restart"|mail -s httpd restart root@localhost.localdomain
fi
sleep 30
done
0

deal1120

赞同来自:

cc
0

sss

赞同来自:

本帖最后由 sss 于 2015-5-28 14:18 编辑
  1. #!/bin/bash
  2. #Inspection web Servicei
  3. #2015/05/15


  4. while :           
  5. do            
  6.         services=netstat -nltp | grep ':80' | awk -F ':' '{print $4}'
  7.         if [  -z $services ]
  8.         then
  9.                  service httpd restart >/dev/null 2>&1
  10.                  echo " YO YO httpd service reboot "| mail -s "httpd reboot" xxxxxxxxxx@qq.com
  11.          fi
  12.           sleep 30
  13. done


0

cisco12355074

赞同来自:

感谢楼主,我要学习一下
0

苏苏苏苏

赞同来自:

支持linux  支持阿铭
0

一岁拽起

赞同来自:

看看写的怎么样呢
0

哈哈琨少

赞同来自:

学习学习
0

andreking

赞同来自:

学习下
0

llzdwyp

赞同来自:

学习一下
0

丶小作

赞同来自:

kk
0

鑫柏

赞同来自:

#!/bin/bash
while :
do
a=`netstat -lnp|grep ":80"`
if [-z a];then
/etc/init.d/httpd restart
mail -s "http is down" 111@qq.com
fi
sleep 30
done
0

307141950

赞同来自:

对照答案
0

巧杜鹃

赞同来自:

学习
0

剑在飞

赞同来自:

{:4_91:}{:4_92:}{:4_91:}
0

Armani

赞同来自:


while :
do
    nc -t -z  -w  2  localhost  80  >  /dev/null
    case $? in
    0)
     echo "OK!"
     exit
     ;;
    *)
     /etc/init.d/httpd  restart
    echo  "It is  DOWN of  80 port!"  |  mail  -s "down" m13291298083_2@163.com
    ;;
esac
   sleep 30

done
0

翟厚翔

赞同来自:

  查看的8080端口 #!/bin/bash while : do  q=`lsof -i:8080|awk '{print $2}'|grep -v 'PID'|awk '{print $1}'` if [ -z "$q" ]   then     echo " number is 8080 ,please deal with it "|mail -s " 8080 service is problem"  18856066058@139.com      sh /home/tomcat/bin/startup.sh fi sleep 30 done
0

chenqi

赞同来自:

看答案
0

huguihua2002

赞同来自:

look
0

liang1990

赞同来自:

while :;do a=`netstat -lanp|grep ':80'|awk '{$1 ~ /tcp/} {print $0}'|wc -l` if [ $a != 1 ];then   service httpd restart   echo "80端口断开,已重启httpd服务" |mail -s test "httpd警告" 506556658@qq.com fi sleep 30000 done
0

wzwyql

赞同来自:

学习
0

shajoe

赞同来自:

while :
do
netstat -lnp |grep 80
a=`echo $?`
if [ $a != 0 ]
then
/usr/local/apache2/bin/apachectl restart
echo "TCP:80 restart" |mail -s "192.168.153.130:80" 123@mail.com
else
echo "TCP:80 is running"
fi
sleep 10
done
0

渐行渐远

赞同来自:

  1. #!/bin/bash
  2. a=`netstat -lnp | grep :80`
  3. if [ -z "$a" ]
  4. then
  5. /etc/init.d/httpd restart
  6. mail -s "aa" 825536458@qq.com
  7. fi
0

放牛

赞同来自:

{:7_173:}
0

timfeng3535

赞同来自:

dd
0

wudi61

赞同来自:

学习
0

汤小东

赞同来自:

check
0

krven

赞同来自:

ddddd
0

Shawn

赞同来自:

l=netstat -lnp | grep 80 | wc -l ;判断
0

tutu

赞同来自:

q
0

Rohero

赞同来自:

学习
0

ldp840611

赞同来自:

看目的地
0

仙人掌

赞同来自:

#!/bin/bash

while true
do
a=`netstat -anltp |grep httpd |awk '{print $4}' |awk -F ':' '{print $4}'`
if [ $a -eq 80 ]
then
   echo "it is OK"
else
service httpd start
echo "http has problem" |mail -s httpd xxx@qq.com
fi
sleep 30
done
0

qin521ne

赞同来自:

1
0

石头

赞同来自:

{:4_91:}
0

两天

赞同来自:

看看
0

ヾSun

赞同来自:

本帖最后由 ヾSun 于 2015-12-21 11:25 编辑
  1. #!bin/bash
  2. while [ "1" = "1" ]
  3.     do
  4.     netstat -lnp|grep :80 >80.log
  5.     if [ -s 80.log ]; then
  6.         echo ""
  7.     else
  8.         service httpd start
0

YZR

赞同来自:

明天面试,过来先补下shell
0

qin521ne

赞同来自:

#!/bin/bash
mail=123@qq.com
while :;do
        ps1=`ps aux |grep '80'>/dev/null 2>$1
        if [ -z $ps1 ];then
                echo "The 80 port is down ,please restart!" |mail -s "80_error,restart httpd." $mail
                /etc/init.d/httpd restart
        else
                echo  "The 80 port is on,don't worry."
        fi
sleep 30
done
0

蓝色风暴

赞同来自:

#!/bin/bash

service_status=`nmap -sT 121.42.176.208 | grep 'http$' | awk '{print $2}'`
date=`date`

if [ "$service_status" == "open" ];then
        echo "apache的服务运行正常" &> /dev/null
else
        /usr/local/apache4/bin/apachectl restart
        echo "$date apache服务运行不正常,重新启动apache服务" >> /tmp/error.txt
fi
0

我是学渣

赞同来自:

{:4_99:}
0

出VU时代

赞同来自:

学习了
0

Landon

赞同来自:

asd
0

hlymlv

赞同来自:

本帖最后由 hlymlv 于 2016-1-7 15:21 编辑

看看,#!/bin/bash
while :
do
netstat -lnp |grep ':80' |grep -q LISTEN
if  [ $? -eq 0 ];then
exit
else
service httpd restart
echo "httpd is not on" |mail -s "apache" 123@321.com
fi
sleep 30
done

不能比啊
0

xteplinux

赞同来自:

{:4_91:}
0

陈秋明

赞同来自:

for i in `netstat -lnp |grep 80 `; do if[ -z $i];then service httpd restart | mail -s else sleep 30 fi done
0

prospect

赞同来自:

while true
do
ss=`ps -ef | grep httpd |wc -l`
    if [[ ! $ss -gt 1 ]];then
        service httpd start       mail -s "httpd is down" xxx xxx@qq.com
    fi
sleep 30
done
0

loveyouhyf

赞同来自:

本帖最后由 loveyouhyf 于 2016-1-11 23:36 编辑

看下
0

licengceng

赞同来自:

学习
0

googleqicq

赞同来自:

{:4_92:}
0

maria

赞同来自:

{:6_148:}

  1. #~/bin/bash
  2. log=/tmp/net.log
  3. while :;
  4. do
  5.     lsof -i:80 >$log;
  6.     ztm=`grep 'httpd' $log`;
  7.     if [ -n $ztm ]
  8.     then
  9.         service httpd start;
  10.         echo `date`"Apache服务关闭了,重启!" |mail -s "重启Apache" root@localhost;
  11.     else
  12.         rm $log;
  13.     fi
  14.     sleep 30;
  15. done
0

沈诚

赞同来自:

学习一下
0

casparcc

赞同来自:

学习啦学习shell
0

Burgess

赞同来自:

学习
0

inzaghidai

赞同来自:

学习一下
0

HwangChen

赞同来自:

look look
0

skylake_

赞同来自:

学习

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: