当前位置: 技术问答>linux和unix
请问在linux中,怎样用c语言编程来实现对计算机的IP地址、子网掩码的修改啊?
来源: 互联网 发布时间:2015-03-20
本文导语: 老板真变态,非要我在linux下编程,痛苦中... | 给你一个修改IP地址的代码 #include #include #include #include #include #include #include #include #include int ifconfig(const char *ifname, const char *ipaddr) { struct...
老板真变态,非要我在linux下编程,痛苦中...
|
给你一个修改IP地址的代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
int ifconfig(const char *ifname, const char *ipaddr)
{
struct sockaddr_in sin;
struct ifreq ifr;
int fd;
int ret;
char *ptr;
short found_colon = 0;
bzero(&ifr, sizeof(struct ifreq));
if (ifname == NULL)
return (-1);
if (ipaddr == NULL)
return (-1);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
{
perror("Not create network socket connectionn");
return (-1);
}
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(ipaddr);
memcpy(&ifr.ifr_addr, &sin, sizeof(sin));
if (ioctl(fd, SIOCSIFADDR, &ifr)
#include
#include
#include
#include
#include
#include
#include
#include
#include
int ifconfig(const char *ifname, const char *ipaddr)
{
struct sockaddr_in sin;
struct ifreq ifr;
int fd;
int ret;
char *ptr;
short found_colon = 0;
bzero(&ifr, sizeof(struct ifreq));
if (ifname == NULL)
return (-1);
if (ipaddr == NULL)
return (-1);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
{
perror("Not create network socket connectionn");
return (-1);
}
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(ipaddr);
memcpy(&ifr.ifr_addr, &sin, sizeof(sin));
if (ioctl(fd, SIOCSIFADDR, &ifr)