-
Notifications
You must be signed in to change notification settings - Fork 10
/
oneip
executable file
·148 lines (137 loc) · 3.56 KB
/
oneip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
ARG_IP=""
ARG_NET=""
NET_IDS=$(onevnet list | grep -v ID | awk '{print $1}')
if [ $1 == "-c" ] || [ $1 == "--check" ]
then
if [[ $2 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]
then
ARG_IP=$2
else
echo "#############################################"
echo " This ($2) is not valid IP format"
echo "#############################################"
exit
fi
fi
if [ $1 == "-g" ] || [ $1 == "--get" ]
then
NET_EXISTS=false
ARG_NET="$2"
for i in "${NET_IDS[@]}"
do
if [[ $i == $ARG_NET ]]
then
NET_EXISTS=true
fi
done
if [[ $NET_EXISTS == "true" ]]
then
echo "#############################################"
echo " Net with this IP ($2) does not exists"
echo "#############################################"
exit
fi
fi
ALL_IPS=""
for NETS in $NET_IDS
do
NET_RANGES=$(onevnet show $NETS | grep "^IP" | awk '{print $2 "-" $3}')
for RANGE in $NET_RANGES
do
RANGE_FIRST=$(echo $RANGE | awk -F \- '{print $1}')
RANGE_LAST=$(echo $RANGE | awk -F \- '{print $2}')
WILD_CARD=$(echo $RANGE_FIRST | awk -F \. '{print $1"."$2"."$3".%g" }')
RANGE_START=$(echo $RANGE_FIRST | awk -F \. '{print $4}')
RANGE_LAST=$(echo $RANGE_LAST | awk -F \. '{print $4}')
SUBNET_IPS=$(seq -f "$WILD_CARD" $RANGE_START $RANGE_LAST)
ALL_IPS="$ALL_IPS $SUBNET_IPS"
done
done
case $1 in
"-c" | "--check")
IP_PINGED=false
IP_RESERVED=false
IP_USED=false
IP_IS_IN_SUBNET_BUT_NOT_USED=false
for NET in $NET_IDS
do
IP_IN_SUBNET=$(onevnet show $NET|grep -i " $2 ")
if [ -z "$IP_IN_SUBNET" ]
then
if ping -c1 -w2 $2 >/dev/null 2>&1
then
IP_PINGED=true
fi
else
if [[ ! -z "$IP_IN_SUBNET" ]]
then
VM_ID=$(echo $IP_IN_SUBNET | awk '{print $2}' | awk -F\: '{print $2}')
fi
if [ "$VM_ID" == "-1" ]
then
IP_RESERVED=true
echo "#############################################"
echo " This IP is reserved"
echo "#############################################"
else
IP_USED=true
VM_NAME=$(onevm show $VM_ID | grep ^NAME | awk '{print $3}')
VM_HOST=$(onevm show $VM_ID | grep ^HOST | awk '{print $3}')
echo "#############################################"
echo " This IP is on VM"
echo " VM : $VM_NAME"
echo " Host : $VM_HOST"
echo "#############################################"
fi
fi
done
if [ "$IP_PINGED" == "true" ] && [ "$IP_RESERVED" == "false" ] && [ "$IP_USED" == "false" ]
then
echo "#############################################"
echo " This IP is not used by opennebula but it is pingable"
echo "#############################################"
fi
if [ "$IP_PINGED" == "false" ] && [ "$IP_RESERVED" == "false" ] && [ "$IP_USED" == "false" ]
then
echo "#############################################"
echo " IP is FREE"
echo "#############################################"
fi
;;
"-g" | "--give")
USED_IPS=$(onevnet show $NET_ID | grep -Eo '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+')
FOUND_IP=false
for IP in $SUBNET_IPS
do
for used_ip in $USED_IPS
do
if [ $IP == $used_ip ]
then
FOUND_IP=true
break
else
FOUND_IP=false
continue
fi
done
if [ $FOUND_IP == "false" ]
then
if ping -c1 -w3 $IP >/dev/null 2>&1
then
continue
else
echo $IP is free
exit
fi
fi
done
;;
*)
echo
echo "oneip --check 172.16.77.134"
echo "oneip -c 172.16.77.134"
echo "oneip --get [NET_ID]"
echo "oneip -g [NET_ID]"
;;
esac