手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下Shell脚本获取网络信息

[日期:2012-08-03] 来源:Linux社区  作者:txgc_wm [字体:大 中 小]

在linux的网络编程中,经常需要用到网络信息如:ip、gateway、dns等。以下脚本实现了获取网络信息的功能,包括ip、broadcast、netmask、gateway以及dns的信息。

  1. #! /bin/sh

  2. default_route=$(ip route show)
  3. default_interface=$(echo $default_route | sed -e 's/^.*dev \([^ ]*\).*$/\1/' | head -n 1)
  4. address=$(ip addr show label $default_interface scope global | awk '$1 == "inet" { print $2,$4}')
  5.   
  6. #ip address
  7. ip=$(echo $address | awk '{print $1 }')
  8. ip=${ip%%/*}
  9.   
  10. #broadcast
  11. broadcast=$(echo $address | awk '{print $2 }')
  12.   
  13. #mask address
  14. mask=$(route -n |grep 'U[ \t]' | head -n 1 | awk '{print $3}')
  15.   
  16. #gateway address
  17. gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}')
  18.   
  19. #dns
  20. dns=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
  21.   
  22. echo ip:$ip,mask:$mask,broadcast:$broadcast,gateway:$gateway,dns:$dns
linux
相关资讯       Linux Shell 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款