一.实验要求:

1.对于给定范围的ip地址进行主机在线测试

  2.ping测试结果保存在/tmp/ping目录下以时间戳为文件名的文件里,以便后续查看

二.实验过程:

#!/bin/env/ bash
#

#variable declaration
declare -a start_arr
declare -a end_arr

#define function
#IP address initialization
initial_fun(){

  read -p "Initial Address:" host_start
  read -p "End Address:" host_end
  echo -e "Your test segment is : \033[31m${host_start} ~ ${host_end}\033[0m"
}

#Input validation
confirm_fun(){
  read -p "Confirm(y);Exit(q);Re-Enter(r): " ack
  ack=${ack:="y"}
  while [ $ack != "y" -a $ack != "q" -a $ack != "r" ];do
  read -p "Confirm(y);Exit(q);Re-Enter(r):" ack
  done
}

#IP segmentation
Ip_field(){
  local i
  for ((i=1;i<=4;i++));do
    start_arr[$[i-1]]=`echo $host_start | cut -d. -f $i`
    end_arr[$[i-1]]=`echo $host_end | cut -d. -f $i`
  done
}

#C class test
Cping_fun(){
  local cnet=$1
  local i=$2
  local j=$3
  while [ $i -le $j ];do
    if ping -W 1 -c 1 $cnet.$i &> /dev/null;then
      echo "$cnet.$i is up." | tee -a /tmp/ping/`date +%F-%H\:%M`.ping
    else
      echo "$cnet.$i is down."
    fi
    trap 'exit 1' INT
    let i++

  done
}

#B class test
Bping_fun(){
  local bnet=$1
  local i=`echo $2 | cut -d. -f1`
  local I=`echo $2 | cut -d. -f2`
  local j=`echo $3 | cut -d. -f1`
  local J=`echo $3 | cut -d. -f2`
  if [ $i -eq $j ];then
    Cping_fun $bnet.$i $I $J
  else
    while [ $i -le $j ];do
      Cping_fun $bnet.$i 0 254
        let i++
    done
  fi

}

#main
cat << EOF
Host detetion address vaild range as follows:
B)| 128.0.0.0 ~~ 191.255.255.255
C)| 192.0.0.0 ~~ 223.255.255.255
Please enter your initial address and end address segment detection
such as:192.168.1.1 192.168.1.99
EOF
[ ! -d /tmp/ping ] && mkdir /tmp/ping
initial_fun
confirm_fun
until [ $ack == "y" ];do

  if [ $ack == "q" ];then
    exit 1
  else
  initial_fun
  confirm_fun
  fi

done
[ $ack == "y" ]&& echo "Test is starting..."
Ip_field
if [ ${start_arr[0]} -ge 192 -a ${start_arr[0]} -le 223 ];then
  net_addr=${start_arr[0]}.${start_arr[1]}.${start_arr[2]}
  flag_start=${start_arr[3]}
  flag_end=${end_arr[3]}
  Cping_fun $net_addr $flag_start $flag_end
elif [ ${start_arr[0]} -ge 128 -a ${start_arr[0]} -le 191 ];then
  net_addr=${start_arr[0]}.${start_arr[1]}
  flag_start=${start_arr[2]}.${start_arr[3]}
  flag_end=${end_arr[2]}.${end_arr[3]}
  Bping_fun $net_addr $flag_start $flag_end
else
  echo "Sorry,the detection of such addresses is not supported for the time being."
fi

三.实验总结:递推

四.实验思考:

  1.如何进行A类地址测试

      2.如何解决超址范围的ip引出的bug

  3.能不能给定三个参数,起始ip,结束ip,子网掩码编写个更优的测试脚本

  

ping探测在线主机的更多相关文章

  1. sh 测试网段在线主机

    yum install nmap nmap -sP  192.168.21.1/24 查看网段在线主机 grep -vwf file1 file2 文件内容比较 #!/bin/bash # day=` ...

  2. 求局域网内所有在线主机的ip

    在一个局域网内,已知其中一台主机的ip为192.,子网掩码为255.,求所有其他在线主机的ip. shell 编码实现 #!/bin/bash netWorkIP=. ) do $netWorkIP$ ...

  3. 批量ping 检测linux主机是否可以通

    批量ping 检测linux主机是否可以通 # 1.配置列表 [root@db137 liweiwie]# cat /home/dbatlbb/script/liweiwie/ping_ip.txt ...

  4. 解决ping不通win7主机

    之前在路由器上ping笔记本发现ping不通,但是笔记本ping路由器通,也没多想.今天想起来可能是win7的防火墙作怪,以前上课虚拟机好像也是ping不通宿主机,但是宿主机能ping通虚拟机. 简单 ...

  5. 连续4个小时ping不通远端主机,则本机关机

    #!/bin/bash #连续4个小时ping不通远端主机,则本机关机. begin_time=$(date "+%s") while true;do sleep 2 ping - ...

  6. VMware的linux虚拟机配置ip后无法ping通宿主机

    VMware的linux虚拟机配置ip(使用eth0)后无法ping通宿主机,同样宿主机无法ping通linux虚拟机. 可能原因:linux虚拟机使用的网卡,与本机使用的网卡不同,配置成与本机一致的 ...

  7. 运维派 企业面试题4&5 创建10个 用户 ; ping探测主机是否在线

    Linux运维必会的实战编程笔试题(19题) 企业面试题4: 批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串). #!/bin/bash # ;i<=; ...

  8. ping探测与Nmap扫描

    一.实验目的 学习信息收集的一般步骤 学会使用ping命令 利用Nmap工具进行信息搜集 二.实验环境 系统环境:一台windows7系统.一台XP系统.一台kali系统 软件环境:安装Wiresha ...

  9. cnentos中进行bond网卡配置,一切配置无问题,就是ping不通宿主机

    服务器网口绑定   1. ifcfg-bond0   DEVICE=bond0 ONBOOT=yes IPADDR=192.168.100.64 NETMASK=255.255.255.0   2. ...

随机推荐

  1. 测试 code style

    c++ #include <iostream> int main(int argc, char *argv[]) { /* An annoying "Hello World&qu ...

  2. CF-1140 E - Palindrome-less Arrays

    题意:给定一个没有填完的序列,数值为-1表示你可以用 1~k 中的数字去覆盖它,求将该序列填充后,不存在长度为奇数的回文串的方案数 分析: 使之不存在长度为奇数的回文串,只需要满足不存在长度为3的回文 ...

  3. 如何用纯 CSS 创作一个行驶中的火车 loader

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/RBLWzJ 可交互视频 ...

  4. settings.py常规配置项

    settings.py常见配置项 1. 配置Django_Admin依照中文界面显示 LANGUAGE_CODE = 'zh-hans' 2. 数据库配置(默认使用sqlite3) 使用MySQL的配 ...

  5. 购物车小程序(while循环,列表)

    while True: salary = input("please input your salary:") if salary.isdigit(): salary=int (s ...

  6. Python入门学习笔记2:刷题

    1) LeetCode 强的面试题和算法题,要求也比较高,很多国内外的码农在上面刷题.难度从easy到hard都有,而且覆盖面极广,需要你的综合实力去答题. 最简单的题比如字符串的处理有的时候也要用到 ...

  7. UVALive - 3942 Remember the Word (Trie + DP)

    题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...

  8. ACM 贪心算法总结

    贪心算法的本质: 就是当前状态的最优解,它并不考虑全局. 什么是当前状态的最优解? 成本问题? https://www.cnblogs.com/xuxiaojin/p/9400892.html (po ...

  9. Post页面爬取失败__编码问题

    python3爬取Post页面时, 报以下错误 "POST data should be bytes or an iterable of bytes. It cannot be of typ ...

  10. ogre3D程序实例解析1-平移旋转与缩放

    接着上篇写 http://www.cnblogs.com/songliquan/p/3294902.html 旋转 这里有必要看一下关于旋转的源代码:    virtual void pitch(co ...