<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dennis henry &#187; assign ip addresses</title>
	<atom:link href="http://dennishenry.net/tag/assign-ip-addresses/feed/" rel="self" type="application/rss+xml" />
	<link>http://dennishenry.net</link>
	<description>ramblings of an IT professional</description>
	<lastBuildDate>Mon, 26 Sep 2011 16:50:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Scripting IP additions in OpenVZ</title>
		<link>http://dennishenry.net/2010/04/07/scripting-ip-additions-in-openvz/</link>
		<comments>http://dennishenry.net/2010/04/07/scripting-ip-additions-in-openvz/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 20:27:43 +0000</pubDate>
		<dc:creator>dennis</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[OpenVZ]]></category>
		<category><![CDATA[assign ip addresses]]></category>

		<guid isPermaLink="false">http://dennishenry.net/?p=49</guid>
		<description><![CDATA[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 &#124; grep -v CTID &#124; awk '{print $1}'`
do
     let k=0
     for j in `cat  [...]]]></description>
			<content:encoded><![CDATA[<p>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:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>vzlist <span style="color: #660033;">-a</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> CTID <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1}'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">do</span>
     <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">k</span>=<span style="color: #000000;">0</span>
     <span style="color: #000000; font-weight: bold;">for</span> j <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>ips<span style="color: #000000; font-weight: bold;">`</span>
     <span style="color: #000000; font-weight: bold;">do</span>
          <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$k</span> <span style="color: #660033;">-lt</span> <span style="color: #000000;">2</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
          <span style="color: #000000; font-weight: bold;">then</span>
               vzctl <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #007800;">$i</span> <span style="color: #660033;">--ipadd</span> <span style="color: #007800;">$j</span> <span style="color: #660033;">--save</span>
               <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">k</span>=k+<span style="color: #000000;">1</span>
          <span style="color: #000000; font-weight: bold;">else</span>
               <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-i</span> <span style="color: #ff0000;">'1,2d'</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>ips
               <span style="color: #7a0874; font-weight: bold;">break</span>
          <span style="color: #000000; font-weight: bold;">fi</span>
     <span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>The main things to note here are the following:</p>
<ul>
<li>When assigning a variable a value in a for loop, you need to use &#8220;let&#8221; before declaring it</li>
<li>SED can be used to remove lines just as it can be used to add and replace items in lines</li>
<li>Break is not something to be afraid of!</li>
</ul>
<p>In any case, this script might be handy for others trying to perform similar tasks so I&#8217;ll just leave it here. Feel free to modify and use this code as you please <img src='http://dennishenry.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Let me know if you have any optimizations you can think of in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://dennishenry.net/2010/04/07/scripting-ip-additions-in-openvz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

