Objective:
C language has a certain foundation of students can quickly get started under the Linux shell script application. The following script applies ping to detect the connectivity of the last server in the machine room rack. And with the necessary annotations
A simple process for creating a new shell script:
#vim filename.sh New and edit scripts
#chmod +x filename.sh Give script file Execute permissions
#./filename.sh Execute Script
Example:
ping.sh
#!/bin/bash
#
PING () {#声明一个函数PING, re-use
If Ping-c 1-w 1 $ &>/dev/null; Then #ping一次某ip, the wait time is one second. The command return value is used here as a criterion. The return value received within one second is true if the condition is determined, otherwise false.
echo "is online."
Else
echo "are down!!!!!"
Fi
}
While true; Do #利用while的无限循环来持续输出巡检结果.
For I in {100..129}; do #巡检连续的ip
{
PING 192.168.89. $I #此处为函数调用处, reusing the ping function. ' 192.168.89. $I ' as a parameter to the ping () function ($1=192.168.89. $I)
Sleep 0.5 #输出结果后睡眠半秒, give the user a viewing time.
}
done
For I in {89.243,40.1,42.1,49.1,47.1,60.1,54.1,56.1,44.1,42.3,78.1,78.9,222.222}; Do #处理不连续的ip段
{
PING 192.168. $I
Sleep 0.5
}
Done
#下面一行为完成一次shell的巡检后, give the user feedback
echo "finish! PING process would restart SECOMD later.
If you want to exit, click Ctrl + Z
"
Sleep 10
Done
Advanced version:
serping.sh
#!/bin/bash
#
PING () {
If Ping-c 1-w 1 $ &>/dev/null; Then
echo "is online."
Else
echo "are down!!!!!"
Fi
}
While true; Do
For I in {' 501E ', 2.{ 2,4,5},1.123,14.60,1.30,1.23,2.20, ' 301F ', 12. {8,5,6,7,3,2}, ' 502A ', 1.124,0.20,1. {20,25}, ' 502B ', 1.16,13.2,14.13,3.4,1.10,0.205,16.7, ' 502C ', 1.42,16. {4,9,6},13. {4,1},1.24,13.7,16.8, ' 502D ', 13.5,1.15,3.29,14.14,3.1,14. {4,7,11,12}}; Do #机架名称和ip字符放在for循环里, continuous IP segment with {} Simplified representation
{
if [${i:0:3} = = ' 502 '-o ${i:0:3} = = ' 501 '];then #取字符前面三位数比较, if 50X, is the rack number, direct output rack number
{
Echo $I
Continue #提前结束本次for循环
}
Else
{
PING 192.168. $I
Sleep 0.5
}
Fi
}
Done
echo "
finish! PING process would restart SECOMD later.
If you want to exit, click Ctrl + Z
"
Sleep 10
Done
Learn more about Shell examples and you'll be able to digest them.
Interested classmates can copy and paste the test into the shell script.
This article is from the "zgysolution" blog, make sure to keep this source http://zgysolution.blog.51cto.com/9605745/1599131
Shell script implementation of Computer room server Inspection (Preliminary)