linux shell 获取默认网关地址
2014-06-07 11:34
239 查看
route | grep 'default' | awk '{print $2}'
编程实现:
1730 /*
1731 * 获取默认网关
1732 */
1733 int get_gateway(char *gate)
1734 {
1735 FILE *fp = NULL;
1736 char temp[20] = {0};
1737 int i = 0;
1738
1739 fp = popen(GET_GATEWAY_CMD, "r");
1740 if (fp == NULL) {
1741 dbg_out(DBG_ERR, "popen failed\n");
1742 return -1;
1743 } else {
1744 if (fread(temp, sizeof(char), sizeof(temp), fp) == -1) {
1745 dbg_out(DBG_ERR, "fread %s failed\n", GET_GATEWAY_CMD);
1746 pclose(fp);
1747 }
1748 pclose(fp);
1749
1750 while (temp[i] != '\n') {
1751 i++;
1752 }
1753 temp[i] = '\0';
1754
1755 memcpy(gate, temp, strlen(temp));
1756 }
1757 return 0;
1758 }
编程实现:
1730 /*
1731 * 获取默认网关
1732 */
1733 int get_gateway(char *gate)
1734 {
1735 FILE *fp = NULL;
1736 char temp[20] = {0};
1737 int i = 0;
1738
1739 fp = popen(GET_GATEWAY_CMD, "r");
1740 if (fp == NULL) {
1741 dbg_out(DBG_ERR, "popen failed\n");
1742 return -1;
1743 } else {
1744 if (fread(temp, sizeof(char), sizeof(temp), fp) == -1) {
1745 dbg_out(DBG_ERR, "fread %s failed\n", GET_GATEWAY_CMD);
1746 pclose(fp);
1747 }
1748 pclose(fp);
1749
1750 while (temp[i] != '\n') {
1751 i++;
1752 }
1753 temp[i] = '\0';
1754
1755 memcpy(gate, temp, strlen(temp));
1756 }
1757 return 0;
1758 }
相关文章推荐
- shell 获取网卡IP地址 子网掩码 默认网关 广播地址 MAC地址
- C#编程获取IP地址、子网掩码、网关地址
- C#编程获取IP地址、子网掩码、网关地址
- 默认网关 网关 子网掩码 广播地址
- Android获取WIFI网关地址与子网掩码地址
- 设置ipv6全球地址和默认网关
- 获取网关的INTERNET地址
- 本地连接的ip地址 子网掩码 默认网关 还有dns服务器地址怎么设置? (转自网易博客)
- cisco配置IP 网关 辅助地址 静态 默认 浮动路由
- 获取本机的所有IP,子网掩码(subnet mask)和默认网关
- 4.2.4 默认网关地址
- C#中获取本机IP地址,子网掩码,网关地址
- Linux下网关地址的获取
- 获取本机的所有IP,子网掩码(subnet mask)和默认网关
- linux C 获取ip mac 广播地址,以及网关
- C#编程获取IP地址、子网掩码、网关地址
- linux shell 将当前文件地址作为默认路径写入环境变量
- 嵌入式 获取指定网卡在路由表中的网关以及源地址和目的地址
- linux编程获取网关地址
- linux 下获取默认网关的方法