According to Brother Bird's ping script, I changed it a bit. Used knowledge: Read, Linux read by line, shell output effect adjustment, etc.
Where Linux reads files by line is more important, you can see the link
1 Scripting Features:
Bulk Ping the IP address in the file under the current path, the IP address must be filled in one line
2 Note:
Follow the prompts to run the script to ensure that the script has execute permissions and cannot run on window
#!/bin/bash# program# usePingcommand to check the network's PC state.# history# ./Geneva/ -Learn Bird # environment variable path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/Binexport Pathfilepath=`pwd' FILENAME=Mlgbmethod=1#使用ping确认网络是否通functionfun_ping {Ping-C1-W 1${ipaddress} &>/dev/NULL&& result=0|| result=1# start to show the result is correct display (UP), no connectivity (down)if["$result"==0]; ThenEcho "${ipaddress}, is up."|Tee-A ${filepath}/Pingresult.csvElseEcho "${ipaddress}, is down."|Tee-A ${filepath}/Pingresult.csvfi} #循环读取文件2慢速法functionWhile_read_line_bottm () {if[-F ${filepath}/${filename}]; Then>${filepath}/Pingresult.csv whileRead IPADDRESS Dofun_ping Done< ${filepath}/${filename}Echo-E"\033[31;1m execution completed, results in ${filepath}/pingresult.csv \033[0m"Else Echo-E"\033[41;37;5m file ${filepath}/${filename} \033[0m not found"fi} #循环读取文件1快速法functionFor_in_file () {if[-F ${filepath}/${filename}]; Then>${filepath}/Pingresult.csv forIPADDRESSinch`Cat${filepath}/${filename} ' Dofun_ping Done Echo-E"\033[31;1m execution completed, results in ${filepath}/pingresult.csv \033[0m"Else Echo-E"\033[41;37;5m file ${filepath}/${filename} \033[0m not found"fi} Case " $" inch Help|--help|?) Echo "Usage: This script is used to ping the IP in the file, make sure that the file exists, and that the IP is one lineThere are two ways to run:1 SHScript name;2./after execution of the script name is completed, the result of the ping is stored in the Pingresult.csv file under the current path. "Exit1Esac#输入参数和基本验证 # Clean up the screenEcho-E"\033[2j \033[31;1m"#调整显示样式 #Echo-E"\033[31;1m"Echo "Make sure that the IPAddress files and scripts are in the same path"Read-T --P"Please enter a file name for IPAddress:"Filenameread-TTen-P"Please select execution speed, 1 fast, 2 slow:, default = 1:"method# Restore default display styleEcho-E"\033[0m"#主函数 Case "$METHOD" inch 1) Timefor_in_file;; 2) TimeWhile_read_line_bottm;; *) Echo-E"\033[41;37;5m You fill the wrong number, Lao Zi according to the slowest to you to perform \033[0m" TimeWhile_read_line_bottmEsac
Click to view Code
Another record: When the Linux function uses the return command, the value of return code cannot exceed 255, and once it is exceeded, it continues to be calculated starting from 0
Reference:
Linux reads files by line
Change the output of the Linux shell [font color, style, etc.]
Linux Read usage
Linux Batch ping script