Writing IP to file

Often you have to log on to a computer remotely, using VPN or something similar. Unfortunately for various reasons, the IP address of the host computer might change from time to time. In order to still be able to log on you either have to physically look it up on the machine or hope a friendly colleague is hanging around, who is able to look up your IP. In order to avoid this situation I wrote a small bash script which reads your IP on a WIndows machine and writes it to a folder, which in my case is my Dropbox acount, and therefor available wherever I have internet connectivity:

  • Save the file .bat file and replace the name and path in the first line of the code.
  • Update @echo %IP% >> C:\my\folder\Dropbox\current_IP.txt with the filename you want the IP to be stored in
 
rem Saved in C:\some\folder\theCurrentFile.bat
@echo off
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
@echo %IP% >> C:\my\folder\Dropbox\current_IP.txt

 

From here the only thing left to do is to schedule a task in windows, to make it update the current_IP.txt every day:

  • Open WIndows Task Scheduler
  • Create a new task
  • Actions -> New -> Start a Program and select the .bat filetask
  • Triggers -> On a schedule -> Daily (and set time etc.)trigger

That’s it, now the file should be updated every day with the current IP of the host machine.