Chinaunix首页 | 论坛 | 博客
  • 博客访问: 527347
  • 博文数量: 159
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1264
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-20 14:15
个人简介

LoveMoney

文章存档

2016年(2)

2015年(8)

2014年(148)

2011年(1)

分类: Python/Ruby

2014-05-27 14:40:07

========================================
 | |                                         案例1                                           | |
========================================
#!/usr/bash
test -e result || touch result.txt
while read ipaddr
do
 ping $ipaddr -n 1 -m 1 >>log
 if [ $? != 0 ]
 then
  echo $ipaddr " is OffLine ."
 else
  echo $ipaddr " is OnLine ."
 fi
done < hn
把需要测试主机名或者IP写入到hn文件内
系统会自动生产result.txt文件,如果该文件不存在的话
脚本解析:
#!/usr/bash
test -e result.txt || touch result.txt 判断当前文件夹下是否存在result.txt文件,如果不存在,则创建result.txt文件
while read ipaddr 从hn文件内读入一行,并赋值给ipaddr变量
do 开始执行程序
 ping $ipaddr -n 1 -m 1 >>log 测试ping 主机 -n 1是ping 1次,-m是timeout 1s,并把输出记录到log文件内
 if [ $? != 0 ] 如果ping的通就会返回0,ping不通就是>0,
 than
  echo $ipaddr " is OffLine ." >>result.txt ping的结果不等于0.说明无法ping通该主机,并把结果写入result.txt文件
 else
  echo $ipaddr " is OnLine ." >>result.txt ping的结果等于0.说明可以ping通该主机,并把结果写入result.txt文件
 fi
done < hn 读入hn文件内的主机名
========================================
| |                                         案例2                                           | |
========================================

此脚本用于检查192.168.1.100到192.168.1.200之间的主机是否alive。脚本如下:

#!/bin/bash
#Checks to see if hosts 192.168.1.100-192.168.1.200 are alive
for n in {100..200}; do
         host=192.168.1.$n
         ping -c2 $host &>/dev/null
         if [ $? = 0 ]; then
                      echo "$host is UP"
         else
                      echo "$host is DOWN"
         fi
done 

※提示:注意$?与=之间必须空格。




阅读(989) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~