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

前端 未结 16 1593
攒了一身酷
攒了一身酷 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 12:39

    Although an old question, it still seems to be important (at least important enough for me to deal with this). My script relies on nmap too, so nothing special here except that ou can define which interface you want to scan and the IP Range is created automagically (at least kind of).

    This is what I came up with

    #!/bin/bash
    #Script for scanning the (local) network for other computers 
    
    command -v nmap >/dev/null 2>&1 || { echo "I require nmap but it's not installed. Aborting." >&2; exit 1; }
    
    if [ -n ""$@"" ];  then
        ip=$(/sbin/ifconfig $1 | grep 'inet '  | awk '{ print $2}' | cut -d"." -f1,2,3 )
        nmap -sP $ip.1-255
    else
        echo -e "\nThis is a script for scanning the (local) network for other computers.\n"
        echo "Enter Interface as parameter like this:"
        echo -e "\t./scannetwork.sh $(ifconfig -lu | awk '{print $2}')\n"
    
        echo "Possible interfaces which are up are: "   
        for i in $(ifconfig -lu)
        do
            echo -e "\033[32m \t $i \033[39;49m"
        done
    
        echo "Interfaces which could be used but are down at the moment: "
        for i in $(ifconfig -ld)
        do
            echo -e "\033[31m \t $i \033[39;49m"
        done
        echo
    fi
    

    One remark: This script is created on OSX, so there might be some changes to linux environments.

提交回复
热议问题