找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 22140|回复: 6

[系统相关] 批处理如何将ping域名的ip地址写入host文件内

[复制链接]
发表于 2021-7-9 15:04:58 | 显示全部楼层 |阅读模式
家中电脑使用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
复制代码
发表于 2021-7-9 15:58:00 | 显示全部楼层
本帖最后由 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
发表于 2021-7-9 16:04:01 | 显示全部楼层
echo就可以追加吧
 楼主| 发表于 2021-7-9 18:43:27 | 显示全部楼层
本帖最后由 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
 楼主| 发表于 2021-7-9 20:33:42 | 显示全部楼层
@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应该怎么修改呢?
发表于 2021-7-9 20:42:08 | 显示全部楼层
回复 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
 楼主| 发表于 2021-7-9 22:25:40 | 显示全部楼层
回复  hyq1979


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

真的可以了,谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 20:11 , Processed in 0.030296 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表