BASH在表

时间:2016-01-25 18:35:47

标签: bash search while-loop

这是几个星期前我得到帮助的脚本的延续。我把所有的东西都搞定了,不幸的是情况发生了一些变化,我有点压力让它运行起来所以我希望有人能帮助我快速修改这个脚本。

以下是基础知识。该脚本的目的是重新捕获特定的浮动IP。由于系统限制,目前唯一的方法是通过玩彩票...脚本请求一个浮动IP池并将它们放在一个如下所示的表中:

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

然后脚本检查我们正在寻找的浮动IP是否在该表中,即已被捕获。如果没有,脚本会将所有IP返回到池中并再次尝试。如果我们要查找的地址已被捕获,则该脚本会丢弃所有其他IP并终止。

此脚本的当前版本非常适合捕获单个IP,但现在我们需要它来捕获多个IP。这是旧的单一IP版本:

#!/bin/bash
# Floating IP Reclaimer

################# CONFIG ##################
float="10.10.10.3"
tenantid="blah"
###########################################

# Start
clear
loop=true
if ! [ "$tenantid" = "$OS_TENANT_ID" ]; then
        echo "ERROR - Be sure you have sourced the proper tenant ID!"
else
        l=1
        while $loop; do
                printf "Floating IP Reclaimer\n\n"
                printf "Requesting address block...\n"
                for ((i=1;i<=5;i++));
                do
                        nova floating-ip-create floating-pool > /dev/null 2>&1
                done
                printf "Checking for matches...\n"
                nova floating-ip-list > /tmp/block
                while read garbage1 ip garbage2;
                do
                        if [ "$ip" != "$float" ]; then
                                printf "Releasing $ip\n"
                                nova floating-ip-delete "$ip" > /dev/null 2>&1
                        else
                                loop=false
                        fi
                done < <(tail -n +4 /tmp/block | head -n -1)
                printf "\nFloating IP blocks searched: "$l
                l=$((l+1))
                clear
        done
        printf "\nFloating IP reclaimed!\n\n"
fi

谢谢大家的帮助。在我们发言时,我正在研究解决方案。不幸的是,有时我在压力下工作不太好。

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试使用此尺寸(尚未经过测试)。我知道这不是重击,但它应该完成工作

#!/usr/bin/python
import os,time
import novaclient.client as nvclient

class FloatingIpManager():

    def __init__(self,desired_addresses=[],sleep=5):
        self.ips = desired_addresses
        self.nova = get_nova_client()
        self.sleep = sleep

    def get_nova_client(self,env=os.environ):
        d = {}
        d['username'] = env['username']
        d['api_key'] = env['password']
        d['auth_url'] = env['auth_url']
        d['project_id'] = env['tenant_name']
        return nvclient.Client('2',**d)

    def get_float(self,address=None,pool=None):
        while True:
            try:
                flip = self.nova.floating_ips.create(pool)
                if flip.ip in self.ips or address == None:
                    return True
                else:
                    self.release_float(flip)
            except Exception as e:
                print e
            time.sleep(self.sleep)


    def release_float(self,flip):
        return self.nova.floating_ips.delete(flip)

    def reclaim_all(self,addresses=None):
        if addresses == None:
            addresses = self.ips
        for i in self.ips:
            get_float(i)

if __name__ == '__main__':
    nova = get_nova_client()
    reclaimer = FloatingIpManager(['192.168.100.1'])
    reclaimer.reclaim_all()

答案 1 :(得分:0)

你的bash回答。使用您想要的IP作为参数调用它,例如“reclaimer 192.168.1.1 192.168.1.2 192.168.1.3”

#!/bin/bash
# Floating IP Reclaimer

################# CONFIG ##################
#float="10.10.10.3"
tenantid="blah"
###########################################

# Start
clear
loop=true
let count=$#-1
if ! [ "$tenantid" = "$OS_TENANT_ID" ]; then
        echo "ERROR - Be sure you have sourced the proper tenant ID!"
else
        l=1
        while $loop; do
                printf "Floating IP Reclaimer\n\n"
                printf "Requesting address block...\n"
                for ((i=1;i<=5;i++));
                do
                        nova floating-ip-create floating-pool > /dev/null 2>&1
                done
                printf "Checking for matches...\n"
                nova floating-ip-list > /tmp/block
                while read garbage1 ip garbage2;
                do
                        for float in $@; do
                                if [ "$ip" != "$float" ]; then
                                        printf "Releasing $ip\n"
                                        found=false
                                else
                                        let i=i+1
                                        continue 2
                                fi
                        done
                        nova floating-ip-delete "$ip" > /dev/null 2>&1
                done < <(tail -n +4 /tmp/block | head -n -1)
                printf "\nFloating IP blocks searched: "$l
                l=$((l+1))
                clear
                if [ i -eq count ]; then
                        loop=false
                fi
        done
        printf "\nFloating IP reclaimed!\n\n"
fi