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 ...
随机推荐
- IDEA之插件篇
强烈推荐小伙伴们,安装以下插件:
- 解题:CF1009 Dominant Indices
题面 长链剖分模板题 只能按深度统计,同时比DSU on tree难理解一些,但是复杂度少个log 对每个点抓出向下延伸最长的儿子叫做长儿子.在合并时用指针继承信息,对于长儿子O(1)继承,其他儿子暴 ...
- PyQt5整体介绍
1 PyQt5整体介绍 PyQt5是基于图形程序框架Qt5的Python语言实现,由一组Python模块构成. PyQt5的官方网站是:www.riverbankcomputing.co.uk. Py ...
- 关于ASP.NET MVC的Html.BeginForm()方法
http://zhidao.baidu.com/link?url=9j53URZJv2B9W-TPtQAaKCRbqIcYy2r3WNO0NDzciTON0EYj5Hhd3rl3UlIllK1CqOC ...
- ECharts.js 简单示例
ECharts.js学习(一) 简单入门 EChart.js 简单入门 最近有一个统计的项目要做,在前端的数据需要用图表的形式展示.网上搜索了一下,发现有几种统计图库. MSChart 这个是Vi ...
- POI上传,导入excel文件到服务器1
首先说一下所使用的POI版本3.8,需要用的的Jar包: dom4j-1.6.1.jarpoi-3.8-20120326.jarpoi-ooxml-3.8-20120326.jarpoi-ooxml- ...
- 利用RAP搭建可视化接口管理平台
环境:CentOS7 jdk:1.7.0_51 redis:3.2.8 mysql:5.6 tomcat:8.0 安装过程: 依赖组件安装: 安装jdk.redis.mysql.tomcat过程省略. ...
- 装饰器 and 闭包函数 未完。。。。。
装饰器是一个返回函数的高阶函数.装饰器=高阶函数+函数嵌套+闭包 装饰器需要遵循的原则:不修改被装饰函数的源代码,不修改被装饰函数的调用方式. 高阶函数 1.函数接收的参数是一个函数名 2.函数的返回 ...
- 前端如何使用easy-mock模拟接口
1. 如何使用easy-mock // 获取 easy-mock 的模拟数据 getData () { // 开发环境使用 easy-mock 数据,正式环境使用 json 文件 if (proces ...
- ubuntu 使用小技巧
1. 查看网速 ethstatus ubuntu下用ethstatus可以监控实时的网卡带宽占用.这个软件能显示当前网卡的 RX 和 TX 速率,单位是Byte 安装 ethstatus 软件 sud ...