码迷,mamicode.com
首页 > 系统相关 > 详细

Linux命令:AUTO PING

时间:2016-11-09 15:55:56      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:for until while

分别使用while,until和for(两种形式)循环实现。

要求:通过ping命令测试主机是否在线,
    如果在线,就显示"ip is up.",其中的IP要换为真正的IP地址,且以绿色显示;
    如果不在线,就显示"ip is down.",其中的IP要换为真正的IP地址,且以红色显示;


#!/bin/bash
#
for I in {200..201}; do
  ping -c 1 -W 1 10.109.131.$I  &> /dev/null
  [ $? -eq 0 ] && echo -e "\033[36m110.109.131.$I is up\033[0m" ||echo -e "\033[35m110.109.131.$I is down\033[0m"
done

declare J=215
while [ $J -lt 217 ] ;do
  ping -c 1 -W 1 10.109.131.$J  &> /dev/null
  PP=$?
  [ $PP -eq 0 ] && echo "110.109.131.$J is up" ||echo "110.109.131.$J is down"
  let J++
done

declare K=220
until [ $K -gt 222 ] ;do
  ping -c 1 -W 1 110.109.132.$K  &> /dev/null
  P3=$?
  [ $P3 -eq 0 ] && echo -e "\033[36m110.109.132.$K is up\033[0m" ||echo -e "\033[35m110.109.132.$K is down\033[0m"
  let K++
done


[root@JackTest ~]# ./ping.sh
110.109.131.200 is up
110.109.131.201 is up
110.109.131.215 is down
110.109.131.216 is down
110.109.132.220 is up
110.109.132.221 is down
110.109.132.222 is up

Linux命令:AUTO PING

标签:for until while

原文地址:http://wangfx.blog.51cto.com/1697877/1870975

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!