Linux Get gateway Address

Source: Internet
Author: User

Use of the route command: operation or display of the IP routing table
Route:description
Route manipulates the kernel ' s IP routing tables. Its primary use are to set up static routes to
Specific hosts or networks via an interface after it have been configured with the Ifconfig (8) Pro‐gram.

When the Add or Del Options is used, route modifies the routing tables. Without these options,
Route displays the current contents of the routing tables.
Route-n: (for printing routing tables)
Show numerical addresses instead of trying to determine symbolic host names. This is useful
If you is trying to determine, the route to your nameserver have vanished.

First, look at the routing table under Linux:

(1) route-n with command

[Email protected]:~# Route-n Kernel IP routing table Destination gateway subnet Mask flag Hop reference use interface192.168.123.0   0.0.0.0         255.255.255.0U1      0        0eth10.0.0.0         192.168.123.254 0.0.0.0Ug0      0        0Eth1

(2) Cat/pro/net/route

[Email protected]:/proc/net#CatRoute Iface Destination Gateway flags refcnt use Metric Mask MTU Window Irtt Eth1 007ba8c000000000    0001        0        0        100FFFFFF0    0    0eth100000000Fe7ba8c00003        0        0        0        00000000        0    0    0 

Second, the implementation of code:

#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>intGET_GATEWAY_ADDR (Char*gateway_addr) {    Charbuff[ the]; intNL =0 ; structin_addr GW; intFlgs,ref, use, metric; unsignedLong intd,g,m; unsignedLongaddr; 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) {intIFL =0;  while(buff[ifl]!=' '&& buff[ifl]!='\ t'&& buff[ifl]!=' /') 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;}intMain () {Chargateway_addr[ the] = {0};    GET_GATEWAY_ADDR (GATEWAY_ADDR); printf ("gateway_addr:%s\n", GATEWAY_ADDR); return 0;}

Third, the operation result:

GATEWAY_ADDR:192.168. 123.254

Linux Get gateway Address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.