ping batch ip and display results

Keywords: socket network ssh

Make a record to avoid forgetting. Don't spray)

Type 1:
Ping the ip in a file. If you can ping up, if you can't Ping down

#! /bin/bash
for ip in `cat iplist`
do
ping=`ping -c 1 $ip|grep loss|awk '{print $8}'|awk -F "%" '{print $1}'`
if [ $ping -eq 100  ];then
echo $ip is down 
else
echo $ip is up
fi
done

Explain:

for ip in `cat iplist`  #This is to read the IP in the iplist file one by one. It's better to write one line for each IP
ping=`ping -c 1 $ip|grep loss|awk '{print $8}'|awk -F "%" '{print $1}'`
#ping the read IP once to see if it is OK. And take the value of the percentage of loss, that is, the value of the packet, to see whether it is a packet
if [ $ping -eq 100  ];then #Check whether the value of the packet is 100. If it is 100, it means all the packets are dropped. If it is not 100, it means no ping pass. Then it means ip is down. If it is not 100, it means ping pass. Then it means ip is up

Type 2: ping the hosts in a network segment to show which are connected and which are not.

#!/bin/bash
for ip in `seq 1 255`
do
ping=`ping -c 1 $ip|grep loss|awk '{print $8}'|awk -F "%" '{print $1}'`
if [ $ping -eq 100  ];then
echo 192.168.1.$ip is down 
else
echo 192.168.1.$ip is up
fi
done

Explain the above script. The difference is the following line:

for ip in `seq 1 255`   #Is an integer between 1 and 225 (including 1 and 225)

The simple point is:

#!/bin/bash
for ip in `seq 1 255`
do
ping -c 1 192.168.1.$ip > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo 192.168.1.$ip is UP
else
echo 192.168.1.$ip is DOWN
fi
done
wait

Type 3: expand one to view CPU information:
The Ip that needs to check the CPU information is written to the iplist file in advance (note the directory of this file, this blog post is to put the iplist and script in the same directory, and run and script in the way of. / cpuinformation.sh to this directory).

#!/bin/bash
echo "IP CPU information   CPU(s)       Core(s)         Socket(s)"      >> listcpu
for ip in `cat iplist`
do
ping -c 1 $ip > /dev/null 2>&1
if [ $? -eq 0 ]; then
ssh $ip
A=`lscpu | grep '^CPU(s):' | awk -F ":" '{print$2}'`
B=`lscpu | grep 'Core(s) per socket:' | awk -F ":" '{print$2}'`
C=`lscpu | grep 'Socket(s):' | awk -F ":" '{print$2}'`
echo "$ip $A $B $C" >> listcpu
else
echo $ip is DOWN
fi
done
wait

The result is output to cpulist:
The output is:

IP CPU information   CPU(s)     Core(s)         Socket(s)
192.168.*.**                 1     1              1
192.168.*.**                 1     1              1
192.168.*.***                1     1              1
192.168.*.***                1     1              1

If you want to see other parameters of the CPU, just add variables.

Posted by thorpe on Tue, 05 May 2020 14:25:12 -0700