0

Trying to get a list of available IP addresses based off all usable IPs in a range when compared to a device's ARP table.

Basing what I'm doing with comm on this discussion: Intersection of two arrays in BASH

Creating ranges of IPs to compare against - e..g 192.168.20.0/23

RANGE1=(192.168.20.{2..255})
RANGE2=(192.168.21.{0..254})
RANGE=("${RANGE1[@]}" "${RANGE2[@]}")
printf '%s\n' "${RANGE[@]}" | LC_ALL=C sort > "${IPSETS_DIR}/<city-alias>_set.txt"

$1 is an IP of a network device. OID is basically a device's ARP table. GREP_SEARCH example: "192.168.20|192.168.30|192.168.55"

$(which snmpbulkwalk) -v2c -c <community-string> "${1}" .1.3.6.1.2.1.4.35.1.4 > "${RESULTS_FILE}"
STRIPPED_RESULTS=( $(cut -d\" -f2 "${RESULTS_FILE}" | egrep -w "(^|\s)${GREP_SEARCH}") )
printf "%s\n" "${STRIPPED_RESULTS[@]}" | LC_ALL=C sort > "${STRIPPED_FILE}"

The walk returns results such as:
IP-MIB::ipNetToPhysicalPhysAddress.118161416.ipv4."X.X.X.X" = STRING: XX:XX:XX:XX:XX:XX

I then compare using the below. $1 is city-alias.

$(which comm) -13 "${STRIPPED_FILE}" "${IPSETS_DIR}/${1}_set.txt" > "${DIR}/${1}_stored_results.txt"

This MOSTLY works, but I'm still getting IPs that are in use. Not sure what I'm missing.

6
  • Related Linux tools to treat files as sets and perform set operations on them Commented Jun 15, 2022 at 18:18
  • "Set Complement" - "comm -23 <(sort set1) <(sort set2)" - "# outputs elements in set1 that are not in set2" -- That's it. Thanks for the link! Also, my GREP_SEARCH wasn't completely correct, so that was throwing me off. Commented Jun 15, 2022 at 19:52
  • $(which comm) is a really weird statement. Why not juse comm? (Likewise elsewhere; you really don't need which like this - just use the command.) Commented Jun 15, 2022 at 21:18
  • Yeah, definitely better ways to handle that. Everyone that will be using what I'm writing, besides myself, will be on WSL or Mac, so trying to make sure it'll work everywhere. Commented Jun 15, 2022 at 21:46
  • which(1) is already searching inside the PATH, so there is no reason to use it. If comm fails, so will $(which comm). Besides, there is only a handful of locations that command could normally be installed, most likely /usr/bin. Commented Jun 16, 2022 at 8:01

1 Answer 1

0

Stéphane's link gave me my answer.

Set Complement
$ comm -23 <(sort set1) <(sort set2)
# outputs elements in set1 that are not in set2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.