首页
社区
课程
招聘
[原创]cmd中netsh命令管理网卡的一些基本操作
2023-3-24 15:44 5483

[原创]cmd中netsh命令管理网卡的一些基本操作

2023-3-24 15:44
5483

最近使用C++ 11来实现Windows的网卡管理监测功能,尝试Win32调用系统API,甚至DeviceIoControl这样的API都用上了,那编程的感觉,挺难忘的,最后还加了COM编程,想想,又没有什么特殊要求,为什么不直接用cmd命令来实现呢,放过自己,少点折腾,后期维护也比起那一堆COM接口和API容易的多

 

查看各个网卡状态

1
netsh interface show interface

查看各个网卡信息

1
netsh interface ip show config

后续修改网卡状态、信息等操作需要提权。VS中这样设置

 

启用被禁用的网卡

1
netsh interface set interface "网卡名" admin=enable

设置DHCP分配IP

1
netsh interface ip set address name="网卡名" source=dhcp

手动设置静态IP地址

1
2
3
4
5
netsh interface ip set address name="网卡名" static  IP  subnet_mask  gateway;
 
eg.
 
netsh interface ip set address name="以太网" static  192.168.168.123  255.255.255.0  192.168.168.2;

设置DHCP分配DNS服务器地址

1
netsh interface ip set dns name="网卡名" source=dhcp

手动设置首选DNS服务器地址

1
netsh interface ip set dns "Ethernet0" static 192.168.242.2 primary

手动设置备用DNS服务器地址

1
netsh interface ip add dns "Ethernet0" 8.8.8.8 index=2

删除备用DNS服务器地址

1
netsh interface ip delete dnsservers "Ethernet0" 114.114.114.114

C++打印当前系统时间

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <chrono>
#include <ctime>
 
void PrintCurrentTime()
{
    auto current_time = std::chrono::system_clock::now();
    std::time_t current_time_t = std::chrono::system_clock::to_time_t(current_time);
 
    // 输出当前时间
    std::cout << std::endl << "当前系统时间为: " << std::ctime(&current_time_t) << std::endl;
}

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
点赞0
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回