Board logo

标题: [文本处理] [已解决]根据mac地址获取IP并写入指定文件的批处理 [打印本页]

作者: newgain    时间: 2019-9-3 16:37     标题: [已解决]根据mac地址获取IP并写入指定文件的批处理

本帖最后由 newgain 于 2019-9-4 16:57 编辑

根据指定的MAC地址获取IP,然后把获取到的IP写入另一个文件的指定位置,如图1,图2。
运行后自动根据mac地址a4-4c-c8-17-97-c6获取ip,然后把获取到的IP写入c:\test\producer.properties文件中图二红框的位置覆盖原来的ip,
这样的程序怎么写,求。




作者: terse    时间: 2019-9-3 19:51

  1. @echo off & setlocal enabledelayedexpansion
  2. (for /f %%a in ('arp -a ^| find  /i a4-4c-c8-17-97-c6"') do (
  3.      for /f "delims=:" %%b in ('findstr  /inbc:"url=" "c:\test\producer.properties"') do (
  4.           for /f  "tokens=1* delims=:" %%i in ('findstr /n .* "c:\test\producer.properties"') do (
  5.                 set "str=%%j"
  6.                 if %%i equ %%b (echo;!str:*\=url=%%a\!) else echo;%%j  
  7.            )
  8.      )
  9. ))>tem.t
  10. move tem.t  "c:\test\producer.properties"
  11. pause
复制代码

作者: newgain    时间: 2019-9-4 08:23

运行后出现了一个小锁

文件中一个出现乱码
  1. 锘縗#Update=value
复制代码

作者: terse    时间: 2019-9-4 08:56

原文件编码?
作者: newgain    时间: 2019-9-4 09:09

回复 4# terse

原文编码不太懂,我一般arp-a查看,然后记录下来IP地址直接用记事本打开文件修改IP。
作者: Batcher    时间: 2019-9-4 11:31

回复 5# newgain


用记事本打开producer.properties
文件
另存为
能看到编码是什么
作者: newgain    时间: 2019-9-4 11:36

回复 6# Batcher


    编码 ANSI
作者: terse    时间: 2019-9-4 14:42

按utf-8码处理呢
  1. 1>1/* :
  2. @echo off
  3. set mac=a4-4c-c8-17-97-c6
  4. for /f %%i in ('arp -a ^| find  /i "%mac%"') do set ip=%%~i
  5. for /f "delims=:" %%i in ('findstr  /inbc:"url=" "c:\test\producer.properties"') do set i=%%i
  6. setlocal enabledelayedexpansion
  7. cscript.exe -nologo -e:jscript "%~0" "c:\test\producer.properties" "!i!" "!ip!"
  8. pause & exit
  9. */
  10. function adoText(file, len, ip) {
  11.        var t,stream,lines = [];
  12.        var re = /^(url=)?(\d{1,3}\.){3}(\d{1,3})/;
  13.        stream = new ActiveXObject("ADODB.Stream");
  14.        stream.type = 2;
  15.        stream.charset = 'utf-8';
  16.        stream.open();
  17.        stream.loadFromFile(file);
  18.        while (!stream.EOS) {
  19.              t = stream.readText(-2);
  20.              if (lines.length == len) {t = t.replace(re, '$1' + ip)};
  21.              lines.push(t);
  22.        }
  23.        stream.Position = 0;
  24.        stream.writeText(lines.join('\r\n'));
  25.        stream.saveToFile(file, 2);
  26.        stream.close();
  27. }
  28. adoText(WSH.Arguments.Item(0),WSH.Arguments.Item(1)-1,  WSH.Arguments(2));
复制代码

作者: newgain    时间: 2019-9-4 16:58

回复 8# terse


    非常感谢,试了一下很好!
作者: 窄口牛    时间: 2019-9-5 09:17

我这里失败
提示
.bat(1,6)microsoft jscript 编译错误:条件编译已关闭
作者: terse    时间: 2019-9-5 14:13

回复 10# 窄口牛
第一行换这个看看呢
  1. @set @i=0 /*
复制代码

作者: 窄口牛    时间: 2019-9-5 15:35

了解,谢谢指导。




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