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 路由表
目标            网关            子网掩码        标志  跃点   引用  使用 接口
               eth1
               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                                00FFFFFF      
eth1          FE7BA8C0                                           

二、实现代码:

#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)
{
    ];
     ;
    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)
    {
        ;
    }

    nl =  ;
    memset(buff, ,sizeof(buff));
    while( fgets(buff, sizeof(buff), fp) != NULL )
    {
        if(nl)
        {
            ;
            while(buff[ifl]!=' ' && buff[ifl]!='\t' && buff[ifl]!='\0')
                ifl++;
            buff[ifl]=;    /* interface */
            , "%lx%lx%X%d%d%d%lx",
                   &d, &g, &flgs, &)
            {
                fclose(fp);
                ;
            }

            ifl = ;        /* parse flags */
            //if(flgs&RTF_UP)
            //{
                gw.s_addr   = g;

                )
                {
                    strcpy(gateway_addr, inet_ntoa(gw));
                    fclose(fp);
                    ;
                }
            //}
        }
        nl++;
    }    

    if(fp)
    {
        fclose(fp);
        fp = NULL;
    }

    ;
}

int main()
{

    ] = {};
     get_gateway_addr(gateway_addr);
    printf("gateway_addr:%s\n", gateway_addr);
    ;
}

三、运行结果:

gateway_addr:192.168.123.254

Linux 获取网关地址的更多相关文章

  1. Linux 获取 MAC 地址并去除 : 字符

    ifconfig -a | grep eth0 | awk -F ' ' '{print $5}' | sed 's/://g'

  2. linux获取域名地址

    dig live-195887137.cn-north-1.elb.amazonaws.com.cn +short

  3. 【转载】linux获取mac地址

    #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/soc ...

  4. 使用adb/Linux获取网关ip

    ip route list table

  5. Linux 获取本机IP、MAC地址用法大全

    getifaddrs()和struct ifaddrs的使用,获取本机IP ifaddrs结构体定义如下: struct ifaddrs { struct ifaddrs *ifa_next; /* ...

  6. Linux下Python获取IP地址

    <lnmp一键安装包>中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是 ...

  7. get_linux_ip_info.sh 获取ip地址

    linux 获取ip地址 get_linux_ip_info.sh #!/bin/bash #/告诉使用者,这程序的用户是从ipconfig 命令中获取IP地址 echo "该程序是从命令中 ...

  8. linux&nbsp;ip地址自动获取,ip地址…

    linux ip地址自动获取,ip地址手动设置(图文解释) 2011-04-19 16:19:31| 分类: 服务器(appache/n | 标签: |字号大中小 订阅 linux ip地址自动获取( ...

  9. Java 获取Linux 的IP地址

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

随机推荐

  1. BZOJ 1007 水平可见直线 | 计算几何

    BZOJ 1007 水平可见直线 题面 平面直角坐标系上有一些直线,请求出在纵坐标无限大处能看到哪些直线. 题解 将所有直线按照斜率排序(平行的直线只保留最高的直线),维护一个栈,当当前直线与栈顶直线 ...

  2. KEIL工程中头文件包含的一些小记

    @2018-4-25 11:02:27 > 源文件包含自身头文件无须指出其路径

  3. 【IOI 2018】Doll 机械娃娃

    我感觉这个题作为Day2T1,有一定的挑战性.为$Rxd$没有完成这道题可惜. 我觉得这道题,如果按照前几个部分分的思路来想,就有可能绕进错误的思路中.因为比如说每个传感器最多只在序列中出现$2$次, ...

  4. BZOJ2525 [Poi2011]Dynamite 【二分 + 贪心】

    题目链接 BZOJ2525 题解 就是要求所有有炸弹的点到点燃点距离最大值最小 显然二分答案距离\(D\) 然后按深度排序,贪心点燃当前没覆盖的深度最深的点往上第\(D\)层的点 每覆盖一个点要标记其 ...

  5. 单点登录(十七)----cas4.2.x登录mongodb验证方式成功后返回更多信息更多属性到客户端

    我们在之前已经完成了cas4.2.x登录使用mongodb验证方式登录成功了.也解决了登录名中使用中文乱码的问题. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方 ...

  6. adb server version (32) doesn't match this client (36); killing...

    http://blog.csdn.net/seaker_/article/details/55107598 FAQ: adb server version (36) doesn't match thi ...

  7. easyUI datetimebox 自定义显示格式

    http://blog.csdn.net/littlewolf766/article/details/7329123 项目里需要显示自定义显示格式,比如只显示yyy,yyyy-mm,yyyy-mm-d ...

  8. in的对象选择(子查询还是List集合),in 的优化,in与exists

    近日查看SQL慢查询日志,发现对于in的查询总是出现超时问题.超时相关SQL语句:select * from flow_ru_bizvar where businessId IN () and sta ...

  9. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  10. git 第一次关联远程仓库

    1.首先需要先git pull origin master 2.然后合并两个无关的仓库 git pull origin master --allow-unrelated-histories