壳和可达性测试的多种循环方法

 

1. 喜欢 C 程序

#!/垃圾桶

for ((我=1;一世<=100;我++))

echo $i

完成

 

2 . 代码块

#!/垃圾桶

for i in {1..100}

echo $i

完成

 

3. seq mode

#!/垃圾桶

为我在`seq 1 100`

echo $i

完成

Result of execution

[根@ AMH〜]# bash for.sh

1

2

3

4

5

6

7

.

100

 

Insert a mass of ip in /opt/ip.txt , one ip for each row,

and detect the reachability of all ip address by a shell script.

#/垃圾桶

for ip in `cat /opt/ip.txt`

ping $ip -c 2 >> /dev / null 2>&1

如果 [ $? -eq 0 ]; 然后

echo $ip is up

其他

echo $ip is down

完成

 

Result of execution

[root @ localhost〜]# bash ip_new.sh

10.8.37.67 is up

10.8.20.35 is up

10.8.23.223 is up

10.8.23.43 is down

10.8.39.19 is down

10.8.24.95 is down

10.8.20.63 is up

10.8.14.207 is down

10.8.23.59 is down

 

Enrich content of this script, confirming whether port 36566 is opening or

not if ip can ping.

#/垃圾桶

package=`rpm -qa | grep ^nc-`

如果 [ $package != `rpm -qa | grep ^nc-` ]; 然后

回声 “preparing…”

yum install nc -y >>/dev/null

其他

回声 “nc has been installd

for ip in `cat /opt/ip.txt`

ping $ip -c 2 >> /dev / null 2>&1

如果 [ $? -eq 0 ]; 然后

echo -e $ip is up “\C”

sshd_port=`nc -w 10 -z $ip 36566 | awk‘{打印 $7}重启MySQL即可生效

如果 [ “$sshd_port” == “succeeded!” ]; 然后

回声 “港口 36566 ok

其他

回声 “港口 36566 错误”

fielse

echo $ip is down

完成

 

Result of execution

[root @ localhost〜]# bash ip_new.sh

nc has been installd

10.8.38.35 is up port 36566 ok

10.8.37.63 is down

10.8.23.11 is down

10.8.39.15 is up port 36566 错误

10.8.23.43 is up port 36566 ok

10.8.24.115 is down

10.8.38.139 is up port 36566 ok

发表评论