当前位置:网站首页>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文件
边栏推荐
猜你喜欢
随机推荐
Spark MLlib特征处理 之 StringIndexer、IndexToString使用说明以及源码剖析
Visual Studio2022创建setup项目
《scala 编程(第3版)》学习笔记4
synchronized锁原理详解
2022年中高级 Android 大厂面试秘籍,为你保驾护航金九银十,直通大厂
浅谈性能优化:APP的启动流程分析与优化
MPU6050 加速度计和陀螺仪传感器与 Arduino 连接
【Arduino 连接GP2Y1014AU0F 灰尘传感器】
Two-Stream Convolutional Networks for Action Recognition in Videos双流网络论文精读
基于阿里云OSS+PicGo的个人图床搭建
面试知识点整理:Skia 架构的场景渲染
whistle 手机调试代理工具
Laravel 的关联模型 及其 预加载多个关联 with使用方法
机器学习相关 概率论重点笔记
C# Form按ESC关闭窗体
完全背包问题(动态规划)
Windows下MySQL数据库报“ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:8000‘ (10061)”错误解决
无源域适应(SFDA)方向的领域探究和论文复现(第一部分)
修复APP的BUG,热修复的知识点和大厂的相关资料汇总
三相同步发电机的空载短路的simulink仿真