Board logo

标题: [系统相关] 批处理如何将ping域名的ip地址写入host文件内 [打印本页]

作者: wanshuyou    时间: 2022-10-20 16:17     标题: 批处理如何将ping域名的ip地址写入host文件内

  1. @echo off
  2. set n=bathome.net
  3. for /f "delims= " %%i in ('ping %n% -n 1 -w 0 ^|findstr "^[0-9]"') do (
  4. echo;%%i locathost >%SystemRoot%\system32\drivers\etc\hosts
  5. )
  6. pause
复制代码
我看了这个觉得bat还是要花时间了解的
作者: Batcher    时间: 2022-10-20 17:52

回复 1# wanshuyou


执行这个代码的时候遇到什么问题了吗?
作者: hfxiang    时间: 2022-10-21 11:37

修改%SystemRoot%\system32\drivers\etc\hosts需要管理员权限,试试以下代码
  1. @fltmc>nul||mshta vbscript:CreateObject("Shell.Application").ShellExecute("%~dpnx0","%*",,"runas",1)(window.close)&&exit
  2. @echo off
  3. cd /d "%~dp0"
  4. set n=bathome.net
  5. for /f "delims= " %%i in ('ping %n% -n 1 -w 0 ^|findstr "^[0-9]"') do (
  6. echo;%%i locathost >>%SystemRoot%\system32\drivers\etc\hosts
  7. )
  8. pause
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2