简体   繁体   English

BASH 增量 MAC 地址

[英]BASH Increment MAC Address

I've got a valid mac address in a var called oldMAC I need to increment this and then return a new valid MAC Address.我在名为oldMAC的 var 中有一个有效的 mac 地址,我需要增加它,然后返回一个新的有效 MAC 地址。

In this example I'm incrementing by 1, but it could be by any value.在这个例子中,我增加了 1,但它可以是任何值。

So far I've got the following:到目前为止,我有以下几点:

echo $oldMAC
mac=$(echo $oldMAC |  tr '[:lower:]' '[:upper:]' | tr -d ':') # upper case and remove :
echo $mac

macdec=$( printf '%d\n' 0x$mac ) # convert to decimal
echo $macdec

macadd=$( expr $macdec + 1 ) # add 1
echo $macadd

machex=$( printf '%X\n' $macadd ) # convert to hex
echo $machex

This outputs:这输出:

00:12:34:ae:BC:EF (oldMAC)
001234AEBCEF (mac)
78193278191 (macdec)
78193278192 (madadd)
1234AEBCF0 (machex)

The issue I have is working out how to convert 1234AEBCF0 so it returns as 00:12:34:AE:BC:F0我遇到的问题是如何转换1234AEBCF0 ,使其返回为00:12:34:AE:BC:F0

Can anyone advise how to do this... or is there a better way ?任何人都可以建议如何做到这一点......或者有更好的方法吗?

Thanks谢谢

sed to rescue: sed来拯救:

macnew=$(echo $machex | sed 's/../&:/g;s/:$//')

The pattern is图案是

+------------ substitute
|  +--------- any two characters
|  | +------- with the whole match
|  | |+------ and :
|  | || +---- all occurrences, utilizing the fact it means non-overlapping
|  | || |+------- another command
|  | || ||+------ substitute
|  | || ||  +---- :
|  | || ||  |+--- at the end of line
|  | || ||  || +- with nothing to get rid of the trailing :
V  V VV VV  VV V
s/../&:/g;s/:$//

You also need to make sure 12 digits are actually printed.您还需要确保实际打印了 12 位数字。 The printf command can do that, just make the pattern "%012x"0 means pad with 0 s (instead of spaces) and 12 is the minimum width. printf命令可以做到这一点,只需创建模式"%012x" —— 0表示用0 s(而不是空格)填充, 12是最小宽度。 Use uppercase X for uppercase hex digits and lowercase x for lowercase hex digits.使用大写X表示大写十六进制数字,小写x表示小写十六进制数字。


You can simplify the addition a bit by using the bash's built-in arithmetic expansion, which understand hexadecimal output directly, and understands both upper and lowercase, so you only need to drop the : s:您可以通过使用 bash 的内置算术扩展来稍微简化加法,它可以直接理解十六进制输出,并且可以理解大小写,因此您只需要删除: s:

mac=$(echo $oldMAC | tr -d ':')
macadd=$(( 0x$mac + 1 ))

It still comes back as decimal, so you still need the printf "%012x" to convert it.它仍然以十进制形式返回,因此您仍然需要 printf "%012x" 来转换它。 You can pipe it directly to the sed to keep it short.您可以将其直接通过管道传输到 sed 以保持简短。

macnew=$(printf "%012x" $macadd | sed 's/../&:/g;s/:$//')

Just need to change 2 lines.只需要改变2行。

mac=$(echo $oldMAC | tr '[:lower:]' '[:upper:]' | tr -d ':') mac=$(echo $oldMAC | tr '[:lower:]' '[:upper:]' | tr -d ':')

to

 mac=$(echo $oldMAC | sed s/":"//g)

and then进而

machex=$( printf '%X\\n' $macadd ) machex=$( printf '%X\\n' $macadd )

to

machex=$( printf '%X\n' $macadd |tee|tr A-Z a-z)

overall总体

mac=$(echo $oldMAC|sed s/":"//g)
macdec=$(printf '%d\n' 0x$oldmac)
macadd=$(expr $macdec + 1)
machex=$( printf '%X\n' $macadd |tee|tr A-Z a-z)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM