<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title><![CDATA[0xpebbles.org]]></title>
    <link><![CDATA[http://blog.0xpebbles.org]]></link>
    <description><![CDATA[0xpebbles blog]]></description>
    <lastBuildDate>Fri, 09 Apr 2021 08:37:24 +0200</lastBuildDate>
    <pubDate>Fri, 09 Apr 2021 08:37:24 +0200</pubDate>
    <language>en</language>

<!-- 20130925 -->    <item>
      <title>Random MAC address generation with jot and ksh</title>
      <link>http://blog.0xpebbles.org/Random-MAC-address-generation-with-jot-and-ksh</link>
      <pubDate>25 Sep 2013 00:00:00 +0000</pubDate>
      <content:encoded><![CDATA[

  

<p>
Here's a snippet I use to randomize my <b>if</b>'s MAC-48 address. The script makes use of a seed, <b>$1</b>. Note that using a new random address too frequently might just fill up a DHCP server's lease table unnecessarily. Note that this is for a client machine, like a laptop. Depending on the use, modify to clear the two bits referred to in the comment, or not:
</p>

<style type="text/css">
<!--
pre.sh { font-family: monospace; color: #dadada; background-color: #000000; }
.Comment { color: #626262; }
.Constant { color: #87afff; font-weight: bold; }
.Special { color: #00d700; font-weight: bold; }
.Statement { color: #ff8700; font-weight: bold; }
.String { color: #8787ff; }
.shShellVariables { color: #87afff; }
-->
</style>
<pre class='sh'>
<span class="Comment">#!/usr/local/bin/ksh</span>

<span class="Comment"># $1 is seed</span>

<span class="Comment"># Generate, but clear MSBytes's 2 LSB's to specify 'globally unique' and 'unicast'.</span>
<span class="Statement">printf</span> <span class="Statement">&quot;</span><span class="String">%02x:</span><span class="Statement">&quot;</span> <span class="shShellVariables">$((</span><span class="Special">$</span><span class="shShellVariables">(</span><span class="Special">jot -</span><span class="Statement">r</span><span class="Special"> </span><span class="Constant">1</span><span class="Special"> </span><span class="Constant">0</span><span class="Special"> </span><span class="Constant">255</span><span class="Special"> </span><span class="shShellVariables">$((</span><span class="shShellVariables">$1</span><span class="Special">+</span><span class="Constant">1</span><span class="shShellVariables">)))</span><span class="Special"> </span><span class="Statement">&amp;</span><span class="Special"> </span><span class="Constant">2#11111100</span><span class="shShellVariables">))</span>
jot <span class="Special">-r</span> <span class="Special">-s</span> : <span class="Special">-w</span> %02x <span class="Constant">5</span> <span class="Constant">0</span> <span class="Constant">255</span> <span class="shShellVariables">$1</span>
</pre>

<p>
Then, on FreeBSD I can change <b>$if</b>'s MAC-48 (driver permitting, in my case <b>iwn</b>) as follows:
</p>

<pre class='sh'>ifconfig <span class="shShellVariables">$if</span> link <span class="shShellVariables">$random_mac</span>
</pre>

<p>
If you use DHCP to configure <b>$if</b> it is a good idea to clean out the lease file to avoid requesting the same IP for a different MAC address, leading to problems. On FreeBSD, the lease file for this interface would be <i>/var/db/dhclient.leases.$if</i>.
</p>

]]></content:encoded>
    </item>




  </channel>
</rss>

