在代码中更新系统的gateway,适用于linux操作系统
来源:互联网 发布:知乎邀请机制 编辑:程序博客网 时间:2024/05/22 09:39
今天在网上找了一下希望能找到用系统函数更新gateway的函数没有找到。看了一下busybox中关于route命令的实现,发现也比较麻烦。
最后只好用老办法了,用system来调用route的命令了。下面是代码。
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *pfile=NULL;
ssize_t read;
size_t len=0;
char* line=NULL;
char *c=NULL;
len=256;
char routeinfofile[]="route2.txt";
char shellcmd[128];
char ipaddr[20];
memset(shellcmd,0,sizeof(shellcmd));
sprintf(shellcmd,"route >%s",routeinfofile);
system(shellcmd);
line=(char*)malloc(len);
if(line==NULL) return;
if(NULL==(pfile=fopen(routeinfofile,"r")) )
{
printf("/ncannot open route.txt/n");
return;
}
while((read=getline(&line,&len,pfile))!=-1)
{
printf("Retriveved line of length %zu :/n",read);
printf("%s",line);
c=strstr(line,"default");
if(c)
{
printf("/nfound default:line=(%p) c=(%p)",line,c);
int i;
int posbegin=0;
int posend=0;
for(i=7;i<read;i++)
{
if((line[i]!=' ')&&(posbegin==0))
{
posbegin=i;
}
if(posbegin>0)
{
if((line[i]==' ')&&(posend==0))
{
posend=i;
break;
}
}
}
if(posend>0)
{
printf("/nthe IP address founded is begin(%d) end(%d):",posbegin,posend);
memset(ipaddr,0,sizeof(ipaddr));
for(i=posbegin;i<posend;i++)
{
printf("%c",line[i]);
if((i-posbegin)<sizeof(ipaddr))
ipaddr[i-posbegin]=line[i];
}
printf("/n");
memset(shellcmd,0,sizeof(shellcmd));
sprintf(shellcmd,"route del default gw %s dev eth0",ipaddr);
system(shellcmd);
}
}
else printf("/nnot found default. line=(%p) c=(%p)",line,c);
}
if(line) free(line);
memset(shellcmd,0,sizeof(shellcmd));
sprintf(shellcmd,"route add default gw %s dev eth0","192.168.1.1");
system(shellcmd);
return;
}
- 在代码中更新系统的gateway,适用于linux操作系统
- 在命令行中运行eclipse中创建的java项目,适用于linux 操作系统
- TimePicker在ScrollView中滑动冲突的解决方法(更新适用于全控件的方法)
- TimePicker在ScrollView中滑动冲突的解决方法(更新适用于全控件的方法)
- 统计代码行数,适用于所有的开源操作系统
- 编写适用于模拟器的Linux代码
- 在linux操作系统中如何截获系统调用
- 在Linux操作系统中如何截获系统调用
- 在grub 1.97中加载系统中所有硬盘中的所有操作系统–更新grub.cfg
- 在linux下 获取,修改网关GateWay的两个函数
- 在Linux下获取修改网关GateWay的两个函数
- [土哥原创]在Redhat Linux中使用apt取代up2date来更新你的系统!
- 操作系统中关于信号量操作的代码示例(Linux + windows)
- 操作系统中关于信号量操作的代码示例(Linux + windows)
- linux 操作系统中系统设置配制setup命令的使用
- OpenWrt——适用于路由器的Linux系统
- linux 中 更新系统时间
- Linux 操作系统的时钟系统
- 寻找成为开发高手的密匙
- 使用platformRequest()自动更新MIDlet套件
- VS2008结合VA使用经验总结
- chinaitzhe.com
- Source Insight 经典教程
- 在代码中更新系统的gateway,适用于linux操作系统
- 第二章 SQLite的基本语法
- js数组传入后台非ajax实现
- [学习]_转帖:为什么你应该写博客
- SQL重复记录处理(查找,过滤,删除)
- 用Eclipse+gcc编译器开始你的旅程
- mytie22222
- vim ctags cscope的配合使用阅读源码
- PHP关于VC9和VC6以及Thread Safe和Non Thread Safe版本选择的问题