0

Coming from Linux I'm fighting with a really basic problem with batch scripting on windows. On a LAN a network drive needs to be remapped based on a device MAC address. Finding it's IP can be achieved with this little script

@echo off
for /L %%a in (1,1,254) do @start /b ping 192.168.1.%%a -n 2 > nul
ping 127.0.0.1 -n 3 > nul
arp -a | find /i "01-00-5e-7f-ff-fa"

Gives me the following line based on a search for 01-00-5e-7f-ff-fa

192.168.135.3       01-00-5e-7f-ff-fa     static

How can I extract/save the IP 192.168.135.3 in a variable called ASSIGNED_IP to map the device as drive Y like:

net use drive Y /delete
net use Y: \\%ASSIGNED_IP%\files

What I've tried without luck: Using a FOR loop for assigning the result as suggested here further using the substring method as explained here

Anatol
  • 1,923
  • 6
  • 26
  • 55
  • thanks @LotPings It's failing with `The system cannot find the file 'arp -a | find /i "01-00-5e-7f-ff-fa".` – Anatol Jun 24 '19 at 18:41
  • `for /f %%a in ('arp -a ^| find /i "01-00-5e-7f-ff-fa" ' ) do set "ASSIGNED_IP=%%a"` but you should 1st initialize the var to naught and afterwards check it is set with an `if defined ASSIGNED_IP ...`. –  Jun 24 '19 at 18:42
  • perfect this does it! would you post it as an answer that I can accept it? – Anatol Jun 24 '19 at 18:45
  • That's pretty basic stuff, the for /f parses the output of the command in single quotes (executed by a 2nd cmd.exe) with a default delimiter of space/tab and only token=1 similar to piping to `|awk '{print $1}'` –  Jun 24 '19 at 19:12
  • @LotPings true basic stuff. as said windows is lightyears away for me so this was hard ;) – Anatol Jun 24 '19 at 19:13

0 Answers0