slitaz-vz view vz-contribs/easymac.sh @ rev 1

Add contribs scripts for OpenVZ
author Eric Joseph-Alexandre <erjo@slitaz.org>
date Tue Nov 01 09:21:58 2011 +0100 (2011-11-01)
parents
children
line source
1 #!/bin/sh
2 # Script for generating VMware MAC Addresses
3 # http://www.easyvmx.com
4 #
5 # Works on any *NIX system with standard Bourne Shell and /dev/urandom
6 #
7 # Freely distributable under the BSD license:
8 #
9 # ------------- Start licence --------------
10 # Copyright (c) 2006, http://www.easyvmx.com
11 # All rights reserved.
12 #
13 # Redistribution and use in source and binary forms, with or without
14 # modification, are permitted provided that the following conditions are met:
15 #
16 # Redistributions of source code must retain the above copyright notice,
17 # this list of conditions and the following disclaimer.
18 #
19 # Redistributions in binary form must reproduce the above copyright notice,
20 # this list of conditions and the following disclaimer in the documentation
21 # and/or other materials provided with the distribution.
22 #
23 # Neither the name of the <ORGANIZATION> nor the names of its contributors
24 # may be used to endorse or promote products derived from this software without
25 # specific prior written permission.
26 #
27 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 # POSSIBILITY OF SUCH DAMAGE.
38 # ------------- End licence --------------
39 #
40 # Changelog
41 # ---------
42 # 2006-11-06:
43 # Version 1.0, first release of EasyMAC!
44 #
45 # 2007-07-20:
46 # Version 1.1:
47 # Added option for _any_ MAC address, not only VMware addresses.
48 # Changed output for static/random MAC address. It now tells that these are VMware MAC adresses.
49 #
50 # 2008-10-08:
51 # Version 1.2:
52 # Added option for XenSource MAC address creation.
53 # You can now output MAC address only, without the leading text.
54 #
55 # 2009-04-08:
56 # Version 1.3:
57 # Changed the way XenSource MAC addresses are created.
59 #
60 # Version
61 #
63 EMVersion=1.r3
64 ReleaseYear=2009
65 ReleaseDate=2009-04-08
68 #
69 # Functions
70 #
72 # Random VMware MAC Address
73 vmrandom() {
74 vmrandmac=$(dd if=/dev/urandom bs=1 count=3 2>/dev/null | od -tx1 | head -1 | cut -d' ' -f2- | awk '{ print "00:0c:29:"$1":"$2":"$3 }')
75 echo $vmrandmac
76 }
78 # Static VMware MAC Address
79 vmstatic() {
80 max3f=$(printf "%02X" $(expr $(dd if=/dev/urandom bs=1 count=1 2>/dev/null | od -tu1 | head -1 | cut -d' ' -f2-) / 4) | tr A-Z a-z)
81 vmstatmac=$(echo -n "00:50:56:$max3f:" $(dd if=/dev/urandom bs=1 count=2 2>/dev/null | od -tx1 | head -1 | cut -d' ' -f2- | awk '{ print $1":"$2 }') | sed 's/\ //')
82 echo $vmstatmac
83 }
85 # Global MAC Address (any valid MAC address, from the full range)
86 global() {
87 globalmac=$(dd if=/dev/urandom bs=1 count=6 2>/dev/null | od -tx1 | head -1 | cut -d' ' -f2- | awk '{ print $1":"$2":"$3":"$4":"$5":"$6 }')
88 echo $globalmac
89 }
91 # XenSource MAC Address
92 xensource() {
93 max3f=$(printf "%02X" $(expr $(dd if=/dev/urandom bs=1 count=1 2>/dev/null | od -tu1 | head -1 | cut -d' ' -f2-) / 4) | tr A-Z a-z)
94 xensource=$(echo -n "00:50:56:$max3f:" $(dd if=/dev/urandom bs=1 count=2 2>/dev/null | od -tx1 | head -1 | cut -d' ' -f2- | awk '{ print $1":"$2 }') | sed 's/\ //')
95 echo $xensource
96 }
99 #
100 # Process options
101 #
103 case "$1" in
105 r|-r|random|-random)
106 if [ "$2" != "-m" ]; then
107 echo -n "Random VMware MAC Address: "
108 fi
109 vmrandom
110 echo ""
111 ;;
113 R|-R|RANDOM|-RANDOM|Random|-Random)
114 if [ "$2" != "-m" ]; then
115 echo -n "Random VMware MAC Address: "
116 fi
117 vmrandom | tr a-z A-Z
118 echo ""
119 ;;
121 s|-s|static|-static)
122 if [ "$2" != "-m" ]; then
123 echo -n "Static VMware MAC Address: "
124 fi
125 vmstatic
126 echo ""
127 ;;
129 S|-S|STATIC|-STATIC|Static|-Static)
130 if [ "$2" != "-m" ]; then
131 echo -n "Static VMware MAC Address: "
132 fi
133 vmstatic | tr a-z A-Z
134 echo ""
135 ;;
137 g|-g|global|-global)
138 if [ "$2" != "-m" ]; then
139 echo -n "Global MAC Address: "
140 fi
141 global
142 echo ""
143 ;;
145 G|-G|GLOBAL|-GLOBAL|Global|-Global)
146 if [ "$2" != "-m" ]; then
147 echo -n "Global MAC Address: "
148 fi
149 global | tr a-z A-Z
150 echo ""
151 ;;
153 x|-x|xen|-xen)
154 if [ "$2" != "-m" ]; then
155 echo -n "XenSource MAC Address: "
156 fi
157 xensource
158 echo ""
159 ;;
161 X|-X|XEN|-XEN|Xen|-Xen)
162 if [ "$2" != "-m" ]; then
163 echo -n "XenSource MAC Address: "
164 fi
165 xensource | tr a-z A-Z
166 echo ""
167 ;;
169 *)
170 echo ""
171 echo "EasyMAC! v. $EMVersion"
172 echo "Generate hardware adresses for virtual machines"
173 echo "Copyright (c) $ReleaseYear, http://www.easyvmx.com"
174 echo ""
175 echo "Usage: $0 {-r|-R|-s|-S|-x|-X|-g|-G} {-m}"
176 echo ""
177 echo "Options:"
178 echo " -r: Random VMware MAC address, lower case"
179 echo " -R: Random VMware MAC address, UPPER CASE"
180 echo " -s: Static VMware MAC address, lower case"
181 echo " -S: Static VMware MAC address, UPPER CASE"
182 echo " -x: XenSource MAC address, lower case"
183 echo " -X: XenSource MAC address, UPPER CASE"
184 echo " -g: Global MAC address, lower case"
185 echo " -G: Global MAC address, UPPER CASE"
186 echo ""
187 echo " -m: Add the -m option for MAC address only"
188 echo ""
189 echo "All valid options:"
190 echo " Random VMware Lower Case: {r|-r|random|-random}"
191 echo " Random VMware Upper Case: {R|-R|RANDOM|-RANDOM|Random|-Random}"
192 echo " Static VMware Lower Case: {s|-s|static|-static}"
193 echo " Static VMware Upper Case: {S|-S|STATIC|-STATIC|Static|-Static}"
194 echo " XenSource Lower Case: {x|-x|xen|-xen}"
195 echo " XenSource Upper Case: {X|-X|XEN|-XEN|Xen|-Xen}"
196 echo " Global MAC Lower case: {g|-g|global|-global}"
197 echo " Global MAC Upper case: {G|-G|GLOBAL|-GLOBAL|Global|-Global}"
198 echo ""
199 echo "Freely distributable under the BSD license"
200 echo "Visit http://www.easyvmx.com for the best online virtual machine creator!"
201 echo ""
202 exit 1
204 esac
206 exit $?