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 ...
随机推荐
- BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题
BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题 将字符串复制一遍接在原串后面,然后后缀排序即可. #include <cmath> #include &l ...
- 【转】vi编辑只读文档无法保存的解决办法
vi编辑只读文档无法保存的解决办法 使用普通用户编辑nginx.conf 等配置文件: 保存的时 候会提示:没有Root Permission 可以用如下方法解决:保存时加上::w !sudo tee ...
- 【ZOJ3899】State Reversing 解题报告
[ZOJ3899]State Reversing Description 有\(N\)个不同的怪兽,编号从\(1\) 到\(N\).Yukari有\(M\)个相同的房间,编号为\(1\)到\(M\). ...
- C - Ilya And The Tree Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...
- C++模版详解(-)
C++模版: 模版时C++支持多参数多态的工具,使用模版可以为用户为类或函数声明一般模式,使得类的数据成员,或者成员函数的参数,返回值取得任意类型. 模版是一种对类型进行参数化的工具: 通 ...
- R语言画图
转http://www.cnblogs.com/jiangmiaomiao/p/6991632.html 0 引言 R支持4种图形类型: base graphics, grid graphics, l ...
- 题解【51nod 1290 Counting Diff Pairs】
Description 求区间内有多少对 \((i,j)\) 满足 \(|a_i - a_j| \leq k\) Solution 可以莫队做(万能的莫队) 只需要考虑加入一个数会产生多少贡献即可 离 ...
- bzoj 1193 贪心+bfs
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2015 Solved: 914[Submit][Statu ...
- python 文件路径问题
需要用到 os 模块和 sys 模块 import os print(__file__)# 输出相对路径 print(os.path.abspath(__file__)) #输出绝对路径 # D:\P ...
- Docker registry私有仓库(七)
Docker registry私有仓库搭建基本几步流程(采用nginx+认证的方式) 1. 申请免费的ssl证书 https://buy.wosiqn.com/free 2. 设置nginx ssl证 ...