您的位置:首页 > 运维架构 > Shell

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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: