update: 03/07/2019 - change script to fix issue of sometimes having a blank ip in hosts file, causing it to fail.. update: 03/12/2019 - added a linux version. Use cron with root to have it update every minute (if the wifi is not available it exists)
auto change the ip of a hostname in the hosts file
Why?
I have an Android and copy files to/from my computer a lot. Android uses the MTP protocal for USB conections, but i run into many issues while using it.. So i use either FTP or SFTP (currently have https://github.com/Magisk-Modules-Repo/ssh installed) through my Android hotspot wifi. Problem: Every time my Android phone starts a hotspot it uses a new ip for itself/gateway and everytime i want to connect via sftp i need to find the new ip and modify the "host" in filezilla (my sftp transfer client of choice)
My solution:
Have the new ip auto update the windows "hosts" file with the new ip, then i can put the hostname specified in that file in filezilla..
Instructions:
Download the "host-updater.ps1" file and save it anywhere on your computer (i put it in my Appdata folder)
Now to automate this use "Task Scheduler"
-
Open Task Scheduler from start menu
-
Click "Create Task.."
-
Give the task a name and description of your choice
-
Check of "Run with highest privileges" and "Hidden"
-
Go to the "Triggers" tab and click New
-
Add this information Begin the task: "On an event" Log: "Microsoft-Windows-NetworkProfile/Operational" Source: "NetworkProfile" Event ID: "10000"
-
Go to the "Actions" tab and click New Program/script: type "powershell.exe" Add arguments (optional): type "-ExecutionPolicy Bypass \host-updater.ps1" - without the quotes
#I had issues with this setting where task scheduler wasnt able to run the task at all (even manually), if this is the case choose "Any Connection"
- Go to the "Conditions" tab Choose power settings Network: check "start only if the following network connection is available" Choose the network connection that we care about