Mehrere Methoden für den Zyklus in der Schale und Erreichbarkeitstest

 

1. Like C program

#!/bin / bash

zum ((i=1;ich<=100;i++))

tun

echo $i

getan

 

2 . Block of code

#!/bin / bash

for i in {1..100}

tun

echo $i

getan

 

3. seq mode

#!/bin / bash

für i in `seq 1 100`

tun

echo $i

getan

Result of execution

[root @ 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.

#/bin / bash

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

tun

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

wenn [ $? -eq 0 ]; dann

echo $ip is up

sonst

echo $ip is down

Sein

getan

 

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.

#/bin / bash

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

wenn [ $package != `rpm -qa | grep ^nc-` ]; dann

Echo “preparing…”

yum install nc -y >>/dev/null

sonst

Echo “nc has been installd

Sein

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

tun

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

wenn [ $? -eq 0 ]; dann

echo -e $ip is up “\c”

sshd_port=`nc -w 10 -z $ip 36566 | awk ‘{drucken $7}Wir können die Konfigurationsdatei my.cnf hinzufügen

wenn [ “$sshd_port” == “succeeded!” ]; dann

Echo “Hafen 36566 ok

sonst

Echo “Hafen 36566 Error”

fielse

echo $ip is down

Sein

getan

 

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 Error

10.8.23.43 is up port 36566 ok

10.8.24.115 is down

10.8.38.139 is up port 36566 ok

Hinterlasse eine Antwort