Linux下程式設計設定IP,閘道器

2021-06-01 03:05:38 字數 2769 閱讀 5774

1.拼湊成指令實現:

#include #include int main(void)

; char cmd_gw[64] = ;

sprintf(cmd_ip_mask, "ifconfig eth0 %s netmask %s", ip, mask);

sprintf(cmd_gw, "route add default gw %s netmask %s", gateway);

system(cmd_ip_mask);

system(cmd_gw);

return 0;

}

#include #include #include #define file_name "ip.txt"

int main(void)

; char cmd_gw[64] = ;

if( (fp=fopen(file_name,"r"))==null )

fgets(ip, 20, fp);

fgets(mask, 20, fp);

fgets(gateway, 20, fp);

printf("ip : %s ", ip);

printf("mask : %s ", mask);

printf("gateway : %s \n", gateway);

sprintf(cmd_ip_set, "ifconfig eth0 %s", ip);

system(cmd_ip_set);

sprintf(cmd_ip_set, "ifconfig eth0 netmask %s", mask);

system(cmd_ip_set);

sprintf(cmd_ip_set, "route add default gw %s", gateway);

system(cmd_ip_set);

return 0;

}

#include #include #include #include #include #include #include #include #include #include #define false 0

#define true 1

#define file_name "ip.txt"

#if 1

int is_valid_ip_address(const char *addr)

; while ( *(addr + i) )

} ++i;

} i = j = value = count = 0;

memset(buf, 0, sizeof(buf));

while ( *(addr + i) )

}if(*(addr + i) == 0x0a) // linux.

}

/*if(num == 4)

*///printf(" ******** %d %s %d\n", i, (addr + i),strlen(addr + i));

printf("@@@@@@@@@ :%s \n",(addr + i));

if (( ! isdigit(*(addr + i)) ) && (num != 4))

*(buf + j) = *(addr + i);

++i;

++j;

}*(buf + j) = '\0';

//if(num == 4)

///if ( *(addr + i) == '.' )

else

}printf("buf = %s %d \n",buf, strlen(buf));

//m = strlen(buf)-2;

m = strlen(buf);

for(k=0;k>>>>>>>> \n",str);

#if 1

struct in_addr addr;

int ret;

volatile int local_errno;

errno = 0;

ret = inet_pton(af_inet, str, &addr);

local_errno = errno;

if (ret > 0)

fprintf(stderr, "\"%s\" is a valid ipv4 address\n", str);

else if (ret < 0)

fprintf(stderr, "eafnosupport: %s\n", strerror(local_errno));

else

fprintf(stderr, "\"%s\" is not a valid ipv4 address\n", str);

return ret;

#endif

}#endif

int main(void)

; char cmd_mask[64] = ;

char cmd_gw[64] = ;

if( (fp=fopen(file_name,"r"))==null )

fgets(ip, 20, fp);

fgets(mask, 20, fp);

fgets(gateway, 20, fp);

//printf("check : %d \n",is_valid_ip_address(ip));

if(is_valid_ip_address(ip))

else

return 0;

}

linux設定閘道器和ip

linux設定閘道器和ip 修改ip位址 即時生效 ifconfig eth0 192.168.0.20 netmask 255.255.255.0 啟動生效 修改 etc sysconfig network scripts ifcfg eth0 修改default gateway 即時生效 rou...

linux設定閘道器修改ip相關

在網路上搜的 編輯檔案 etc sysconfig network scripts ifcfg eth0 device eth0 裝置名稱,不要修改 bootproto static 不要修改 broadcast 172.16.140.255 廣播位址,一般為本網段的最後乙個ip ipaddr 17...

linux中IP,閘道器的設定

在linux作業系統下修改ip dns和路由配置的命令如下 ifconfig eth0 新的 ip 一 修改ip位址 aeolus db1 network scripts vi ifcfg eth0 device eth0 onboot yes bootproto static ipaddr 219...