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

 

转载于:https://www.cnblogs.com/youthshouting/p/4500957.html

获取Linux操作系统的IP地址网关可以通过使用命令行工具来实现,以下是获取IP地址网关的命令: 1. 获取IP地址: ``` ifconfig ``` 该命令可以列出网络接口信息,其中包括IP地址以及其他网络相关的信息。 2. 获取网关: ``` route -n ``` 该命令可以列出当前系统的路由表信息,其中包括网关的IP地址以及其他网络相关的信息。 如果你是使用Java程序来获取IP地址网关的话,可以通过使用NetworkInterface和InetAddress类来实现,以下是示例代码: 获取IP地址: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class GetIPAddress { public static void main(String[] args) throws Exception { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); if (iface.isLoopback() || !iface.isUp()) { continue; } Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr.isLinkLocalAddress() || addr.isLoopbackAddress() || addr.isMulticastAddress()) { continue; } System.out.println("IP地址:" + addr.getHostAddress()); } } } } ``` 获取网关: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class GetGateway { public static void main(String[] args) throws SocketException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); Enumeration<InetAddress> inetAddresses = iface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (!inetAddress.isLinkLocalAddress() && !inetAddress.isLoopbackAddress() && inetAddress instanceof java.net.Inet4Address) { Enumeration<NetworkInterface> subInterfaces = iface.getSubInterfaces(); while (subInterfaces.hasMoreElements()) { NetworkInterface subInterface = subInterfaces.nextElement(); Enumeration<InetAddress> subInetAddresses = subInterface.getInetAddresses(); while (subInetAddresses.hasMoreElements()) { InetAddress subInetAddress = subInetAddresses.nextElement(); if (!subInetAddress.isLinkLocalAddress() && !subInetAddress.isLoopbackAddress() && subInetAddress instanceof java.net.Inet4Address) { System.out.println("网关地址:" + subInetAddress.getHostAddress()); } } } } } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值