单地址); /*使用ioctl设置Ip地址*/ ioctl(fd、SIOCSIFADDR和ifr); /*关闭fd*/ 关闭(fd); printf(“IP地址更新成功。\n”); /*更新后获取Ip地址*/ /*用0x20-空格清除ip_地址缓冲区*/ memset((无符号字符*)ip_地址,0x20,15); ioctl(fd、SIOCGIFADDR和ifr); /*提取Ip地址*/ strcpy(ip地址,inet\u ntoa(((结构sockaddr\u in*)和ifr.ifr\u addr)->sin\u addr)); printf(“更新的IP地址为:%s\n”,IP\u地址); 返回0; },c++,networking,C++,Networking" /> 单地址); /*使用ioctl设置Ip地址*/ ioctl(fd、SIOCSIFADDR和ifr); /*关闭fd*/ 关闭(fd); printf(“IP地址更新成功。\n”); /*更新后获取Ip地址*/ /*用0x20-空格清除ip_地址缓冲区*/ memset((无符号字符*)ip_地址,0x20,15); ioctl(fd、SIOCGIFADDR和ifr); /*提取Ip地址*/ strcpy(ip地址,inet\u ntoa(((结构sockaddr\u in*)和ifr.ifr\u addr)->sin\u addr)); printf(“更新的IP地址为:%s\n”,IP\u地址); 返回0; },c++,networking,C++,Networking" />

在Linux中使用c++; 我想设置我的IP、子网、DHCP地址,但是我需要用C++来完成。我在互联网上进行了研究,并尝试了一些代码。不幸的是,它不起作用。还有一件事,运行它应该是安全的,我的意思是我不能使用需要特权的函数,比如system()等等。你能给我什么建议吗 #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <arpa/inet.h> int main() { char ip_address[15]; int fd; struct ifreq ifr; struct sockaddr_in *addr; /*Read IP Address*/ printf("Enter Ip Address: "); scanf("%s",ip_address); /*AF_INET - to define network interface IPv4*/ /*Creating soket for it.*/ fd = socket(AF_INET, SOCK_DGRAM, 0); /*AF_INET - to define IPv4 Address type.*/ ifr.ifr_addr.sa_family = AF_INET; /*eth0 - define the ifr_name - port name where network attached.*/ memcpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); /*defining the sockaddr_in*/ addr=(struct sockaddr_in *)&ifr.ifr_addr; /*convert ip address in correct format to write*/ inet_pton(AF_INET,ip_address,&addr->sin_addr); /*Setting the Ip Address using ioctl*/ ioctl(fd, SIOCSIFADDR, &ifr); /*closing fd*/ close(fd); printf("IP Address updated sucessfully.\n"); /*Getting the Ip Address after Updating.*/ /*clear ip_address buffer with 0x20- space*/ memset((unsigned char*)ip_address,0x20,15); ioctl(fd, SIOCGIFADDR, &ifr); /*Extracting Ip Address*/ strcpy(ip_address,inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); printf("Updated IP Address is: %s\n",ip_address); return 0; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 int main() { 字符ip_地址[15]; int-fd; 结构ifreq-ifr; *地址中的结构sockaddr_; /*读取IP地址*/ printf(“输入Ip地址:”); scanf(“%s”,ip_地址); /*AF_INET-定义网络接口IPv4*/ /*为它创建soket*/ fd=插座(AF INET,插座DGRAM,0); /*AF_INET-定义IPv4地址类型*/ ifr.ifr\u addr.sa\u family=AF\u INET; /*eth0-定义ifr_名称-端口名称 网络连接的位置*/ memcpy(ifr.ifr_名称,“eth0”,IFNAMSIZ-1); /*在中定义sockaddr_*/ addr=(struct sockaddr\u in*)和ifr.ifr\u addr; /*将ip地址转换为正确的写入格式*/ 网络地址(自动网络、ip地址和地址->单地址); /*使用ioctl设置Ip地址*/ ioctl(fd、SIOCSIFADDR和ifr); /*关闭fd*/ 关闭(fd); printf(“IP地址更新成功。\n”); /*更新后获取Ip地址*/ /*用0x20-空格清除ip_地址缓冲区*/ memset((无符号字符*)ip_地址,0x20,15); ioctl(fd、SIOCGIFADDR和ifr); /*提取Ip地址*/ strcpy(ip地址,inet\u ntoa(((结构sockaddr\u in*)和ifr.ifr\u addr)->sin\u addr)); printf(“更新的IP地址为:%s\n”,IP\u地址); 返回0; }

在Linux中使用c++; 我想设置我的IP、子网、DHCP地址,但是我需要用C++来完成。我在互联网上进行了研究,并尝试了一些代码。不幸的是,它不起作用。还有一件事,运行它应该是安全的,我的意思是我不能使用需要特权的函数,比如system()等等。你能给我什么建议吗 #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <arpa/inet.h> int main() { char ip_address[15]; int fd; struct ifreq ifr; struct sockaddr_in *addr; /*Read IP Address*/ printf("Enter Ip Address: "); scanf("%s",ip_address); /*AF_INET - to define network interface IPv4*/ /*Creating soket for it.*/ fd = socket(AF_INET, SOCK_DGRAM, 0); /*AF_INET - to define IPv4 Address type.*/ ifr.ifr_addr.sa_family = AF_INET; /*eth0 - define the ifr_name - port name where network attached.*/ memcpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); /*defining the sockaddr_in*/ addr=(struct sockaddr_in *)&ifr.ifr_addr; /*convert ip address in correct format to write*/ inet_pton(AF_INET,ip_address,&addr->sin_addr); /*Setting the Ip Address using ioctl*/ ioctl(fd, SIOCSIFADDR, &ifr); /*closing fd*/ close(fd); printf("IP Address updated sucessfully.\n"); /*Getting the Ip Address after Updating.*/ /*clear ip_address buffer with 0x20- space*/ memset((unsigned char*)ip_address,0x20,15); ioctl(fd, SIOCGIFADDR, &ifr); /*Extracting Ip Address*/ strcpy(ip_address,inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); printf("Updated IP Address is: %s\n",ip_address); return 0; } #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 int main() { 字符ip_地址[15]; int-fd; 结构ifreq-ifr; *地址中的结构sockaddr_; /*读取IP地址*/ printf(“输入Ip地址:”); scanf(“%s”,ip_地址); /*AF_INET-定义网络接口IPv4*/ /*为它创建soket*/ fd=插座(AF INET,插座DGRAM,0); /*AF_INET-定义IPv4地址类型*/ ifr.ifr\u addr.sa\u family=AF\u INET; /*eth0-定义ifr_名称-端口名称 网络连接的位置*/ memcpy(ifr.ifr_名称,“eth0”,IFNAMSIZ-1); /*在中定义sockaddr_*/ addr=(struct sockaddr\u in*)和ifr.ifr\u addr; /*将ip地址转换为正确的写入格式*/ 网络地址(自动网络、ip地址和地址->单地址); /*使用ioctl设置Ip地址*/ ioctl(fd、SIOCSIFADDR和ifr); /*关闭fd*/ 关闭(fd); printf(“IP地址更新成功。\n”); /*更新后获取Ip地址*/ /*用0x20-空格清除ip_地址缓冲区*/ memset((无符号字符*)ip_地址,0x20,15); ioctl(fd、SIOCGIFADDR和ifr); /*提取Ip地址*/ strcpy(ip地址,inet\u ntoa(((结构sockaddr\u in*)和ifr.ifr\u addr)->sin\u addr)); printf(“更新的IP地址为:%s\n”,IP\u地址); 返回0; },c++,networking,C++,Networking,到目前为止,我一直在尝试这个代码。运行此命令后,我会检查ifconfig,但没有任何更改。好吧,也许您应该检查所有正在进行的系统调用的返回值。这是一个很好的习惯,要尽早养成:不要只希望所有的系统调用都能正常工作,并且假设是这样。始终检查每个系统调用的返回值。然后检查所有正在进行的系统调用的手册页,看看是否必须是root用户才能进行调用。如果您没有权限,我怀疑您是否可以设置网络设备的IP地址。调用system()不需要权限。即使您可以将计算机的IP地址更改为随机值,这将如何影响网络的其他部分?例如

到目前为止,我一直在尝试这个代码。运行此命令后,我会检查
ifconfig
,但没有任何更改。

好吧,也许您应该检查所有正在进行的系统调用的返回值。这是一个很好的习惯,要尽早养成:不要只希望所有的系统调用都能正常工作,并且假设是这样。始终检查每个系统调用的返回值。然后检查所有正在进行的系统调用的手册页,看看是否必须是root用户才能进行调用。如果您没有权限,我怀疑您是否可以设置网络设备的IP地址。调用system()不需要权限。即使您可以将计算机的IP地址更改为随机值,这将如何影响网络的其他部分?例如,如果您选择与我相同的地址,是否会突然将我的internet流量重定向到您?希望不是。:-)即使我不需要特权,system()函数中仍然存在一些安全问题。