Atomiklan
Atomiklan

Reputation: 5454

Bash search for IP in table

I am having trouble with a simple search of an output of values in a table. This is the output from my nova command:

+------------+-----------+----------+----------+
| Ip         | Server Id | Fixed Ip | Pool     |
+------------+-----------+----------+----------+
| 10.10.10.1 |           | -        | floating |
| 10.10.10.2 |           | -        | floating |
| 10.10.10.3 |           | -        | floating |
| 10.10.10.4 |           | -        | floating |
| 10.10.10.5 |           | -        | floating |
+------------+-----------+----------+----------+

Lets say I am looking for 10.10.10.4. I need to run this nova command to output this table, then search the table for 10.10.10.4. For each IP that is NOT 10.10.10.4 I need to run:

nova floating-ip-delete <ip>

command. Once it finally finds the correct IP, the script stops.

In essence, we are capturing a large block of floating IPs at a time (5 in this table/example), looking to see if we captured a specific IP, and if not, releasing the IPs back to the pool. Then rinse and repeat until we finally catch the right IP.

Any help would be greatly appreciated!

* UPDATE *

Here is where I am at so far. Please ignore the first parts with the tenantid section. This is just some internal checks for safety:

#!/bin/bash
# Capture floating IP

################# CONFIG ##################
FLOAT="blah"
TENANTID="blah"
###########################################

# Start
if ! [ "$TENANTID" = "$OS_TENANT_ID" ]; then
        echo "ERROR"
else
        for ((i=1;i<=10;i++));
        do
                echo $i
                nova floating-ip-create floating
        done
        nova floating-ip-list > BLOCK
        while read garbage1 IP garbage2;
        do
                if [ "$IP" != "FLOAT" ]; then
                        nova floating-ip-delete "$IP"
                else
                        exit 0
                fi
        done < <(tail -n +4 /tmp/BLOCK)
fi

Upvotes: 1

Views: 130

Answers (2)

Oleg Andriyanov
Oleg Andriyanov

Reputation: 5289

You can start with something like this:

while read garbage1 ip garbage2; do
    if [ "$ip" != '10.10.10.4' ]; then
        nova floating-ip-delete "$ip"
    else
        exit 0
    fi
done < <(tail -n +4 /path/to/file | head -n -1)

Read about:

Process substitution, Read builtin, tail(1), head(1)

Upvotes: 1

IT_User
IT_User

Reputation: 779

You could try something like:

#!/bin/bash
while [[ $(novaCommand | grep '10.10.10.4') = "" ]] ; do
  for i in $(novaCommand | grep -Eo '10\.10\..*') ; do 
   nova floating-ip delete ${i}
  done
done
exit 

Upvotes: 0

Related Questions