在linux下 获取,修改网关GateWay的两个函数
//获去GateWay
QString GetGateWay()
{
FILE *fp;
char buf[512];
char cmd[128];
char gateway[30];
char *tmp;
strcpy(cmd, "ip route");
fp = popen(cmd, "r");
if(NULL == fp)
{
perror("popen error");
return "";
}
while(fgets(buf, sizeof(buf), fp) != NULL)
{
tmp =buf;
while(*tmp && isspace(*tmp))
++ tmp;
if(strncmp(tmp, "default", strlen("default")) == 0)
break;
}
sscanf(buf, "%*s%*s%s", gateway);
printf("default gateway:%s\n", gateway);
pclose(fp);
return QString(gateway);
}
//设置网关
int SetGateWay(const char *szGateWay)
{
int ret = 0;
char cmd[128];
QString DefGW = GetGateWay();
const char *strGW = DefGW.latin1();
strcpy(cmd, "route del default gw ");
strcat(cmd, strGW);
ret = system(cmd);
if(ret < 0)
{
perror("route error");
return -1;
}
strcpy(cmd, "route add default gw ");
strcat(cmd, szGateWay);
ret = system(cmd);
if(ret < 0)
{
perror("route error");
return -1;
}
return ret;
}
相关文档:
1. HCI层协议概述:
HCI提供一套统一的方法来访问Bluetooth底层。如图所示:
从图上可以看出,Host Controller Interface(HCI) 就是用来沟通Host和Module。Host通常就是PC, Module则是以各种物理连接形式(USB,serial,pc-card等)连接到PC上的bluetooth Dongle。
在Host这一端:application,SDP,L2cap等协议 ......
/*
* linux/kernel/floppy.c
*
* (C) 1991 Linus Torvalds
*/
/*
* 02.12.91 - Changed to static variables to indicate need for reset
* and recalibrate. This makes some things easier (output_byte reset
* checking etc), and means less i ......
解析Linux网络分析的三大利器(ZT)
ZT from linuxsir
解析Linux网络分析的三大利器
随着Internet的迅猛发展,网络已无处不在,但是,它可能随时受到来自各方的攻击。了解哪些人正在访问资源、哪些人正在享受服务、哪些人正在发送大
量垃圾等,对网络管理员来说是非常必要的。利用Linux中较常见的网络分析工具Tcpdump、N ......
管道:当从一个进程连接数据流到另一个进程时,使用术语管道(pipe)。
#i nclude <unistd.h>
int pipe(int filedes[2]); //创建管道
pipe()说明:
返回值:0成功,-1出错。
如果调用成功,则进程此时由了两个额外的打开文件描述符,filedes[0]中的值是管道的读取端,而filedes[1]是管道的写入端。
#include&l ......