Windows下自动切换IP脚本

来源:互联网 发布:杜兰特生涯数据 编辑:程序博客网 时间:2024/05/22 09:01

在不同环境使用电脑经常要切换IP,利用netsh interface ip命令可以轻松切换IP和DNS:

     netsh interface ip有以下命令:

     add            - 在一个表格中添加一个配置项。
     delete         - 从一个表格中删除一个配置项。
     dump           - 显示一个配置脚本。
     help           - 显示命令列表。
     install        - 安装 IP 协议。
     reset          - 重置 IP 配置。
     set            - 设置配置信息。
     show           - 显示信息。
     uninstall      - 卸载 IP 协议。

 

     netsh interface IP set有以下的命令:
     set address    - 设定通向接口的 IP 地址或默认网关。
     set compartment - 修改分段配置参数。
     set dnsservers - 设置 DNS 服务器模式和地址。
     set dynamicportrange - 修更改态端口分配所使用端口的范围。
     set global     - 修改全局配置常规参数。
     set interface  - 修改 IP 的接口配置参数。
     set neighbors  - 设置邻居地址。
     set route      - 修改路由参数。
     set subinterface - 修改子接口配置参数。
     set winsservers - 设置 WINS 服务器模式和地址。

 

     简单示例1:设置固定IP,保存为bat文件

   @echo off
   @rem 设置IP为192.168.0.1,子网掩码为255.255.255.0,默认网关为192.168.0.254
   netsh interface ip set address local static 192.168.0.1 255.255.255.0 192.168.0.254
   @rem 设置首选的DNS服务器192.168.0.254
   netsh interface ip set dns local 192.168.0.254
   @rem 添加DNS服务器202.106.196.115
   netsh interface ip add dns local 202.106.196.115 index=2

 

    示例2:自动获取

   @echo off   netsh interface ip set address name="local" source=dhcp   netsh interface ip set dns name="local" source=dhcp