当前位置:网站首页>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 retrievedhostsWhether 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文件
边栏推荐
猜你喜欢

Visual Studio2022创建setup项目

物联网方案

gradle脚本中groovy语法讲解

【nRF24L01 与 Arduino 连接实现无线通信】

【opencv】error: (-215:Assertion failed) ssize.empty() in function ‘cv::resize‘报错原因

从Attention到Self-Attention和Multi-Head Attention

日志分析系统:ELK

连接本地MySql时出现2003-Can‘t connect to MySql server on ‘localhost‘(10061)

《scala 编程(第3版)》学习笔记3

Go 程序太大了,能要个延迟初始化不?
随机推荐
中国大陆开源镜像站汇总
Typora使用
3D建模作品
C语言上机题(基础)
网站开发方案研究
在 UUP dump 被墙的情况下如何用 UUP 下载 ISO 镜像
[Spark]-RDD详解之变量&操作
【TCS3200 颜色传感器与 Arduino 实现颜色识别】
属性动画的使用和原理解析
保证接口数据安全的10种方案
【nRF24L01 与 Arduino 连接实现无线通信】
[Spark]-协同过滤
sacalatest AnyFunSuite:no implicits found for parameter pos
Acwing:哈夫曼树(详解)
Vision Transformer(ViT)论文精读和Pytorch实现代码解析
Visual Studio2022创建setup项目
【Arduino 连接DHT11 湿度和温度传感器】
面试知识点整理:Skia 架构的场景渲染
Out of memory error on GPU 0. Cannot allocate xxxGB memory on GPU 0, available memory is only xxx
【心率传感器与Arduino连接读取心率数据】