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.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>