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. 滥用DNSAdmins权限进行Active Directory提权

      0x00 前言 除了在实现自己的DNS服务器功能之外,Microsoft还为该服务器实现自己的管理协议以便于管理与Active Directory域集成.默认情况下,域控制器也是DNS服务器; 大 ...

  2. zookeeper的安装及共享锁的应用

         Zookeeper的安装及共享锁的应用 1.zookeeper的安装 1.1  下载安装包 Wget http://mirror.bit.edu.cn/apache/zookeeper/zo ...

  3. springboot中如何添加第三方的jar包或者说如何配置本地jar

    首先推荐博客: spring-boot-maven-plugin 安装本地jar 包 http://www.cnblogs.com/acm-bingzi/p/mavenSpringBootPlugin ...

  4. python基础3--字符串

    字符串 1.大小写转换 >>> name 'bigberg' >>> name.capitalize() # 首字母大写 'Bigberg' >>> ...

  5. linux命令总结之traceroute命令

    通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...

  6. 重定向(Redirect)相关的几个问题

    转载自:http://hi.baidu.com/fboosjgvuvckore/item/405a1cd7be2c36e3795daa8b 此次挂接用户中心,交互大都通过重定向(Redirect)实现 ...

  7. 2018ICPC赛后总结

    南京: 南京是我们队第一场区域赛,而且和一队二队一起,心里总有种踏实感.之前我们仨在一起讨论过我们打大型比赛出现的问题,晨哥说我们在封榜后总是出不了题,确实是这样,我觉得在之前的比赛中,我们队胜负欲都 ...

  8. bzoj千题计划144:bzoj1176: [Balkan2007]Mokia

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ分治 #include<cstdio> #include<iostre ...

  9. codevs 1540 1540 银河英雄传说

    1540 银河英雄传说 题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银 ...

  10. 部署elasticsearch遇到的问题

    为增加搜索功能,最近在自己的服务器上部署elasticsearch,折腾一下,把注意的问题记录一下. 1.  因为最近的es5.5.2要求java1.8,所以确保java版本正确. 2. 我的服务器只 ...