首页 > 解决方案 > 在bash中生成一定范围内的mac地址

问题描述

我需要一些帮助来生成 bash 脚本中某个范围内的所有 mac 地址。

#!/bin/bash
    
    mac='00:00:00:00'
    index=16000
    start_index=53
    
    for i in $( eval echo {$start_index..$((start_index+index))})
    do
        bridge fdb add printf "$mac:%x\n" $i dev sw1p1 static master
    done

设法在python中这样做

mac = '00:00:00:00'
index = 16000
start_index = 53
for number in range(start_index, start_index + index):
    hex_num = hex(number)[2:].rjust(4, '0')
    entry = "{}:{}{}:{}{}".format(mac, *hex_num)
    os.system(f"bridge fdb add {entry} dev sw1p1 static master)

标签: bash

解决方案


使用算术扩展将数字拆分为两个八位位组,并使用printf.

# define mac, start_index, index here
# assume index <= 65535
for ((num=start_index; num<=index; num++)); do
  printf -v sfx ':%02X:%02X' $((num>>8)) $((num&255))
  bridge fdb add "${mac}${sfx}" dev sw1p1 static master
done

推荐阅读