Scan all hosts in the network segment. Online hosts are displayed in green. Offline hosts are displayed in red;
Finally, the numbers of online and offline hosts are displayed respectively;
In order to practice shell, only the for loop is used here, although the efficiency is low, it is not used in the production environment for the purpose of practice.
#!/bin/bash########################################################################## File Name: 005.sh# Author: LookBack# Email: [email protected]# Created Time: Wed 09 Jul 2014 04:06:57 AM CST#########################################################################echo=echofor cmd in echo /bin/echo; do$cmd >/dev/null 2>&1 || continueif ! $cmd -e "" | grep -qE ‘^-e‘; thenecho=$cmdbreakfidoneCSI=$($echo -e "\033[")CEND="${CSI}0m"CDGREEN="${CSI}32m"CRED="${CSI}1;31m"CGREEN="${CSI}1;32m"CYELLOW="${CSI}1;33m"CBLUE="${CSI}1;34m"CMAGENTA="${CSI}1;35m"CCYAN="${CSI}1;36m"CQUESTION="$CMAGENTA"CWARNING="$CRED"CMSG="$CCYAN"declare -i I=0declare -i sum1=0declare -i sum2=0for ((I=0;$I <= 255;I++)); doping -c1 -W1 172.16.250.$I &> /dev/nullcheck=$?if [ "$check" = "0" ]; thenecho "${CGREEN} 172.16.250.$I ${CEND}"let sum1++elseecho "${CWARNING} 172.16.250.$I ${CEND}"let sum2++fidoneecho "${CGREEN} has $sum1 IP online. ${CEND}"echo "${CWARNING} has $sum2 IP offline. ${CEND}"
Shell script Exercise 3