Linux 获取网关地址

route命令的用法:操作或者显示IP路由表
route:DESCRIPTION
Route manipulates the kernel‘s IP routing tables. Its primary use is to set up static routes to
specific hosts or networks via an interface after it has been configured with the ifconfig(8) pro‐gram.

When the add or del options are used, route modifies the routing tables. Without these options,
route displays the current contents of the routing tables.
route-n:(用于打印路由表)
show numerical addresses instead of trying to determine symbolic host names. This is useful
if you are trying to determine why the route to your nameserver has vanished.

一、在Linux下查看路由表:

(1)用命令route -n

root@Ubunut10:~# route -n
内核 IP 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
192.168.123.0   0.0.0.0         255.255.255.0   U     1      0        0 eth1
0.0.0.0         192.168.123.254 0.0.0.0         UG    0      0        0 eth1

(2)cat /pro/net/route

root@Ubunut10:/proc/net# cat route 
Iface    Destination    Gateway     Flags    RefCnt    Use    Metric    Mask        MTU    Window    IRTT                                                       
eth1      007BA8C0    00000000    0001      0      0      1      00FFFFFF      0    0    0                                                                               
eth1      00000000    FE7BA8C0    0003      0      0      0      00000000      0    0    0 

二、实现代码:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <arpa/inet.h>


int get_gateway_addr(char *gateway_addr)
{
    char buff[256];
    int  nl = 0 ;
    struct in_addr gw;
    int flgs, ref, use, metric;
    unsigned long int d,g,m;
    unsigned long addr;
    
    FILE *fp = NULL;
    
    fp = fopen("/proc/net/route", "r");
    if (fp == NULL)
    {
        return -1;
    }
        
    nl = 0 ;
    memset(buff, 0,sizeof(buff));
    while( fgets(buff, sizeof(buff), fp) != NULL ) 
    {
        if(nl) 
        {
            int ifl = 0;
            while(buff[ifl]!=  && buff[ifl]!=\t && buff[ifl]!=\0)
                ifl++;
            buff[ifl]=0;    /* interface */
            if(sscanf(buff+ifl+1, "%lx%lx%X%d%d%d%lx",
                   &d, &g, &flgs, &ref, &use, &metric, &m)!=7) 
            {
                fclose(fp);
                return -2;
            }

            ifl = 0;        /* parse flags */
            //if(flgs&RTF_UP) 
            //{            
                gw.s_addr   = g;
                    
                if(d==0)
                {
                    strcpy(gateway_addr, inet_ntoa(gw));
                    fclose(fp);
                    return 0;
                }                
            //}
        }
        nl++;
    }    
    
    if(fp)
    {
        fclose(fp);
        fp = NULL;
    }
    
    return    -1;
}

int main()
{
    
    char gateway_addr[15] = {0};
     get_gateway_addr(gateway_addr);
    printf("gateway_addr:%s\n", gateway_addr);
    return 0;
}

三、运行结果:

gateway_addr:192.168.123.254

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。