9月27日shell脚本练习题

回复 收藏
设计一个脚本,监控远程的一台机器(假设ip为123.23.11.21)的存活状态,当发现宕机时发一封邮件给你自己。

提示:
1. 你可以使用ping命令   ping -c10 www.baidu.com
2. 发邮件的命令是  echo "邮件内容" |mail -s "主题" abc@139.com
3. 脚本可以搞成死循环,每隔30s检测一次  
2013-09-27 11:34 举报
已邀请:
0

Hello_Lemon

赞同来自:

#!bin/bash
#设计一个脚本,监控远程的一台机器(假设ip为123.23.11.21)的存活状态,当发现宕机时>发一封邮件给你自己。

#ipaddr
ip=123.23.11.21
#mail
ma=abc@139.com

while :;do
        pa=`ping -c5 $ip|grep 'receive'|awk -F ',' '{print $2}'|awk '{print $1}'`
        if [ $pa=="0" ];then
                echo "The host $ip is die"|mail -s "message" $ma
        fi
        sleep 30
done
0

第六感

赞同来自:

#!/bin/bash
#检查宕机
#20130927

while :;do
ping -c10 123.23.11.21>/dev/dull
if($?!=0)
then
echo "邮件内容" |mail -s "主题" abc@139.com
fi
sleep 30
done
0

地平线

赞同来自:

本帖最后由 地平线 于 2013-9-27 15:15 编辑

#!/bin/bash
ip='123.23.11.21'
while true
   do
   sleep 30
   ping -c 10 $ip
   if [[ $? == 0 ]];then
   :
   else
   echo "$ip" |mail -s "error" abc@139.com
   fi
done
0

乐宝儿

赞同来自:

# bin/bash

while : ; do
ping -c10 www.baidu.com > 1.txt
if [ -z 1.txt ] ;then echo "dangji" |mail -s "baidudangji" abc@139.com
fi  
sleep 30
done

0

hkj8808

赞同来自:

#!/bin/bsah
ip="123.23.11.21"
ping -c 4 $ip >/dev/null
a=$?
while : ;do
    if [ $a -ne 0 ];then
          echo "$ip is not alive" |mail -s "test" 123@139.com
    fi
    sleep 30
done

0

不断更新

赞同来自:

#!/bin/bash
while :; do
if `ping -c 10 www.baidu.com>/dev/null 2>&1`;then
echo "ok" >/dev/null
else
echo "down" | mail -s baojing 891287870@qq.com
sleep 30
fi
done
0

ocean

赞同来自:

[root@OceanV sh]# cat check_ping.sh
#!/bin/bash
IP=192.168.9.99
while true
do
ping -c 10 $IP > /dev/null
if [ $? -eq 0 ]
then
echo "`date +%F-%H:%M:%S`   $IP is OPEN" >> /root/sh/check_ping.log
else
echo "`date +%F-%H:%M:%S`   $IP is CLOSE" |mail -s "check_ping" root@localhost
echo "`date +%F-%H:%M:%S`   $IP is CLOSE" >> /root/sh/check_ping.log
fi
sleep 30
done
0

Budweiser小王子

赞同来自:

练习
0

luzhen521

赞同来自:

0

luzhen521

赞同来自:

0

齐天大圣

赞同来自:

#!/bin/bash
##通过ping查看一台机器是否存活,说过没有存活则发送邮件!

while :;
do
  ping -c10 www.baidu.com
  if [ `echo $?` -eq '0' ];then
     sleep 10
  else
     echo "zhe jiqi is down!" |mail -s "DOWN" lichao@jiangmin.com
  fi
done
0

zyfeifie

赞同来自:

#!/bin/bash
while true
do
num=`ping 192.168.2.205 -c 10 | grep "packet loss" | awk '{print $6}' | awk -F "%" '{print $1}'`
if [ $num -eq 0 ];then
echo "the server is not alive" | mail -s "the server is down" zyfeifei@139.com
fi
sleep 30
done
0

Louis

赞同来自:

#!/bin/bash
## This script is for ping to check a remote machine if alive.
## Writed by Louis on 2014/08/29 17:00

while :;do
    echo `ping -c10 www.baidu.com > ping.log`
    ping=`grep '100% packet loss' ping.log|awk -F ',' '{print $3}'|awk '{print $1}'`

    if [ ! -z $ping ]; then
        echo "www.baidu.com is time out!"|mail -s "Ping check" root@localhost
    fi
    sleep 30
done
0

游夜

赞同来自:

本帖最后由 游夜 于 2014-9-4 22:58 编辑

#!/bin/bash
while :;doping -c10 www.baidu.com
if [  echo $? -ne 0 ];then
        echo "Your Website www.baidu.com is down.Please check it"|mail -s "Website Down" 275235245@qq.com
fi
sleep 30
done


0

寒一

赞同来自:

#! /bin/bash

n=`ping -c10 123.23.11.21|wc -l`
while :; do
    if [ $n -gt 10 ]; then
        echo "The network is connected"
    else
        echo "This machine is down" |mail -s "outage" abc@139.com
    fi
    sleep 30
done
0

wyatt88

赞同来自:

#!/bin/bash
while sleep 30
do
rec=`ping -c10 123.23.11.21 | grep 64`
if [ -z $rec ]
then
echo "21 is down" | mail -s "Alarm" abc@139.com 2>log
else
echo "21 is up" > log
fi
done
0

ocean

赞同来自:

cat check_active.sh
#!/bin/bash
# Desc: this scripts is to check host staus , if host has downed then send mail to the user;
# Author:  Jeffery.Su
# Date:    9/9/2014
# version: V1

IP_ADDR=`ifconfig -a | grep "inet addr"| awk '{print $2}'|awk -F: '{print $2}'|head -1`
while :;
do
   NET_ACTIVE=` ping -c3 $IP_ADDR| grep received|awk -F"," '{print $2}'|awk '{print $1}' `
if [ $NET_ACTIVE == 0  ];
   then
   echo "Host $IP_ADDR is down"|mail -s "Host $IP_ADDR is down" 781303205@qq.com
else
   echo "The host is active"
fi
sleep 10;
done{:5_121:}
0

coffee

赞同来自:

#!/bin/bash

while :
do
        sleep 30
        ping -c5 123.23.11.21
        if [ $? -eq 0 ] ;then
                echo "host is up"
         else
                echo "host is down" |mail -s "ping" abc@139.com
        fi

done


0

nihao426181

赞同来自:

^^^^^^^^^^^^^^^^^^^^6
0

nihao426181

赞同来自:

好像感觉很复杂一样,可能是基础太差了吧{:5_135:}
0

huifeidexiaxia

赞同来自:

学习了
0

崔志朋

赞同来自:

                     $!=0
0

pykihwfn

赞同来自:

这个要学习下。。。。。先看下铭哥的脚 本。
0

王靖

赞同来自:

kankan
0

王靖

赞同来自:

本帖最后由 王靖 于 2014-11-22 17:19 编辑

#! /bin/bash  while :  ;do
     /bin/ping -c 1 -W 1 www.baidu.com &>/dev/null
if [ $? -ne 0 ] ;then
     echo "www.baidu.com down" | mail -s "host donw" abc@139.com
fi
sleep 2
done
0

王肖强

赞同来自:

  1. #!/bin/bash
  2. # The monitoring host survival script.
  3. # Writen by Wangxiaoqiang 2014-11-24.

  4. HOST=xxx.xxx.x.xx
  5. MAIL=xxxxxxxxxxx@xxx.com

  6. while true
  7.   do
  8.     ping -c 10 $HOST > /dev/null
  9.     if [ $? -ne 0 ]
  10.       then
  11.         echo "HOST:$HOST down." | mail -s "MAIL FOR $HOSTNAME" $MAIL
  12.     fi
  13.     sleep 30
  14. done

  15. # End
0

aqi

赞同来自:

#!/bin/bash
ip="123.23.11.21"
while :
do
        ping -c10 $ip
        if [[ $? != 0 ]]
        then
                echo "$ip is dead" | mail -s "$ip is dead" abc@139.com
        fi
        sleep 30
done
0

阳光

赞同来自:

#!/bin/bash  #author: GY #date:2014-11-29   while :; do          ping -c 10 192.168.0.99          if [ `echo $?` -eq 0 ];then                 sleep 30         else                 echo "192.168.0.99 is shutdown!" | mail -s "shutdown" 445043972@qq.com                 sleep 5                 break         fi done
0

阳光

赞同来自:


#!/bin/bash

#author: GY
#date:2014-11-29


while :; do

        ping -c 10 192.168.0.99

        if [ `echo $?` -eq 0 ];then
                sleep 30
        else
                echo "192.168.0.99 is shutdown!" | mail -s "shutdown" 445043972@qq.com
                sleep 5
                break
        fi
done

刚才是快速回复,格式全乱了。现重新发表回复
0

dantes

赞同来自:

1111111111
0

zhangw

赞同来自:

学习下
0

川娃子在大连

赞同来自:

本帖最后由 川娃子在大连 于 2014-12-16 20:50 编辑

#!/bin/bash
# -------------------------------------------------------------------------------
# Filename:    amingtest4.sh
# Revision:    1.0
# Date:        2014/12/16
# Author:      FUQIANG LI
# Email:       317377106@qq.com
# Notes:       aminglinux shell exercises of 2013-09-27
# -------------------------------------------------------------------------------

read -p "Please input a host IP: (end for exit) " hostIP
while :
do
        if [ $hostIP == "end" ]
                then
                        exit 0
                else
                        ping -c10 $hostIP &>/dev/null
                        if [ $? -eq 0 ]
                                then
                                        echo "the hostIP $hostIP is up!"
                                else
                                        echo "the hostIP $hostIP is down!"| tee warming.log |mail -s "warming" 317377106@qq.com                            fi
                fi
        sleep 30
done
0

川娃子在大连

赞同来自:

本帖最后由 川娃子在大连 于 2014-12-17 00:29 编辑

#!/bin/bash
# -------------------------------------------------------------------------------
# Filename:    amingtest4.sh
# Revision:    2.0
# Date:        2014/12/17
# Author:      FUQIANG LI
# Email:       317377106@qq.com
# Notes:       aminglinux shell exercises of 2013-09-27
# -------------------------------------------------------------------------------
read -p "Please input a host IP: (end for exit) " hostIP
while :
do
        if [ $hostIP == "end" ]
        then
                exit 0
        elif ping -c5 $hostIP &>/dev/null
        then
                echo "the hostIP $hostIP is up!"
        else
                echo "the hostIP $hostIP is down!"|tee ./warming.log|mail -s "warming" 317377106@qq.com < ./warming.log                                      
        fi
        sleep 30
done
0

崔志朋

赞同来自:

#!/bin/bash # Written 2014-12-22  cuizhipeng  while true do m=`ping -c10 www.baidu.com | grep 'time out'` if [ -n $m ] then   echo "Please up system" fi sleep 30 done
0

Mё飘逸d檒

赞同来自:

看看答案
0

zq13054480665

赞同来自:

看看
0

2422606568

赞同来自:

好好学习
0

2422606568

赞同来自:

看看
0

cmzsteven

赞同来自:

{:4_107:}
0

cmzsteven

赞同来自:

本帖最后由 cmzsteven 于 2015-2-7 20:57 编辑

ip=123.23.11.21
while :; do
        info=`ping -c10 $ip |grep '100% packet loss'|wc -l`
        if (($info==1)); then
                echo "Your host is halt! Host IP is $ip" |mail -s "Host is halt" abc@139.com
        fi
        sleep 30
done
如果不用死循环的话,可以加入到crontab中执行

如果提示:
send-mail: warning: valid_hostname: numeric hostname: 5
send-mail: fatal: file /etc/postfix/main.cf: parameter mydomain: bad parameter value: 5

请安装sendmail
yum install -y sendmail

0

wuhen

赞同来自:

看看。。。
0

wuhen

赞同来自:

本帖最后由 wuhen 于 2015-2-18 18:49 编辑
  1. #!/bin/bash
  2. while :;
  3. do
  4. ip=`ping -c 10 www.baidu.com|grep 'received'|awk '{print $4}'`
  5. if [ $ip -eq 10 ]
  6. then
  7. echo "network normal"
  8. else
  9. echo "network abnormal"|mail -s "network flase" abc@139.com
  10. fi
  11. sleep 30
  12. done
0

qq20847697

赞同来自:

#!/bin/bash
#
while :; do
ping -c10 123.23.11.21 > /dev/null || mail -s "123.23.11.21 is down" abc@139.com
sleep 30
done
0

qq20847697

赞同来自:

答案在哪里呀
0

hangtiangazi

赞同来自:

顶。。。
0

hangtiangazi

赞同来自:

顶铭哥。。。。
0

wangdi244

赞同来自:

#!/bin/bash
#
while true
do
ping 192.168.1.108 -c 1 -w 1
if [ $? == 0 ];then
echo "192.168.1.108 is up"
else
echo "wrong ping"|mail -s "wrong ping" root@localhost.localdomain
echo "192.168.1.108 is down"
fi
sleep 30
done
0

soo

赞同来自:

看看
0

ztonglinyx

赞同来自:

学习学习  
0

sss

赞同来自:

本帖最后由 sss 于 2015-6-5 15:31 编辑
  1. #!/bin/bash


  2. while :
  3. do
  4.         ping -c 123.23.11.21 >/dev/null 2>&1
  5.         if [ $? != 0 ]
  6.         then
  7.         echo "shutdown shutdown shutdown" | mail -s "123.23.11.21 Downtime" xxxxxxxxxx@qq.com
  8. fi
  9. sleep 30
  10. done

0

llzdwyp

赞同来自:

#!/bin/bash
while :
do
  if p=`ping -c2 123.23.11.2|grep "^[^64]"`
  then
      echo "网络不通" |mail -s "检查网络" abc@139.com
  fi
sleep 30
done
0

ayuan124

赞同来自:

先看看
0

wl11930728

赞同来自:

铭哥这个有答案么?
0

wl11930728

赞同来自:

没有答案么?
0

翟厚翔

赞同来自:

for ip in `cat 4.log`    do      echo $ip      ssh $ip      ping -c10 www.daidu.com     done echo " ip is $ip service  down .please deal with it right now"|mail -s "service is down  "  18856066058@139.com
0

此间的少年

赞同来自:

mark
0

大仔黑黑

赞同来自:

。。。。。。
0

chenqi

赞同来自:

查看
0

chenqi

赞同来自:

  1. #!/bin/bash
  2. while :;do
  3.         ping -c5 -w5 61.147.124.57 > /dev/null 2>&1
  4.         if [ $? -ne 0 ];then
  5.         echo "DST WAS DOWN at `date +%T`"
  6.         else
  7.         echo " DST IS ALIVED"
  8.         fi
  9.         sleep 5
  10. done


这样的思路可行么
0

xebszw

赞同来自:

1
0

liang1990

赞同来自:

while :;do
a=`ping -c5 www.baidu.com | grep packet|awk '{print $4}'`
if[$a -eq 0 ];then
   echo "邮件内容" |mail -s "主题" abc@139.com
else
   sleep 3000
fi
done
0

shajoe

赞同来自:


while :
do
ping -c10 www.baidu.com
a=`echo $?`
if [ $a != 0 ]
then
echo "192.168.153.130 ping is down" |mail -s "153.130 is down" 13@mail.com
else
echo "server is up"
fi
sleep 30
done
0

ifconfig2

赞同来自:

1
0

ifconfig2

赞同来自:

1
0

汤小东

赞同来自:

check
0

krven

赞同来自:

dfd
0

J!_yuan

赞同来自:

kankan
0

J!_yuan

赞同来自:

while :  do         sleep 30         pinglog=`ping -c10 www.baidu.com|grep '^10'|awk -F',' '{print $1,$2}'|sed 's/[a-z]//g'|sed 's/ /\n/g'|grep '^[0-9]'`         echo '$pinglog'         for lines in `echo $pinglog`          do                 if [$lines -eq '11'] ;                 then    echo"hello"                 else                         echo"ok"                 fi done done
0

Rohero

赞同来自:

kankan
0

仙人掌

赞同来自:

while true
do
ping -c 5 123.23.11.21
a=`echo $?`
if [ $a -ne 0 ];then
echo "problem" |mail -s "ip had  problem" wwww@xx.com
fi
sleep 30
done
0

ヾSun

赞同来自:

看一下
0

wanjj

赞同来自:

0

蓝色风暴

赞同来自:

#!/bin/bash

ping -c 4 192.168.1.1 > /tmp/1.txt
add=`grep 'transmitted' /tmp/1.txt | awk '{print $4}'`
if [ "$add" -ne "4" ]
        then
        echo "你的服务器挂掉了!!" | mailx -s 'aliyun' yu-yan-good@163.com < /tmp/1.txt
        rm -rf /tmp/1.txt
fi

0

出VU时代

赞同来自:

看一下
0

369666951

赞同来自:

  1. #!/bin/bash
  2. #设计一个脚本,监控远程的一台机器(假设ip为123.23.11.21)的存活状态,当发现宕>机时发一封邮件给你自己。
  3. #
  4. #提示:
  5. #1. 你可以使用ping命令   ping -c10 www.baidu.com
  6. #2. 发邮件的命令是  echo "邮件内容" |mail -s "主题" abc@139.com
  7. #3. 脚本可以搞成死循环,每隔30s检测一次  
  8. while :
  9. do
  10.      n=` ping -c10 www.baidu.com | grep 'received'|awk -F ',' '{print $2}'|awk '{if ($1==0) print "0" ;else print "1"}'`
  11.      if [ $n == 0 ]
  12.      then
  13.     # echo "服务器挂了" | mail -s "检测服务器运行状态" abc@139.com
  14.      echo "服务器挂了"  
  15.      else
  16.      echo "服务器正常运行"
  17.      fi  
  18.      sleep 30s
  19. done
0

Landon

赞同来自:

这个简单
0

hlymlv

赞同来自:

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

#!/bin/bash
while :
do
ping -c10 123.23.11.21 > /dev/null
if [ $? -ne 0 ];then
echo "bad" |mail -s "linux" abc@139.com
fi
sleep 30
done
这样行吗 ?
0

陈秋明

赞同来自:

#! /bin/bash while :;do m=`ping -c10 www.baidu.com | wc -l`; if [ $m -eq 15 ] then echo "the PC is shutdown" | mail -s "tixing" 984053525@qq.com fi done else sleep 30
0

陈秋明

赞同来自:

#! /bin/bash while :;do m=`ping -c10 www.baidu.com | wc -l`; if [ $m -eq 15 ] then echo "the PC is shutdown" | mail -s "tixing" 984053525@qq.com fi done else sleep 30
0

陈秋明

赞同来自:

#! /bin/bash while :;do m=`ping -c10 www.baidu.com | wc -l`; if [ $m -eq 15 ] then echo "the PC is shutdown" | mail -s "tixing" 984053525@qq.com fi done else sleep 30       #没有做好
0

HMOM

赞同来自:

本帖最后由 HMOM 于 2016-1-8 00:17 编辑

ip=11.11.11.11
ping $ip -c10
if [ $? eq 0 ];then
          echo "ok" &>/dev/null
else
           echo "your webserver is down!!" | mail -s "warning"
123@qq.com
fi
sleep 30

0

拉卡

赞同来自:

#!/bin/bash
## 监控主机存活状态
i=`ping -c10 172.28.68.31 |grep 'packets' |awk 'OFS=" [ ]+" {print $6}' |sed 's/%//g'`
if [ $i -gt 0 ]
then
        ping -c10 172.28.68.31 > /root/1.txt
        cat /root/1.txt |mail -s "The server was died! HaHa" xm.cheng@sunmedia.com.cn
fi
0

loveyouhyf

赞同来自:

#!/bin/bash
##监控远程的一台机器(假设ip为123.23.11.21)的存活状态,当发现宕机时发一封邮件给你
##write by 2016-01-08
read -p "please enter a ip address: " ip
if [ -n "$ip" ]&&[ -z `echo "$ip"|tr -d "."|sed 's/[0-9]//g'` ];then
while :;do
##ping -q $a -c 2
if ping $ip -c1|grep -q "ttl=";then
echo -e `date +%F" "%T`"\t$ip is good!"
sleep 10
continue
else
echo  "ping [$ip] failed,please check ! "
echo  "ping [$ip] failed,please check ! " |mail -s "$ip bad" abc@139.com
exit
fi
done
else
echo "输入值为空或不是正确的ip址址,请重新输入"
fi
0

prospect

赞同来自:

#!/bin/bash

while true
do
    res=`ping -c5 123.23.11.21 | grep packet |awk '{print int($6)}'`
    if [[ $res -eq 100 ]];then
        echo "123.23.11.21 is down" |mail -s "down" abc@139.com
    fi
sleep 30
done
0

lyhabc

赞同来自:

看答案  
0

lyhabc

赞同来自:


#!/bin/bash
#write by 2016-2-2


ping_fun(){
if ping -c $1 $2 > /dev/null 2>&1
then
return 0
else
return 1
fi
}

host=192.168.12.253
count=2

while :
do
ping_fun $count $host
if [ $? -eq 0 ]
then
echo "$host is up"
else
echo "$host  is down " |mail -s "$host is down " abc@139.com
fi
sleep 3
done
0

bbcw

赞同来自:

#!/bin/bash
ping -c3 -w10 www.baidu.com >/dev/null 2>/dev/null
PING=`echo $? `
while :
do
if [ $PING -eq 0 ]
then
    echo "123.23.11.21 一切正常"
else
    echo "123.23.11.21Server downtime"|mail -s "downtime" abc@139.com
fi
   sleep 30
done
0

gxp2008

赞同来自:

本帖最后由 gxp2008 于 2016-3-1 21:09 编辑

#!/bin/bash
a=`ping -c10 www.baidu.com|grep '% packet loss' |awk '{print $6}' |sed 's/%//'`
if [$a -eq 0]
then      
    echo "host is down" |mail -s "host is down" abc@139.com      

else      
    echo "the packet not loss"
fi
0

sy0258

赞同来自:

kk
0

sy0258

赞同来自:

kk
0

木头爱木头媳妇

赞同来自:

1
0

andyaaa

赞同来自:

1
0

Linuxpp

赞同来自:

look
0

阿凯

赞同来自:

while :
do
ping -c10 123.23.11.21
if [ $? -ne "0" ]
then
        echo "The server is down." |mail -s "down" abc@139.com
fi
        sleep 30
done
0

alvinnull

赞同来自:

答案在哪里啊
0

a_leon

赞同来自:

没写出来,看看答案
0

stone

赞同来自:

while :; do if `ping -c 1 www.baidu.com |grep -q "error"`; then echo "邮件内容" |mail -s "主题" abc@139.com; fi ; sleep 30; done
0

monga

赞同来自:

学习
0

拉卡

赞同来自:

#!/bin/bash
##监控主机是否存活
packet=`ping -c10 123.23.11.21 |grep 'packet' |awk {print $6} |sed 's/%//g'`
whlie [ $packet -eq 100 ]
do
        echo "Your Server is lost" |mail -s "monitor" abc@123.com
        sleep 30
done
0

smatch

赞同来自:

本帖最后由 smatch 于 2016-5-30 21:41 编辑

#!/bin/bash
while
do
        ping -c10 123.23.11.21
        a = `echo $?`
        if [ $a -eq 0 ];then
                echo
                sleep 30
        else
                echo "宕机" |mail -s "宕机" abc@123.com
        fi
done

回复帖子,请先登录注册

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