IT运维笔记


Netsh命令详解与使用方法

Netsh是一种命令行实用程序,可用来显示与修改当前正在运行的计算机的网络配置(如:IP地址、网关、DNS和MAC地址等)。Netsh还提供脚本功能,可以对特定的计算机以批处理方式运行一组命令。Netsh使用帮助程序提供称为上下文(Context)的广泛功能集。上下文(Context)是特定于网络组件一组命令,它们通过为一个或多个服务、实用程序或协议提供配置和监视支持来扩展Netsh的功能。要运行Netsh命令,必须从命令提示符(cmd.exe)启动Netsh并更改到包含要使用的命令的上下文(Context)。可以使用的上下文(Context)取决于用户安装的网络组件。 比如,可以调用以下命令修改本地计算机的网络设置。 1、修改IP地址addr和子网掩码mask: netsh interface ip set address name="本地连接" source=static addr=192.168.0.106 mask=255.255.255.0 2、修改默认网关gateway netsh interface ip set address name="本地连接" gateway=192.168.0.1 gwmetric=0 3、修改首选(PRIMARY)的DNS netsh interface ip set dns name="本地连接" source=static addr=202.96.128.66 register=PRIMARY 如果在VC++程序中,可以调用函数WinExec来执行netsh.exe命令,简单代码如下:
//cmd命令行
CString str_cmd("");
str_cmd = "netsh interface ip set address name=\"本地连接\" source=static addr=192.168.0.106 mask=255.255.255.0"
//修改IP地址和子网掩码
::WinExec(str_cmd,SW_HIDE);