嵌入式系统中通过pppd拨号后的dns设置
系统环境说明:linux 2.6.30,pppd 2.4.5 ,syslog使用3g拨号方式进行网络连接,操作参考 http://www.west263.com/www/info/18303-1.htm ,pppd拨号成功后通过读取/var/log/messages内容,在/var/log/messages查找“DNS address”关键字,将关键字后面的dns地址输入到/etc/resol
·
系统环境说明:linux 2.6.30,pppd 2.4.5 ,syslog
使用3g拨号方式进行网络连接,操作参考 http://www.west263.com/www/info/18303-1.htm ,pppd拨号成功后通过读取/var/log/messages内容,在/var/log/messages查找“DNS address”关键字,将关键字后面的dns地址输入到/etc/resolv.conf文件中,文件格式
domain your.isp.domain.name
nameserver 192.168.1.1
nameserver x.x.x.x
,然后使用ping尝试通过域名连接服务器。
#include<stdio.h>
#include<stdlib.h>#include<string.h>
FILE *ppp_fb=NULL;
char Dns1[128];
char Dns2[128];
int get_dns(char *dns,char *key,char *msg)
{
int pos=0;
int len=0;
int key_len=0;
int i;
len=strlen(msg);
key_len=strlen(key);
if(len<=0||key_len<=0)
return -1;
for(i=key_len;i<len;i++)
{
if(msg[i]=='.'||(msg[i]>='0'&&msg[i]<='9'))
{
dns[pos]=msg[i];
pos++;
}
}
}
int check_ppp_log(FILE *fb)
{
char log_message[1024];
int log_message_pos=0;
char check_key[]="DNS address";
char *p=NULL;
char ch;
while(feof(fb)==0)
{
ch=fgetc(fb);
if(ch=='\n')
{
if(log_message_pos<=0)
continue;
else
{
p=strstr(log_message,check_key);
if(p!=NULL)
{
p=NULL;
p=strstr(log_message,"primary");
if(p==NULL)
{
p=strstr(log_message,"secondary");
if(p!=NULL)
{
printf("p len = %d\n",strlen(p));
printf("dns2=%s\n",p);
get_dns(Dns2,"secondary DNS address",p);
}
}
else
{
printf("p len = %d\n",strlen(p));
printf("dns1=%s\n",p);
get_dns(Dns1,"secondary DNS address",p);
}
}
}
log_message_pos=0;
memset(log_message,0,1024);
}
else
{
log_message[log_message_pos]=ch;
log_message_pos=log_message_pos+1;
}
}
return 0;
}
int main(void)
{
int ret=-1;
char file_app[]="/var/log/messages";
char buf[64];
char cmd[512];
memset(buf,0,64);
printf("start\n");
if((ppp_fb = fopen(file_app, "r")) == NULL)
{
printf("can't open %s\n",file_app);
exit(2);
}
printf("check_ppp_log\n");
ret=check_ppp_log(ppp_fb);
if(ret==-1)
{
printf("Not found DNS address\n");
fclose(ppp_fb);
exit(2);
}
printf("fclose ok\n");
fclose(ppp_fb);
printf("DNS1 = %s; DNS2 = %s\n",Dns1,Dns2);
system("cp /etc/resolv.conf /etc/resolv.conf_back");
sleep(1);
memset(cmd,0,512);
sprintf(cmd,"echo \"nameserver %s\" >> /etc/resolv.conf",Dns1);
system(cmd);
memset(cmd,0,512);
sprintf(cmd,"echo \"nameserver %s\" >> /etc/resolv.conf",Dns2);
system(cmd);
sleep(1);
system("ping www.XXXXX.com");
return 0;
}
更多推荐
已为社区贡献1条内容
所有评论(0)