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

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

在这里插入图片描述
在这里插入图片描述

但是这样做很麻烦,我们可以通过程序来更改静态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");

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

在这里插入图片描述

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

测试结果:
在这里插入图片描述

1.设置静态IP

运行程序,输入1,再次输入IP地址,子网掩码,默认网关,提示静态IP设置成功。
找到以太网Internet协议版本4(TCP/IP)的属性看到设置成功。
在这里插入图片描述

2.输入2设置动态IP

运行程序,输入2,提示动态IP设置成功。
找到以太网Internet协议版本4(TCP/IP)的属性看到设置成功。在这里插入图片描述

C代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

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 <iostream>

//分割线
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;
}


  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值