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;
}
C/C++利用netsh设置动态IP和静态IP相关推荐
- 电脑ip地址设置_路由器动态IP和静态IP上网方式怎么设置【设置教程】
在我们设置路由器的过程中,路由器设置界面一般会给我们提供4种上网方式选项,分别是:"让路由器自动选择上网方式(推荐)"."PPPoE(ADSL虚拟拨号)".&q ...
- arm linux设置ip地址命令,ARM Linux系统修改动态IP为静态IP地址
# Wired or wireless interfaces auto eth0 #iface eth0 inet dhcp # pre-up /bin/grep -v -e " ...
- 动态ip、静态ip、pppoe拨号的区别
转载文章,仅供参考: 动态ip.静态ip.pppoe拨号的区别 - 电子发烧友网电子发烧友为您提供的动态ip.静态ip.pppoe拨号的区别,在设置无线路由器上网时,上方方式有pppoe拨号(宽带拨号 ...
- 动态IP与静态ip的区别是什么
1.DHCP IP即动态ip,可以自动获取IP地址.静态ip上网又被称为固定IP地址上网,需要手动设置IP地址. 2.在网速上,动态ip和静态ip没有区别. 3.动态ip不是一个真实的IP地址,静态I ...
- 动态ip与静态ip的区别是什么?
动态ip和静态ip的区别在于: 动态ip可以自动获取IP地址,静态ip需要手动设置IP地址. 动态ip一般用于局域网内部或小型企业网中,静态ip一般用于大型企业网或骨干网等对安全性要求高的场合. 在网 ...
- 爬虫动态IP和静态IP的区别
动态ip地址指的是在需要的时候才进行动态ip地址分配的方式.动态ip地址和静态ip地址是对应的.所谓动态就是指当你每一次上网时,运营商会随机分配一个动态ip地址,静态指的是固定分配一个动态ip地址,每 ...
- win7计算机静态ip,win7电脑动态IP和静态IP有什么差别?如何区分动态IP和静态IP
很多用户朋友不知道win7电脑动态IP和静态IP有什么差别?动态IP和静态IP的差别在于:动态IP需要在连接网络时自动获取IP地址以供用户正常上网,而静态IP是ISP在装机时分配给用户的IP地址,可以 ...
- 动态ip与静态ip的概念、区别、应用场景
动态ip与静态ip的区别 前言 一.介绍IP地址的概念和作用 1.1.IP地址的定义 1.2.IP地址的作用 二.动态IP和静态IP的区别 2.1.动态IP和静态IP的定义 2.2.动态IP和静态IP ...
- 你知道动态IP和静态IP的真正区别吗?点击查看详细解析!
一.动态IP和静态IP的基本概念 二.动态IP和静态IP的区别 三.动态IP和静态IP各自的优势 四.动态IP和静态IP的应用场景 五.总结 Aorta Cloud 厦门主动脉云科技 一.动态IP和静 ...
- 设置数据库服务器电脑固定IP,指定IP,静态IP的方法教程,WIn7电脑如何设置固定IP的方法,设置电脑的指定IP
设置数据库服务器电脑固定IP,指定IP,静态IP的方法教程,WIn7电脑如何设置固定IP的方法,设置电脑的指定IP_哔哩哔哩_bilibili本期视频讲解:如何将服务器电脑的IP地址改成固定IP.ht ...
最新文章
- Educational Codeforces Round 105 (Rated for Div. 2) C. 1D Sokoban
- 使用迭代查找一个list中最小和最大值,并返回一个tuple。
- ovs-ofctl: s1 is not a bridge or a socket 解决方法
- 阿里笔试题—战报交流
- Eclipse Spring Boot实现热部署
- python——logging模块
- Boost:字符串裁剪Trim的测试程序
- SVN使用log,list,cat,diff命令查看特定文件版本信息
- 不用任何插件实现 WordPress 的彩色标签云
- 关于React-native的介绍以及环境搭建
- Oracle Comment 获取并修改表或字段注释
- Nginx作为web服务器的安装配置
- nosql第二章课后习题
- HTTP权威指南学习笔记:连接管理
- 印刷文字的字体与字号规定
- MyBatisPlus多表关联查询,返回list
- python librosa将哼唱旋律转换为音符
- 美妆行业如何在小红书营销推广?美妆品牌的线上推广怎么宣传更有效果?
- 指令集创始人潘爱民受邀出席CCF系统软件技术论坛,探讨泛在操作系统技术发展...
- Linux驱动-字符设备驱动