ramblings of an IT professional
Posts tagged assign ip addresses
Scripting IP additions in OpenVZ
Apr 7th
So I just had an interesting issue where I was trying to add 60 IPs to 30 OpenVZ instances, 2 IPs per node. I came up with the following script to do so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/bin/bash for i in `vzlist -a | grep -v CTID | awk '{print $1}'` do let k=0 for j in `cat /root/ips` do if [ $k -lt 2 ] then vzctl set $i --ipadd $j --save let k=k+1 else sed -i '1,2d' /root/ips break fi done done |
The main things to note here are the following:
- When assigning a variable a value in a for loop, you need to use “let” before declaring it
- SED can be used to remove lines just as it can be used to add and replace items in lines
- Break is not something to be afraid of!
In any case, this script might be handy for others trying to perform similar tasks so I’ll just leave it here. Feel free to modify and use this code as you please
Let me know if you have any optimizations you can think of in the comments below.