批处理文件来获取本地连接名称在Windows XP [英] Batch File to retrieve Local Area Connection Name in Windows XP

查看:695
本文介绍了批处理文件来获取本地连接名称在Windows XP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个简单的批处理脚本自动设置LAN连接的DNS。下面是脚本:

I'm currently writing a simple batch script to set the DNS of a LAN connection automatically. Here is the script:

REM Set DNS 
netsh interface ip set dns name="Local Area Connection" static X.X.X.X
netsh interface ip add dns name="Local Area Connection" Y.Y.Y.Y index=2
netsh interface ip add dns name="Local Area Connection" Z.Z.Z.Z index=3

但事实是,如果局域网的名称不是默认(即本地连接),该脚本将无法工作。

But the thing is, if the Local Area Network name is not default (i.e. Local Area Connection), the script will not work.

有没有什么办法可以检测所有本地连接的名称和使用批处理文件中设置的所有这些LAN连接DNS的?

Is there any way I can detect all the Local Area Connection names and set all of those LAN connections' DNS using the batch file?

任何帮助将AP preciated:)

Any help will be appreciated :)

推荐答案

我在测试这code的 Windows 7的的。您可能需要做一些修改了的的Windows XP

I've tested this code in Windows 7. You may need to make some modifications for Windows XP.

@Echo Off
For /f "skip=2 tokens=4*" %%a In ('NetSh Interface IPv4 Show Interfaces') Do (
    Call :UseNetworkAdapter %%a "%%b"
)
Exit /B

:UseNetworkAdapter
:: %1 = State
:: %2 = Name (quoted); %~2 = Name (unquoted)
If %1==connected (
    :: Do your stuff here, for example:
    Echo %2
)
Exit /B

我就注意到,我总是用呼叫语句,而不是括号内的脚本。人们常常感到困惑时,如预期在括号脚本环境变量不规矩。我觉得调用标签使得脚本更容易使用。

I'll just note that I always use Call statements rather than bracketed script. Too often people become confused when environment variables don't behave as expected in bracketed script. I find calling a label makes script easier to work with.

编辑: Explination

Explination.

命令读取文件中的每一行或命令结果。结果
在('命令')告诉它读命令的结果的每一行。结果
跳过= 2 跳过输出的前两行,在这种情况下,列标题。结果
标记= 4 * 表示,阅读每行的第四件事作为一个变量( 4 ),和一切之后作为另一个变量( * )。
%% A 表示在上面的标记%% A 存储%% b 分别结果
DO(命令)执行命令的每一行。

The For command reads each line of a file or command result.
In ('command') tells it to read each line of the results of command.
skip=2 skips the first two lines of output, in this case, the column header.
tokens=4* says to read the fourth thing on each line as one variable (4), and everything after that as another variable (*). %%a says to store the above tokens in %%a and %%b respectively.
Do (commands) executes the commands for each line.

我的 netsh接口的IPv4显示接口输出是:

Idx     Met         MTU          State                Name
---  ----------  ----------  ------------  ---------------------------
  1          50  4294967295  connected     Loopback Pseudo-Interface 1
 15          50        1500  disconnected  Bluetooth Network Connection
 24          10        1500  connected     Network Bridge

所以我把第四令牌(州)和(名称)终究令牌,并将其传递给一个脚本函数调用。在这里,他们被检索作为命令行参数,即%1 %2 。结果
请注意,每个名称由因空间的两个或三个令牌,因此使用 * 而不是 5

这篇关于批处理文件来获取本地连接名称在Windows XP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆