• Delphi 调用netsh命令修改IP地址


    Delphi 调用netsh命令修改IP地址
    先介绍一下Netsh命令的使用方法:

    在这里跟大家介绍几个简单的指令
    1.Show IP
    1.1Cmd Mode
    直接在cmd下面输入
    netsh interface ip show address
    亦可简写为
    netsh int ip sh ad
    看看,指令是不是和Cisco的nos指令很像!非常怀疑是抄袭Cisco的。 1.2Netsh Mode
    您也可以进入netsh的命令模式下
    netsh //进入到 netsh mode
    netsh>int //进入到 interface 子选项。
    interface>ip //進入到 ip 子选项。
    interface ip>show
    show address - 显示 IP 位址。
    show config - 显示 IP 位址及其他资料。
    show dns - 显示 DNS 服务器位址。
    show icmp - 显示 ICMP 统计
    show interface - 显示 IP 介面统计
    show ipaddress - 显示 IP 位址
    show ipnet - 显示 IP net-to-media 对应
    show ipstats - 显示 IP 统计
    show joins - 显示加入的多点传送群组
    show offload - 显示 offload 内容
    show tcpconn - 显示 TCP 连线
    show tcpstats - 显示 TCP 统计
    show udpconn - 显示 UDP 连线
    show udpstats - 显示 UDP 统计
    show wins - 显示 WINS 服务器位址。
    2.Set IP
    下列是所有可用的指令。
    这个內容中的指令:
    set address - 在指定的介面设定 IP 位址或预设网关。
    set dns - 设定 DNS 服务器模式及位址。
    set wins - 设定 WINS 服务器模式及位址。2.1.设定IP位址
    2.1.1.DHCP设定
    若您希望由DHCP取得IP位址可输入
    interface ip>set ad "区域连接" DHCP
    或简写成
    interface ip>set ad "区域连接" D2.1.2.静态IP设定
    2.1.2.1.设定IP位址与子网关
    netsh -进入到 netsh mode
    netsh>int -进入到 interface 子选项。
    interface>ip -进入到 ip 子选项。
    interface ip>set address name = "区域连接" source = static addr = 10.2.2.100 mask = 255.255.255.0
    可简写成
    interface ip>set ad "区域连接" s 10.2.2.100 255.255.255.02.1.2.2.设定IP路由
    interface ip>set address name = "区域连接" gateway = 10.2.2.254 gwmetric = 1
    可简写成
    interface ip>set ad "区域连接" ga=10.2.2.254 gw = 12.1.2.3同时设定IP位址和路由
    interface ip>set address name = "区域连接" source = static addr = 10.2.2.100 mask = 255.255.255.0 gateway = 10.2.2.254 gwmetric = 1
    可简写成
    interface ip>set ad "区域连接" s 10.2.2.100 255.255.255.0 10.2.2.254 1设定完後,记得用sh ad去看一下设定的對不對。2.3设定DNS来源
    若是由DHCP取得,请输入
    interface ip>set dns "区域连接" source=dhcp
    若是使用静态设定,请输入
    interface ip>set dns name = "区域连接" source = static addr = 10.2.5.2
    新增第二組DNS,请输入
    interface ip>add dns name = "区域连接" addr = 10.2.5.32.4设定WINS来源
    若是由DHCP取得,请输入
    interface ip>set wins "区域连接" source=dhcp
    若是使用静态设定,请输入
    interface ip>set wins name = "区域连接" source = static addr = 10.2.5.10
    新增第二組WINS,请输入
    interface ip>add wins name = "区域连接" addr = 10.2.5.17
    3.将网路状态设定导出/导入
    3.1导出
    netsh -c interface dump >c:/netset.txt
    当然,interface可以简写成int,dump更可简化成d,所以就变成了
    netsh -c int d >c:/netset.txt
    3.2导入
    netsh -f c:/netset.txt 既可在Delphi中调用过程:
    //设置成自动获取IP:
    NetShTxt := 'netsh interface ip set address ' + '"' + NetWork + '"' + ' source=dhcp';
    winexec(pchar(NetShTxt), sw_hide);
    //以下代码设置IP地址和网关:
    NetShTxt := 'netsh interface ip set address name=' + '"' + NetWork + '"' + ' source=static addr=' + IPAddress.IPString + ' mask=' + submask.IPString;
    winexec(pchar(NetShTxt), sw_hide);
    //网关
    NetshTxt := 'netsh interface ip set address name=' + '"' + NetWork + '"' + ' gateway=' + DefaultGateway.IPString + ' gwmetric=0';
    winexec(pchar(NetShTxt), sw_hide);
    其中使用了IPEdit,这个组件,经过修改,已经基本能用于IP地址的输入和获取.本来从网上下载的以为能用,可实际用的过程中会发现不少问题.
    设置DNS:
    NetshTxt := 'netsh interface ip set dns name=' + '"' + NetWork + '"' + ' source=static addr=' + FirstDns.IPString + ' register=PRIMARY';
    winexec(pchar(NetShTxt), SW_Hide);
    //netsh interface ip add dns name="本地连接" addr=你的DNS的IP地址 index=2
    NetshTxt := 'netsh interface ip add dns name=' + '"' + NetWork + '"' + ' addr=' + SecondDNS.IPString + ' index=2';
    注意以上第二个DNS,添加用的命令:
    //netsh interface ip add dns name="本地连接" addr=你的DNS的IP地址 index=2
    第三个DNS:
    //netsh interface ip add dns name="本地连接" addr=你的DNS的IP地址 index=3后期调用winExec执行,感觉很不错,注意生成后使用GetAdaptersInfo函数进行检查一下IP地址是否设置正确.
    在网络上流传的好多类似的工具都是这个原理.

    设为dhcp   
       winexec(
    'netsh    interface    ip    set    address    本地连接1    dhcp',
    SW_HIDE);   
        
       设为手工ip   
       WinExec(
    'netsh    interface    ip    set    address    本地连接1    static    192.168.1.189    255.255.255.0    192.168.1.1    1',SW_HIDE);
     
    设置IP地址  
      shellexecute(handle,'','netsh   interface   ip   set   address   '   +   SRegedit   +  
                                'static   192.168.0.1   255.255.255.0   192.168.0.10   1',nil,nil,sw_hide)
  • 相关阅读:
    如何在Oracle官网下载java的JDK最新版本和历史版本
    屏幕录制专家【Bandicam】
    Bandicam下载 + 破解
    华为荣耀7i手动更改DNS,提高网页加载速度
    SQL中使用GROUP BY注意事项
    JavaScript数据类型判断
    React.lazy和Suspense组合实现组件懒加载
    使用React+TypeScript构建自己的组件库
    leetcode-0101 对称二叉树
    leetcode-0543 二叉树的直径
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/4643111.html
Copyright © 2020-2023  润新知