How can I write a Linux bash script that tells me which computers are ON in my LAN?

前端 未结 16 1543
攒了一身酷
攒了一身酷 2020-12-12 11:59

How can I write a Linux Bash script that tells me which computers are ON in my LAN?

It would help if I could give it a range of IP addresses as input.

16条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 12:56

    Assuming my network is 10.10.0.0/24, if i run a ping on the broadcast address like

    ping -b 10.10.0.255
    

    I'll get an answer from all computers on this network that did not block their ICMP ping port.

    64 bytes from 10.10.0.6: icmp_seq=1 ttl=64 time=0.000 ms
    64 bytes from 10.10.0.12: icmp_seq=1 ttl=64 time=0.000 ms 
    64 bytes from 10.10.0.71: icmp_seq=1 ttl=255 time=0.000 ms 
    

    So you just have to extract the 4th column, with awk for example:

    ping -b 10.10.0.255 | grep 'bytes from' | awk '{ print $4 }'
    
    10.10.0.12:
    10.10.0.6:
    10.10.0.71:
    10.10.0.95:
    

    Well, you will get duplicate, and you may need to remove the ':'.

    EDIT from comments : the -c option limits the number of pings since the script will end, we can also limit ourself on unique IPs

    ping -c 5 -b 10.10.0.255 | grep 'bytes from' | awk '{ print $4 }' | sort | uniq
    

提交回复
热议问题