C/C++利用netsh设置动态IP和静态IP

在使用电脑时我们可以根据在更改以太网Internet协议版本4(TCP/IP)的属性来设置动态IP和静态IP

C/C++利用netsh设置动态IP和静态IP_第1张图片
C/C++利用netsh设置动态IP和静态IP_第2张图片

但是这样做很麻烦,我们可以通过程序来更改静态IP和动态IP,只需要一条简单的代码就可以实现。

静态IP:

system("cmd /c netsh interface ip set address \"以太网\" static 192.168.1.10 255.255.255.0 192.168.1.1");

动态IP:

system("cmd /c netsh interface ip set address name = \"以太网\" source = dhcp");
system("netsh interface ip set dns name = \"以太网\" source = dhcp");

显示所有网络适配器(网卡、拨号连接等)的完整TCP/IP配置信息:

system("ipconfig /all");

注意:“以太网”是根据网络连接文件夹中的名称来命名的,有的是本地连接!!!

C/C++利用netsh设置动态IP和静态IP_第3张图片

根据这两条指令可以制作一个简单的设置IP状态的小程序

测试结果:
C/C++利用netsh设置动态IP和静态IP_第4张图片

1.设置静态IP

运行程序,输入1,再次输入IP地址,子网掩码,默认网关,提示静态IP设置成功。
找到以太网Internet协议版本4(TCP/IP)的属性看到设置成功。
C/C++利用netsh设置动态IP和静态IP_第5张图片

2.输入2设置动态IP

运行程序,输入2,提示动态IP设置成功。
找到以太网Internet协议版本4(TCP/IP)的属性看到设置成功。C/C++利用netsh设置动态IP和静态IP_第6张图片

C代码:

#include
#include
#include

int main()
{
	system("ipconfig /all");
	printf("\n");
	printf("------------------------------------------------------------------------\n");
	printf("设置静态IP输入:1\n");
	printf("设置动态IP输入:2\n");
	printf("------------------------------------------------------------------------\n");
	printf("请输入:");
	int model = 0;
	scanf_s("%d", &model);
	while (model != 1 && model != 2)
	{
		printf("输入无效,请重新输入\n");
		printf("------------------------------------------------------------------------\n");
		printf("请输入:");
		scanf_s("%d", &model);
		if (model == 1 || model == 2)break;
	}
	if (model == 1)				//设置静态IP
	{
		char *ret = (char*)malloc(128);
		memset(ret, 0, sizeof(ret));
		char static_ip_1[] = "cmd /c netsh interface ip set address \"以太网\" static";
		char space[] = " ";
		printf("输入IP地址(192.168.1.110):");
		char static_ip_2[20];
		scanf_s("%s", static_ip_2, 20);		
		printf("输入子网掩码(255.255.255.0):");
		char static_mask[20];
		scanf_s("%s", static_mask, 20);		
		printf("输入默认网关(192.168.1.1):");
		char static_gateway[20] = "";
		scanf_s("%s", static_gateway, 20);
		int len1 = strlen(static_ip_1) + 1;
		strcat_s(ret, len1, static_ip_1);//"cmd /c netsh interface ip set address \"以太网\" static"
		int len2 = strlen(ret) + strlen(space) + 1;
		strcat_s(ret, len2, space);//"cmd /c netsh interface ip set address \"以太网\" static "
		int len3 = strlen(ret) + strlen(static_ip_2) + 1;
		strcat_s(ret, len3, static_ip_2);//"cmd /c netsh interface ip set address \"以太网\" static 192.168.1.110"
		int len4 = strlen(ret) + strlen(space) + 1;
		strcat_s(ret, len4, space);//"cmd /c netsh interface ip set address \"以太网\" static 192.168.1.110 "
		int len5 = strlen(ret) + strlen(static_mask) + 1;
		strcat_s(ret, len5, static_mask);//"cmd /c netsh interface ip set address \"以太网\" static 192.168.1.110 255.255.255.0"
		int len6 = strlen(ret) + strlen(space) + 1;
		strcat_s(ret, len6, space);//"cmd /c netsh interface ip set address \"以太网\" static 192.168.1.110 255.255.255.0 192.168.1.1"
		int len7 = strlen(ret) + strlen(static_gateway) + 1;
		strcat_s(ret, len7, static_gateway);
		system(ret);
		free(ret);
		printf("静态IP设置成功\n");
		printf("------------------------------------------------------------------------\n");
	}
	else if (model == 2)		//设置动态IP
	{
		system("cmd /c netsh interface ip set address name = \"以太网\" source = dhcp");
		printf("动态IP设置成功\n");
		printf("------------------------------------------------------------------------\n");
	}
	else
	{
		printf("输入错误\n");
	}
}

C++代码:

#include 

//分割线
void PrintLine()
{
	std::cout << "------------------------------------------------------------------------\n";
}

int main()
{
	system("ipconfig /all");
	std::cout << std::endl;

	PrintLine();
	std::cout << "设置静态IP输入:1\n";
	std::cout << "设置动态IP输入:2\n";
	PrintLine();

	int model = 0;
	while (1)
	{
		std::cout << "请输入:";
		std::cin >> model;

		if (model == 1 || model == 2)
			break;

		std::cout << "输入无效,请重新输入\n";
		PrintLine();
	}

	if (model == 1)				//设置静态IP
	{
		std::string static_prefix = "cmd /c netsh interface ip set address \"以太网\" static";

		std::string static_ip, static_mask, static_gateway;
		std::cout << "输入IP地址(192.168.1.110):";
		std::cin >> static_ip;
		std::cout << "输入子网掩码(255.255.255.0):";
		std::cin >> static_mask;
		std::cout << "输入默认网关(192.168.1.1):";
		std::cin >> static_gateway;

		std::string static_ret = static_prefix + " " + static_ip + " " + static_mask + " " + static_gateway;
		const char* ret = static_ret.c_str();
		system(ret);
		std::cout << "静态IP设置成功\n";
		PrintLine();
	}
	else if (model == 2)		//设置动态IP
	{
		system("cmd /c netsh interface ip set address name = \"以太网\" source = dhcp");
		std::cout << "动态IP设置成功\n";
		PrintLine();
	}
	else
	{
		std::cout << "输入错误\n";
	}
	return 0;
}


你可能感兴趣的:(C/C++,c语言,c++,tcp/ip)