Board logo

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

作者: hyq1979    时间: 2021-7-9 15:04     标题: 批处理如何将ping域名的ip地址写入host文件内

家中电脑使用pppoe上网,搭建服务器可是ip地址经常变动,最后申请动态域名与ip绑定。
需求如下:
将ping域名返回的ip地址再加域名写入hosts文件内(没有后缀名)

hosts内容大概如下
192.168.1.73       localhost

目前只会将当前目录hosts文件批处理存到系统目录
  1. @echo off
  2. copy hosts %SystemRoot%\system32\drivers\etc\hosts /Y
  3. pause
复制代码

作者: idwma    时间: 2021-7-9 15:58

本帖最后由 idwma 于 2021-7-9 19:09 编辑

@echo off
set n=bathome.net
for /f "delims= " %%i in ('ping %n% -n 1 -w 0 ^|findstr "^[0-9]"') do (
echo;%%i locathost >%SystemRoot%\system32\drivers\etc\hosts
)
pause
作者: sinsky    时间: 2021-7-9 16:04

echo就可以追加吧
作者: hyq1979    时间: 2021-7-9 18:43

本帖最后由 hyq1979 于 2021-7-9 18:46 编辑
@echo off
set n=bathome.net
for /f "delims= " %%i in ('ping %n% -n 1 -w 0 ^|findstr "^[0-9]"') do  ...
idwma 发表于 2021-7-9 15:58


谢谢解答,hosts这个文件是让windows系统对特定域名重新定向至ip地址,能否获取到ip后补充域名写入hosts内呢?
比如:
脚本ping bathome.net 的ip为 192.168.1.1,然后加上locathost这个域名,写入hosts文件
如下
192.168.1.1    locathost
作者: hyq1979    时间: 2021-7-9 20:33

@echo off
set n=bathome.net
for /f "delims= " %%i in ('ping %n% -n 1 -w 0 ^|findstr "^[0-9]"') do  ...
idwma 发表于 2021-7-9 15:58

谢谢,可以用了!

再问一下,如果多个域名也要改相同ip应该怎么修改呢?
作者: idwma    时间: 2021-7-9 20:42

回复 5# hyq1979


    @echo off
set n=bathome.net
for /f "delims= " %%i in ('ping %n% -n 1 -w 0 ^|findstr "^[0-9]"') do (
echo;%%i locathost1 >>%SystemRoot%\system32\drivers\etc\hosts
echo;%%i locathost2 >>%SystemRoot%\system32\drivers\etc\hosts
echo;%%i locathost3 >>%SystemRoot%\system32\drivers\etc\hosts
echo;%%i locathost4 >>%SystemRoot%\system32\drivers\etc\hosts
echo;%%i locathost5 >>%SystemRoot%\system32\drivers\etc\hosts
echo;%%i locathost6 >>%SystemRoot%\system32\drivers\etc\hosts
echo;%%i locathost7 >>%SystemRoot%\system32\drivers\etc\hosts
echo;%%i locathost8 >>%SystemRoot%\system32\drivers\etc\hosts

)
pause
作者: hyq1979    时间: 2021-7-9 22:25

回复  hyq1979


    @echo off
set n=bathome.net
for /f "delims= " %%i in ('ping %n% -n 1 -w 0  ...
idwma 发表于 2021-7-9 20:42

真的可以了,谢谢!




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