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 2157: 旅游
2157: 旅游 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1347 Solved: 619[Submit][Status][Discuss] ...
- 【BZOJ4006】管道连接(动态规划,斯坦纳树)
题面 BZOJ 洛谷 题解 和这题区别不是很大吧. 基本上拿过来改一下就做完了. #include<iostream> #include<cstdio> #include< ...
- 【ARC075F】Mirror
Description 给定正整数\(D\),求有多少个正整数\(N\),满足\(rev(N)=N+D\). 其中\(rev(N)\)表示将\(N\)的十进制表示翻转来读得到的数(翻转后忽略前 ...
- Vuejs+axios+SpringMVC 1
原文:https://blog.csdn.net/annghi/article/details/78925668 Vuejs+axios+SpringMVC4.3.7 (一) 2017年12月28日 ...
- Chapter 6(树)
1.树的储存方式 //****************双亲表示法************************ #define Max_TREE_SIZE 100 typedef int TElem ...
- 003 Python与类C语言的区别(未完)
#写在前面的话:重点记录Python的特点 Python特点: 1. 无分号断句 2. 不用担心溢出问题 3. if-else的用法不同 #if或else后面都要添加冒号: import random ...
- 【Asp.net入门3-03】jQuery-选择元素
练习:使用jQuery实现一个可以给table增加.删除行的页面 HTML代码: <body> <input type="button" value=" ...
- 清空git默认的用户名和密码,
https://www.jianshu.com/p/a1908f55bef8 在家目录下找到.gitconfig git config --local --unset credential.helpe ...
- Git5:Git操作远程仓库
目录 说明 一.git clone 二.git remote 三.git fetch 四.git pull 五.git push 说明 Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个 ...
- 知识笔记:jQuery 事件对象属性小结
使用事件自然少不了事件对象.因为不同浏览器之间事件对象的获取,以及事件对象的属性都有差异,导致我们很难跨浏览器使用事件对象.jQuery中统一了事件对象,当绑定事件处理函数时,会将jQuery格式化后 ...