Suman R. answered 10/13/20
Java and Computer Science Tutor
for i in {1..254};do (ping -c 1 192.168.9.$i > /dev/null && echo $i &); done
Looks like the problem is to first get the ip of your machine and then check for the other machines that are in your network segment
- ip linux command will give you the ip address of your machine.
- You need to pick up the ip command and use the first 3 numbers with a dots and replace it with yours eg: if I check mine I get something like 192.168.1.155 so in the above algorithm, I will replace the 192.168.9 with 192.168.1 then I will implement the complete shell script. I will explain what is happening below
I will make the assumption that I am running this in bash shell
for i in {1..254} or for i in `seq 1 254`
what this is telling is that we will enter a for loop with i value starting from 1 to 254, this is because the maximum number for a single network segment is 254
do
indicates the beginning of the for loop
(ping -c 1 192.168.9.$i > /dev/null && echo $i &) has some syntax errors and the will echo only the last segment a better way for this command is (ping -c1 192.168.9.$i > /dev/null 2>&1 && echo 192.168.9.$i) & this will execute the command and will echo the complete IP of the machine from which it got a successful return. if there are no response then we will get an error and the echo will not print anything.
done
indicates the end of the for loop
so the above script should be like below
for i in {1..254}
do
(ping -c1 192.168.9.$i > /dev/null 2>&1 && echo 192.168.9.$i) &
done