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

前端 未结 16 1592
攒了一身酷
攒了一身酷 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:48

    If you want to provide a list of hosts it can be done with nmap, grep and awk.

    Install nmap:

    $ sudo apt-get install nmap
    

    Create file hostcheck.sh like this:

    hostcheck.sh

    #!/bin/bash
    
    nmap -sP -iL hostlist -oG pingscan > /dev/null
    grep Up pingscan | awk '{print $2}' > uplist
    grep Down pingscan | awk '{print $2}' > downlist
    

    -sP: Ping Scan - go no further than determining if host is online

    -iL : Input from list of hosts/networks

    -oG : Output scan results in Grepable format, to the given filename.

    /dev/null : Discards output

    Change the access permission:

    $ chmod 775 hostcheck.sh
    

    Create file hostlist with the list of hosts to be checked (hostname or IP):

    hostlist (Example)

    192.168.1.1-5
    192.168.1.101
    192.168.1.123
    

    192.168.1.1-5 is a range of IPs

    Run the script:

    ./hostcheck.sh hostfile

    Will be generated files pingscan with all the information, uplist with the hosts online (Up) and downlist with the hosts offline (Down).

    uplist (Example)

    192.168.1.1
    192.168.1.2
    192.168.1.3
    192.168.1.4
    192.168.1.101
    

    downlist (Example)

    192.168.1.5
    192.168.1.123
    

提交回复
热议问题