Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    92

    sed -- reperforming operation for each instance found


    I need to change the vif (virtual interface) mac address within a file. I may have anywhere from 1 to 5 different mac addresses to change. I would like the last 2 octets to be randomized. I can do this with the following script, however, all MAC addresses look the same. I would like my MY_RANDOM variable to be run after each match found. How can I accomplish this?

    Code:
    #!/bin/bash
    FILE="/var/ftp/vm.cfg"
    MY_RANDOM=$(xxd -l 1 -p /dev/urandom | tr a-z A-Z)
    MY_RANDOM2=$(xxd -l 1 -p /dev/urandom | tr a-z A-Z)
    FIND_REPLACE=$(sed -ri "s|mac=00:21:f6:((\w{2}(:)?){3})|mac=00:21:f6:AA:${MY_RANDOM}:${MY_RANDOM2}|g" $FILE)
    Output:
    Code:
    vif = ['mac=00:21:f6:AA:4B:93,bridge=101498d8ff', 'mac=00:21:f6:AA:4B:93,bridge=1017dc5841']
    Desired (Last 2 octets of each mac differ):
    Code:
    vif = ['mac=00:21:f6:AA:4B:93,bridge=101498d8ff', 'mac=00:21:f6:AA:C7:62,bridge=1017dc5841']

  2. #2
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    92
    -->
    Found my own solution. Still needs some additional testing, but seems to be working:

    Code:
    #!/bin/bash
    file="/var/ftp/vm.cfg"
    total=$( grep ^vif "$file" | grep -o mac | wc -l )
    for i in `seq 1 $total`; do
      my_random=$(xxd -l 1 -p /dev/urandom | tr a-z A-Z)
      my_random2=$(xxd -l 1 -p /dev/urandom | tr a-z A-Z)
      sed -ri "s|mac=00:21:f6:((\w{2}(:)?){3})|mac=00:21:f6:AA:${my_random}:${my_random2}|${i}" $file
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •