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 路由表
目标 网关 子网掩码 标志 跃点 引用 使用 接口
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 获取网关地址的更多相关文章
- Linux 获取 MAC 地址并去除 : 字符
ifconfig -a | grep eth0 | awk -F ' ' '{print $5}' | sed 's/://g'
- linux获取域名地址
dig live-195887137.cn-north-1.elb.amazonaws.com.cn +short
- 【转载】linux获取mac地址
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/soc ...
- 使用adb/Linux获取网关ip
ip route list table
- Linux 获取本机IP、MAC地址用法大全
getifaddrs()和struct ifaddrs的使用,获取本机IP ifaddrs结构体定义如下: struct ifaddrs { struct ifaddrs *ifa_next; /* ...
- Linux下Python获取IP地址
<lnmp一键安装包>中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是 ...
- get_linux_ip_info.sh 获取ip地址
linux 获取ip地址 get_linux_ip_info.sh #!/bin/bash #/告诉使用者,这程序的用户是从ipconfig 命令中获取IP地址 echo "该程序是从命令中 ...
- linux ip地址自动获取,ip地址…
linux ip地址自动获取,ip地址手动设置(图文解释) 2011-04-19 16:19:31| 分类: 服务器(appache/n | 标签: |字号大中小 订阅 linux ip地址自动获取( ...
- Java 获取Linux 的IP地址
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
随机推荐
- MugLife app是一款可以将静态照片变成3D动画的手机应用
MugLife app是一款可以将静态照片变成3D动画的手机应用,如下效果图所示: 大家可以看到,这个静态图具有了类3D的动画特效,是不是很好玩? 这种算法是如何实现的呢? 这里给出一篇论文“Brin ...
- 【UOJ#80】二分图最大权匹配(KM)
题面 UOJ 题解 模板qaq #include<iostream> #include<cstdio> #include<cstdlib> #include< ...
- BZOJ3688 折线统计 【dp + BIT】
题目链接 BZOJ3688 题解 将点排序 设\(f[i][j][0|1]\)表示以第\(i\)点结尾,有\(j\)段,最后一段上升或者下降的方案数 以上升为例 \[f[i][j][0] = \sum ...
- BZOJ3112 [Zjoi2013]防守战线 【单纯形】
题目链接 BZOJ3112 题解 同志愿者招募 费用流神题 单纯形裸题 \(BZOJ\)可过 洛谷被卡.. #include<algorithm> #include<iostream ...
- bzoj2757【scoi2012】Blinker的仰慕者
题目描述 Blinker 有非常多的仰慕者,他给每个仰慕者一个正整数编号.而且这些编号还隐藏着特殊的意义,即编号的各位数字之积表示这名仰慕者对Blinker的重要度. 现在Blinker想知道编号介于 ...
- 【左偏树】【P3261】 [JLOI2015]城池攻占
Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其 ...
- 栈(C语言实现)
栈是一种线性数据结构,顺序可能是 LIFO(后进先出)或 FILO(先进先出). 堆栈主要有三个基本操作: 1.push,把元素压入栈 2.pop,从栈中弹出元素(同时从栈中移除),最后加入的第一个被 ...
- python【数据类型:字典】
字典的定义 infos = {'name':'张晓红','sex':'女','address':'上海','age':18} stus = {"name":"张三&quo ...
- 利用RAP搭建可视化接口管理平台
环境:CentOS7 jdk:1.7.0_51 redis:3.2.8 mysql:5.6 tomcat:8.0 安装过程: 依赖组件安装: 安装jdk.redis.mysql.tomcat过程省略. ...
- P1077 摆花
P1077 摆花 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共 m 盆.通过调查顾客的喜好,小明列出了顾客最喜欢的 n 种花,从 1 到 n 标号.为了在门口展出更多种花, ...