2

I have text file "whitelist.txt" with one line:

whitelist_mac="11:11:11:11:11:11 22:22:22:22:22:22 33:33:33:33:33:33"

I need a batch script to add a new mac address 44:44:44:44:44:44 to this text in only one line at the end of line with (")

After I run the batch script the modified file should look like this:

whitelist_mac="11:11:11:11:11:11 22:22:22:22:22:22 33:33:33:33:33:33 44:44:44:44:44:44"

My attempt of a script is this:

@echo off
set /p mac=Add new MAC adress to whitelist:
echo whitelist_mac=%mac% >> whitelist.txt
pause

2 Answers 2

1

You can do it with a FOR loop, the only difficulty is working out how to escape the double quote delimiter

If it's the only line in whitelist.txt, the following works

set /p mac=Add new MAC adress to whitelist:
set whitelist=
FOR /F delims^=^"^ tokens^=2  %%G IN ('type whitelist.txt') DO  (set whitelist=%%G)
echo whitelist_mac^=^"%whitelist% %mac%^">whitelist.txt

If there are other lines, then you'll need to do some work with find/findstr (might be worth doing anyway to check for duplicates and check the validity of the provided MAC address)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Only one line. For enter the same MAC adress in two different files, is: echo whitelist_mac^=^"%whitelist% %mac%^">whitelist.txt whitelist2.txt
No, but you can do echo whitelist_mac^=^"%whitelist% %mac%^">whitelist.txt && copy /y whitelist.txt whitelist2.txt
set /p mac=Add new MAC adress to whitelist: set whitelist= FOR /F delims^=^"^ tokens^=2 %%G IN ('type mac_ath0.txt') DO (set whitelist=%%G) echo nvram set ath0_maclist^=^"%whitelist% %mac%^">mac_ath0.txt FOR /F delims^=^"^ tokens^=2 %%G IN ('type mac_ath1.txt') DO (set whitelist=%%G) echo nvram set ath1_maclist^=^"%whitelist% %mac%^">mac_ath1.txt
required for dd-wrt router setting mac adress whitelist, ssh conection, with putty command line script: plink.exe -v -ssh 192.168.1.1 -P 22 -l root -pw password -m mac_ath0.txt
0

How about a powershell script that works on a file in the same folder? Granted it could be more robust but it works as a simple example. I know you asked for a batch file but I am am dabbling in powershell lately. :-/

$NewMac = Read-Host -Prompt 'Add new MAC adress to whitelist'
(get-content .\whitelist.txt) | foreach-object {$_ -replace "`"$", " $NewMac`""}| set-content .\whitelist.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.