How to dynamically modify the host file under Windows
Event Background: in order to verify whether the test data is successfully added at multiple test nodes in another environment after the test data is submitted, a lot of playback methods are found and implemented in a stupid way. Not much nonsense, the idea is as follows:
To be safe, first back up the contents of the hosts file:
1. Read all text contents of hosts. The code is as follows
2. Empty the hosts file:
3. Add a line of hosts, or multiple, and write as appropriate
4. Open the hosts binding website. Here we take Baidu as an example to prompt that we can't connect.
5. After clearing the hosts file, write the original contents of the backup to complete the operation of restoring the hosts file
6. Test code is as follows:
Stupid method, mark first.