2020/01/08

[bash] Check if the IP address is used


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/bin/bash
[ 1 -ne $# ] && {
        echo "Syntax: $0 ip_address"
        exit 0
}

ping -c1 -W1 $1 >/dev/null

ret=$(arp -n | grep "$1" | grep "(incomplete)")
[ -n "$ret" ] &&
{
        echo "$1 unused"
} || {
        echo "$1 used"
}

Line 2~5: Avoid no parameters.
Line 7: Use ping to learn arp.
Line 9~15: Check arp table and echo message.