Increment MAC address
I'm trying to develop a script which will, in part, generate a list of MAC addresses from a given starting base. What I have as input is a list of MAC addresses, and need to increment by 1.
Examples:
00:1D:FE:12:37:1A
(need to generate 00:1D:FE:12:37:1B
)
00:1D:FE:12:37:49
(need to generate 00:1D:FE:12:37:4A
)
00:1D:FE:12:37:4F
(need to generate 00:1D:FE:12:37:50
)
The restriction here is that I need to run this script on a machine that has no "extra" 开发者_开发技巧perl modules neither installed nor available, so the code would need to be included within the script. This means Net::MAC
is out as a module, but I could potentially cannibalize some useful bits and include it but I'm thinking what I'm trying to do shouldn't be entirely complex to implement in a small function utilizing pack(?) or something of the like.
I've found various threads around on forums like PerlMonks, but no one seems to have a conclusive answer. Any input would be appreciated!
It's a 48-bit number. Parse it, increment it, format it.
Many Perl builds only support 32-bit numbers as integers, so I'll avoid forming larger ints.
my $mac_str = '00:1D:FE:12:37:1A';
( my $mac_hex = $mac_str ) =~ s/://g;
my ($mac_hi, $mac_lo) = unpack("nN", pack('H*', $mac_hex));
if ($mac_lo == 0xFFFFFFFF) {
$mac_hi = ($mac_hi + 1) & 0xFFFF;
$mac_lo = 0;
} else {
++$mac_lo;
}
$mac_hex = sprintf("%04X%08X", $mac_hi, $mac_lo);
$mac_str = join(':', $mac_hex =~ /../sg);
I dont know perl but I have done macaddress increment in python. The code is added here
def getmacaddress_increment(macinst, rowindex):
inst = gethex_todecimal(macinst)
macarray = inst.split('.')
instarr = []
seg5 = int(macarray[5]) + rowindex
if seg5 > 255:
macarray[5] = str(seg5%255)
seg4 = int(macarray[4])
macarray[4] = str(seg4 + int(seg5/255))
else:
macarray[5] = str(seg5)
seg4 = int(macarray[4])
if seg4 > 255:
macarray[4] = str(seg4%255)
seg3 = int(macarray[3])
macarray[3] = str(seg3 + int(seg4/255))
else:
macarray[4] = str(seg4)
seg3 = int(macarray[3])
if seg3 > 255:
macarray[3] = str(seg3%255)
seg2 = int(macarray[2])
macarray[2] = str(seg2 + int(seg3/255))
else:
macarray[3] = str(seg3)
seg2 = int(macarray[2])
if seg2 > 255:
macarray[2] = str(seg2%255)
seg1 = int(macarray[1])
macarray[1] = str(seg1 + int(seg2/255))
seg1 = int(macarray[1])
if seg1 > 255:
macarray[1] = '0'
seg0 = int(macarray[0])
macarray[0] = str(seg0 + 1)
seg0 = int(macarray[0])
if seg0 > 255:
macarray[0] = '0'
arr = []
#newval = macarray[0] + '.' + macarray[1] + '.' + macarray[2] + '.' + macarray[3] + '.' + macarray[4] + '.' + macarray[5]
#print 'Updated value: ', newval
for cnt, instr in enumerate(macarray):
arr.append("%0.2x" % int(instr))
return ':'.join(arr)
def toint(macid):
return str(int(macid, 16))
def gethex_todecimal(macaddr):
macaddr = macaddr.replace('-', ':')
inst = map(toint, macaddr.split(':'))
return '.'.join(inst)
Check if you could use this logic to increment in perl. Sorry i dont have a straight forward answer.
Siva
Here I am using code for increment the MAC ADDRESS of but at last position,when we increment the MAC ADDRESS it overflow SO, I used code start from 00
for example: if last position is FF it to starting point 00
#!/bin/sh
mac=$(ifconfig eth0|grep HWaddr|awk '{print $5}'| tr '[a-z]' '[A-Z]' | cut -d ':' -f1-5)
maclast=$(ifconfig eth0|grep HWaddr|awk '{print $5}'| tr '[a-z]' '[A-Z]' | cut -d ':' -f6)
echo "Mac address= $mac:$maclast"
decmac=$(echo "ibase=16; $maclast"|bc)
echo $decmac
if [ $decmac -eq '241' ]
then
macinc='00'
else
incout=`expr $decmac + 1 `
macinc=$(echo "obase=16; $incout"|bc)
fi
echo "Mac address= $mac:$macinc"
OUTPUT:
Mac address= 00:19:D1:F6:F7:FF
Mac address= 00:19:D1:F6:F7:00
Incrementing the MAC ADDRESS by 1:
#!/bin/sh
mac=$(ifconfig eth0|grep HWaddr|awk '{print $5}'| tr '[a-z]' '[A-Z]' | cut -d ':' -f1-5)
maclast=$(ifconfig eth0|grep HWaddr|awk '{print $5}'| tr '[a-z]' '[A-Z]' | cut -d ':' -f6)
echo "Mac address= $mac:$maclast"
hexmac=$(echo "ibase=16; $maclast"|bc)
incout=`expr $hexmac + 1 `
macinc=$(echo "obase=16; $incout"|bc)
echo "Mac address= $mac:$macinc"
OUTPUT:
Mac address= 00:19:D1:F6:F7:F1 Mac address= 00:19:D1:F6:F7:F2
Download Net::Mac and shove it into your script as a module
/tydel.pl
/Net
/Mac.pl
Ruby:
def gen_next_mac(base_mac, rowindex)
mac_ints = base_mac.split(':').map {|s| s.to_i(16).to_s(10).to_i}
mac_ints[5] += rowindex
if mac_ints[5] > 255
mac_ints[5] = mac_ints[5]%255
mac_ints[4] = mac_ints[4] + mac_ints[5]/255
end
if mac_ints[4] > 255
mac_ints[4] = mac_ints[4]%255
mac_ints[3] = mac_ints[3] + mac_ints[4]/255
end
if mac_ints[3] > 255
mac_ints[3] = mac_ints[3]%255
mac_ints[2] = mac_ints[2] + mac_ints[3]/255
end
if mac_ints[2] > 255
mac_ints[2] = mac_ints[2]%255
mac_ints[1] = mac_ints[1] + mac_ints[2]/255
end
if mac_ints[1] > 255
mac_ints[1] = mac_ints[1]%255
mac_ints[0] = mac_ints[0] + mac_ints[1]/255
end
return mac_ints.map {|i| '%02x' %i }.join(':')
end
精彩评论