Shell Script For Collecting Information on the Linux Network Configuration

by on June 2, 2009 · 13 comments

This shell script is tested under CentOS / RHEL and Fedora Linux. It should also work under other Linux distributions. If you would like to collect and submit information on your network configuration to your sr. Linux / UNIX admin use this script.

  1. #!/bin/bash
  2. # A Linux Shell script to collect information on your network configuration.
  3. # -------------------------------------------------------------------------
  4. # Copyright (c) 2003 nixCraft project <http://cyberciti.biz/fb/>
  5. # This script is licensed under GNU GPL version 2.0 or above
  6. # -------------------------------------------------------------------------
  7. # This script is part of nixCraft shell script collection (NSSC)
  8. # Visit http://bash.cyberciti.biz/ for more information.
  9. # -------------------------------------------------------------------------
  10. # Last updated: Jun-03-2009
  11. # -------------------------------------------------------------------------
  12. HWINF=/usr/sbin/hwinfo
  13. IFCFG=/sbin/ifconfig
  14. IP4FW=/sbin/iptables
  15. IP6FW=/sbin/ip6tables
  16. LSPCI=/sbin/lspci
  17. ROUTE=/sbin/route
  18. NETSTAT=/bin/netstat
  19. LSB=/usr/bin/lsb_release
  20.  
  21. ## files ##
  22. DNSCLIENT="/etc/resolv.conf"
  23. DRVCONF="/etc/modprobe.conf"
  24. NETALIASCFC="/etc/sysconfig/network-scripts/ifcfg-eth?-range?"
  25. NETCFC="/etc/sysconfig/network-scripts/ifcfg-eth?"
  26. NETSTATICROUTECFC="/etc/sysconfig/network-scripts/route-eth?"
  27. SYSCTL="/etc/sysctl.conf"
  28.  
  29. ## Output file ##
  30. OUTPUT="network.$(date +'%d-%m-%y').info.txt"
  31.  
  32. ## Email info to?? ##
  33. SUPPORT_ID="you@example.com"
  34.  
  35. chk_root(){
  36. local meid=$(id -u)
  37. if [ $meid -ne 0 ];
  38. then
  39. echo "You must be root user to run this tool"
  40. exit 999
  41. fi
  42. }
  43.  
  44. write_header(){
  45. echo "---------------------------------------------------" >> $OUTPUT
  46. echo "$@" >> $OUTPUT
  47. echo "---------------------------------------------------" >> $OUTPUT
  48. }
  49.  
  50.  
  51. dump_info(){
  52. echo "* Hostname: $(hostname)" >$OUTPUT
  53. echo "* Run date and time: $(date)" >>$OUTPUT
  54.  
  55. write_header "Linux Distro"
  56. echo "Linux kernel: $(uname -mrs)" >>$OUTPUT
  57. $LSB -a >> $OUTPUT
  58.  
  59. [ -x ${HWINF} ] && write_header "${HWINF} --network_ctrl"
  60. [ -x ${HWINF} ] && ${HWINF} --network_ctrl >> $OUTPUT
  61.  
  62. [ -x ${HWINF} ] && write_header "${HWINF} --isapnp"
  63. [ -x ${HWINF} ] && ${HWINF} --isapnp >> $OUTPUT
  64.  
  65. write_header "PCI Devices"
  66. ${LSPCI} -v >> $OUTPUT
  67.  
  68. write_header "$IFCFG Output"
  69. $IFCFG >> $OUTPUT
  70.  
  71. write_header "Kernel Routing Table"
  72. $ROUTE -n >> $OUTPUT
  73.  
  74. write_header "Network Card Drivers Configuration $DRVCONF"
  75. [ -f $DRVCONF ] && grep eth $DRVCONF >> $OUTPUT || echo "Error $DRVCONF file not found." >> $OUTPUT
  76.  
  77. write_header "DNS Client $DNSCLIENT Configuration"
  78. [ -f $DNSCLIENT ] && cat $DNSCLIENT >> $OUTPUT || echo "Error $DNSCLIENT file not found." >> $OUTPUT
  79.  
  80. write_header "Network Configuration File"
  81. for f in $NETCFC
  82. do
  83. if [ -f $f ]
  84. then
  85. echo "** $f **" >> $OUTPUT
  86. cat $f >> $OUTPUT
  87. else
  88. echo "Error $f not found." >> $OUTPUT
  89. fi
  90. done
  91.  
  92. write_header "Network Aliase File"
  93. for f in $NETALIASCFC
  94. do
  95. if [ -f $f ]
  96. then
  97. echo "** $f **" >> $OUTPUT
  98. cat $f >> $OUTPUT
  99. else
  100. echo "Error $f not found." >> $OUTPUT
  101. fi
  102. done
  103.  
  104.  
  105. write_header "Network Static Routing Configuration"
  106. for f in $NETSTATICROUTECFC
  107. do
  108. if [ -f $f ]
  109. then
  110. echo "** $f **" >> $OUTPUT
  111. cat $f >> $OUTPUT
  112. else
  113. echo "Error $f not found." >> $OUTPUT
  114. fi
  115. done
  116.  
  117. write_header "IP4 Firewall Configuration"
  118. $IP4FW -L -n >> $OUTPUT
  119.  
  120. write_header "IP6 Firewall Configuration"
  121. $IP6FW -L -n >> $OUTPUT
  122.  
  123. write_header "Network Stats"
  124. $NETSTAT -s >> $OUTPUT
  125.  
  126. write_header "Network Tweaks via $SYSCTL"
  127. [ -f $SYSCTL ] && cat $SYSCTL >> $OUTPUT || echo "Error $SYSCTL not found." >>$OUTPUT
  128.  
  129. echo "The Network Configuration Info Written To $OUTPUT. Please email this file to $SUPPORT_ID."
  130. }
  131.  
  132. chk_root
  133. dump_info


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Susinthiran

    Hi, Vivek.
    Just wanted to say i like reading your articles and other info on this site.
    You and Dag Weeirs are the super gurus of RHEL/CentOS that i know about :)))

    B.R
    Susinthiran
    Oslo/Norway

  • Vivek Gite

    Thanks for kind words :D

  • innqubus

    Awesome script dude… Thanks a ton. This one’s so useful.

  • adhitya christiawan nurprasetyo

    agree! this website shared a lot of great infos :)

  • Kevin Hatfield

    Most excellent site! More FreeBSD stuff please :))

  • abhi

    what a script man…..lovely….hats off to the writer….

  • Jeff

    This a great script!!!

    I but one more line, to send email

    cat “$OUTPUT” | mail $SUPPORT_ID -s “The Network Configuration Info $(hostname)”

  • boy

    good day,vivek.

    I am currently developing a project that is similar to ftp by using shell script.In my project,I try to implement one replication technique called Read-One-Write-All (ROWA).Do you have any idea how to develop the sourcecode or if possible,any similar project that I could refer to.
    Thank you.

  • Georg

    Hi.

    This is a very good script. I modified it, because i needed an output of “dmidecode”. I thought it’s useful…

    Georg

  • dustin

    I recieved this when running the script,,,,,The Network Configuration Info Written To network.08-12-09.info.txt. Please email this file to

    im using Centos 5.3

  • explorer

    i love this scrips

  • andrei

    same like dustin..

  • A.C.

    Thanks for the great scripts… I’m currently trying to up my skills on shell programming and these are extremely useful and very cleanly written. Please keep them coming!

Previous Script:

Next Script: