2

I have a need to add my external IP address to the Windows registry on a scheduled basis, and I want to do this with a bat/cmd file to automate the process.

I was able to use the advice from this post on this site to use curl.exe to get my external IP using http://icanhazip.com and set that IP as a variable within the batch process.

Now I just need a way to take that variable and inject it into the registry.

I know reg.exe or regedit.exe can add information to the registry by referencing a .REG file, but I don't quite know how to take the variable I get and add it without a .REG file.

One thing I already tried was using a .REG file already created, copying that to a new file (to preserve the original .REG file for reuse), then use echo to place the variable into the .REG file, like:

echo "some_reg_value"="%externalIP%" >> addIP.reg

The problem with this is the %externalIP% variable is adding an additional space after the IP address, so it looks like this in the .REG file and in the registry once the .REG file is added:

"some_reg_value"="192.168.1.100 " 

That extra space at the end causes a serious issue for the purpose I am trying to use it for.

Is there possibly a way to remove that extra space from the variable?

This is how I am getting the variable:

%CURL%\curl http://icanhazip.com > %CURL%\publicIP.txt
for /f "delims= " %%G in (%CURL%\publicIP.txt) do set PublicIP=%%G & del %CURL%\publicIP.txt

Any help would be greatly appreciated. It has been quite a while since I tried to do a whole lot with batch scripting and I can't remember a lot of the available commands.

T-Fed
  • 23

1 Answers1

3

I suspect that the solution is painfully “obvious”:

… do set PublicIP=%%G& del %CURL%\publicIP.txt

i.e., don’t include a space at the end of the set PublicIP= command.

However, another useful trick to know about is %PublicIP:~0,-1%, which is %PublicIP% with the last character removed.  See help set.