標籤:shell linux
掃描網段內的所有主機;線上的,使用綠色顯示;不線上,使用紅色顯示;
最後分別顯示:線上和不線上各有多少主機;
為了練習shell 這裡只用了 for迴圈,雖然效率低下畢竟是練習為目的不是生產環境下所用。
#!/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 指令碼練習3