Change IP from Host File

This script works doing a PING request and obtaining its resultant IP. Feel free to change it and modify it as you need. First part does the PING request, second part changes the Host File and the third part opens the hosts file.
Explanation of the secon part:
1. Save the Hosts File text in an external TXT file without the line with the HOSTNAME. Actually the host file line isn’t change, it’s removed and then added a new one.
2. Write again the external TXT to the Hosts File (Now without the previous line).
3. Add the new line.

@echo off
FOR /f "tokens=1,3 delims=: " %%A IN ('ping -n 1 YOURDNSorURL') DO IF %%A==Reply SET myIp= %%B
set myIp=%myIp: =%
findstr /v HOSTNAME "%systemroot%system32driversetchosts" > "%systemroot%system32driversetchosts.txt"
type "%systemroot%system32driversetchosts.txt" > "%systemroot%system32driversetchosts"
echo %myIp% HOSTNAME >> "%systemroot%system32driversetchosts"

It’ll open hosts file with your new IP. Change “Reply” in first command depending on the language of your computer (e.g. In spanish computers is “Respuesta”).
Change HOSTNAME by the name you want to appear with the IP. Change YOURDNSorURL by the address where you get the IP.
NOTE: This is a usefull method to obtain the IP from a CloudShare machine, ’cause anytime it restarts its IP changes.


No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *