当前位置:网站首页>Modify hosts file using batch script
Modify hosts file using batch script
2022-08-02 04:32:00 【GentleTK】
Recently, the company has a project that needs to be localhosts文件进行修改,Add a local domain name resolution record,If let the customer do the operation,It is easy to destroy the original onehosts文件,It is best to encapsulate it as a batch script,The user only needs to run with administrator privileges,Then select the function to be performed according to the menu.
1 批处理脚本
:: Turn off echoing of commands
@echo off
:: 设置hosts文件路径
set HOSTS=C:\Windows\System32\drivers\etc\hosts
:: 判断当前路径是否存在 hosts.init 文件, Backup if it does not exist
if not exist %HOSTS%.init (
copy /y %HOSTS% %HOSTS%.init && echo 系统hosts文件备份完成!
)
:: %date%=2021年07月07日
:: %time%= 9:12:38:96
:: 年 yyyy = %date:~0,4% Indicates that the pointer is offset from left to right 0 位, 然后提取 4 字符
:: 月 mm = %date:~5,2% Indicates that the pointer is offset from left to right 5 位, 然后提取 2 字符
:: 日 dd = %date:~8,2% Indicates that the pointer is offset from left to right 8 位, 然后提取 2 字符
set yyyymmdd=%date:~0,4%-%date:~5,2%-%date:~8,2%
:: 时 hh = %time:~0,2% Indicates that the pointer is offset from left to right 0 位, 然后提取 2 字符
:: 分 mm = %time:~3,2% Indicates that the pointer is offset from left to right 3 位, 然后提取 2 字符
:: 秒 ss = %time:~6,2% Indicates that the pointer is offset from left to right 6 位, 然后提取 2 字符
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
:: 当 hh 小于等于 9 时, 在前面补 0
if %hh% leq 9 (set hh=0%hh:~1,1%)
GOTO MENU
:MENU
ECHO.
ECHO.############################## 修改系统hosts解析文件 ##############################
ECHO.#
ECHO.# 1.添加域名解析
ECHO.#
ECHO.# 2.编辑hosts文件
ECHO.#
ECHO.# 3.备份hosts文件
ECHO.#
ECHO.# 4.还原hosts文件
ECHO.#
ECHO.# 5.刷新dnscache and exit
ECHO.#
ECHO.############################### %yyyymmdd% %hh%:%mm%:%ss% ###############################
set /p="Please enter the serial number you want to operate:"<nul
@set /p sel=
if "%sel%"=="1" goto add_ipdomain
if "%sel%"=="2" goto edit_hosts
if "%sel%"=="3" goto backup_hosts
if "%sel%"=="4" goto reset_hosts
if "%sel%"=="5" goto refresh_dns
PAUSE
:add_ipdomain
set /p="请输入IP地址和域名(中间以空格分隔):"<nul
@set /p ip_domain=
type %HOSTS% |findstr /v "^#"| findstr /i "%ip_domain%\>"
if %errorlevel% == 0 (
echo %ip_domain% 已存在,请重新添加!&& goto MENU
) else (
echo.%ip_domain% >> %HOSTS% && echo %ip_domain% 添加成功,Please press any key to return to the menu!&& pause >nul
)
goto MENU
:edit_hosts
title 编辑hosts本地域名解析文件
notepad %HOSTS%
echo 按任意键返回菜单!
pause >nul
goto MENU
:backup_hosts
title 备份hosts文件
set yyyymmdd=%date:~0,4%%date:~5,2%%date:~8,2%
copy /y %HOSTS% %HOSTS%.%yyyymmdd%%hh%%mm%
echo ("%HOSTS%.%yyyymmdd%%hh%%mm%") 备份完毕,按任意键返回菜单!
pause >nul
goto MENU
pause
:reset_hosts
title 还原hosts文件
copy /y %HOSTS%.init %HOSTS%
echo hostsThe file has been restored to its original state,按任意键返回菜单!
pause >nul
goto MENU
:refresh_dns
title 刷新本地dnscache and exit
echo 查看修改后的hosts文件内容,5秒后退出!
type "%HOSTS%" |findstr /v "^#"|findstr "[0-9]"
ping -n 5 127.0.0.1>nul
ipconfig /flushdns
echo Refreshing the local cache succeeded,即将退出!
ping -n 3 127.0.0.1>nul
echo.
exit
2 使用方法
2.1 功能菜单
The first time you run the script with administrator privileges, a copy will be madehosts.init
文件,作为hosts
初始文件,方便以后还原.
2.2 添加域名解析
Adding domain name resolution here uses manual input IP
地址 + 域名,and will be retrievedhosts
Whether the corresponding file already exists IP
地址和域名,如果存在,The user is prompted to add it again.
2.3 编辑 hosts 文件
Here is actually calling Notepad to openhosts文件.
2.4 备份 hosts文件
The backup file here is hosts.日期时间
file name format for backup.
2.5 还原 hosts文件
Restoration is in fact hosts.init
去掉后缀名,替换掉原先的 hosts
文件.
2.6 刷新 dns cache and exit
在刷新 dns
前,会先将 hosts
Print the contents of the file,Then perform a refresh dns
Exit the script after the command.
文章作者:GentleTK
原文链接:https://gentletk.gitee.io/Modified using batch scripthosts文件
边栏推荐
- TimeSformer视频理解框架:视频理解中的Transformer
- 野火ISO-V2学习
- MPU6050 加速度计和陀螺仪传感器与 Arduino 连接
- Binder机制详解(一)
- 一分钟get:缓存穿透、缓存击穿、缓存雪崩
- Two-Stream Convolutional Networks for Action Recognition in Videos双流网络论文精读
- 中国大陆开源镜像站汇总
- 张量乘积—实验作业
- ontop-vkg 学习1
- Windows下MySQL数据库报“ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:8000‘ (10061)”错误解决
猜你喜欢
随机推荐
Android-Kotlin anko库实现优雅跳转
whistle 手机调试代理工具
深度学习实战(1):花的分类任务
Acwing:哈夫曼树(详解)
如何在 Scala 中科学地操作 collection(一):集合类型与操作
redo log与binlog间的破事
3D建模作品
zsh: command not found: xxx 解决方法
属性动画的使用和原理解析
C#从入门到精通
树莓派4b安装win11/10过程全教程(附蓝屏inaccessible boot device解决办法)
Temporal Segment Networks:Towards Good Practices for Deep TSN论文精读笔记
修复APP的BUG,热修复的知识点和大厂的相关资料汇总
《scala 编程(第3版)》学习笔记3
保证接口数据安全的10种方案
18张图,直观理解神经网络、流形和拓扑
Scala 中的集合(二):集合性能比较
【心率传感器与Arduino连接读取心率数据】
Arduino D1----Mlx90614红外温度传感器接线和安装包
分布式消息队列平滑迁移技术实战