标题: [注册表类] 批处理怎么读取hosts文件中的IP来设置注册表? [打印本页]
作者: Heisenberg 时间: 2015-2-8 19:46 标题: 批处理怎么读取hosts文件中的IP来设置注册表?
已知hosts文件,格式如下:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
10.123.1.21 W0222201
10.123.1.22 W0222202
10.123.1.31 ES02222101
10.123.1.32 ES02222102
10.123.1.33 ES02222103
10.123.1.34 ES02222104
10.123.1.35 ES02222105
10.123.1.36 ES02222106
10.123.1.37 ES02222107
问题:能否写一个vbs或者bat,可以实现以下步骤:
1;读取10.123.1.21,并将注册表下某项值设为W0222201;读取10.123.1.22,并将注册表下某项值设为W0222202;
2;将本机的ip与hosts文件中的ip比较(从10.123.1.31开始比较),如在hosts文件中存在,则将注册表下主机名项改成右边项的值(10.123.1.31的主机名改成ES02222101)
3;将本机的ip与hosts文件中的ip比较(从10.123.1.31开始比较),如在hosts文件中存在,则将注册表下另一项改成右边列的后三位(10.123.1.31的另一项改成101)
作者: pcl_test 时间: 2015-2-8 20:18
本帖最后由 pcl_test 于 2015-2-8 20:20 编辑
回复 1# Heisenberg
1、什么系统?(最好贴出ipconfig命令的信息)
2、给出要修改的注册表项的位置
作者: Heisenberg 时间: 2015-2-8 20:54
回复 2# pcl_test
win7系统
注册表位置无所谓,重点是改用什么函数
作者: pcl_test 时间: 2015-2-8 22:56
本帖最后由 pcl_test 于 2015-2-8 23:25 编辑
回复 3# Heisenberg
注册表位置无所谓的话,你自己把代码添加进去吧,reg- @echo off
- setlocal enabledelayedexpansion
- ::指定hosts所在目录
- cd /d "C:\windows\system32\drivers\etc\"
- for /f "tokens=2 delims=:" %%i in ('ipconfig ^| findstr "IPv4"') do set "localip=%%i"
-
- for /f "eol=# delims=" %%j in ('type "hosts"') do echo %%j>>$$1
- set n=1
- for /f "tokens=1*" %%a in ($$1) do (
- if !n! lss 3 (
- echo %%a %%b>>$$2
- ) else (
- echo %%a %%b>>$$3
- )
- set /a n+=1
- )
-
- set m=1
- for /f "tokens=1*" %%s in ($$2) do (
- if !m! equ 1 (
- echo 读取%%s,执行将注册表下某项值设为%%t
- ) else (
- echo 读取%%s,执行将注册表下某项值设为%%t
- )
- set /a m+=1
- )
-
- for /f "tokens=1*" %%e in ($$3) do (
- if %localip% equ %%e (
- echo 执行修改主机名为%%f
- set "number=%%f"
- echo 执行修改后三位!number:~-3!
- pause
- del $$* /s /q >nul
- exit
- )
- )
- echo 没有找到匹配项
-
- del $$* /s /q >nul
- pause
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |