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. Navicat使用教程:获取MySQL中的高级行数(第2部分)

    Navicat Premium是一个可连接多种数据库的管理工具,它可以让你以单一程序同时连接到MySQL.Oracle及PostgreSQL数据库,让管理不同类型的数据库更加的方便. 在上篇文章中,我 ...

  2. 【BZOJ1499】【NOI2005】瑰丽华尔兹(动态规划)

    [BZOJ1499]瑰丽华尔兹(动态规划) 题面 BZOJ 题解 先写部分分 设\(f[t][i][j]\)表示当前在\(t\)时刻,位置在\(i,j\)时走的最多的步数 这样子每一步要么停要么走 时 ...

  3. IDA error of " positive sp value has been found"

    问:用IDA静态分析,函数结尾出现 endp ; sp-analysis failed 用F5调不出伪代码,不知道是什么原因,请问有什么解决办法没有? 答:endp ; sp-analysis fai ...

  4. Python word_cloud 样例 标签云系列(三)

    转载地址:https://zhuanlan.zhihu.com/p/20436642word_cloud/examples at master · amueller/word_cloud · GitH ...

  5. pandas读csv、数据处理

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  6. Python 字符串前面加u,r,b的含义

    1.字符串前加 u 例:u"我是含有中文字符组成的字符串." 作用: 后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时 ...

  7. linux命令总结之ls命令

    ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,下面我们就来一起看看ls的用法 英文全名:List即列表的意思,当我们学习某种东西的时候要做到知其所 ...

  8. mysql 设置默认编码为 utf8

    vi /etc/mysql/mysql.conf.d/mysqld.cnf [client] default-character-set=utf8 [mysql] default-character- ...

  9. P1075 质因数分解

    P1075 质因数分解 题目描述 已知正整数 n 是两个不同的质数的乘积,试求出两者中较大的那个质数. 输入输出格式 输入格式: 一个正整数 n . 输出格式: 一个正整数 p ,即较大的那个质数. ...

  10. python中的协程并发

    python asyncio 网络模型有很多中,为了实现高并发也有很多方案,多线程,多进程.无论多线程和多进程,IO的调度更多取决于系统,而协程的方式,调度来自用户,用户可以在函数中yield一个状态 ...