v-list-sys-interfaces 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. # info: list system interfaces
  3. # options: [FORMAT]
  4. #
  5. # The function for obtaining the list of network interfaces.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. format=${1-shell}
  11. # Includes
  12. source $VESTA/func/main.sh
  13. # JSON list function
  14. json_list() {
  15. objects=$(echo "$interfaces" |wc -l)
  16. i=1
  17. echo '['
  18. for interface in $interfaces; do
  19. echo -n ' "'$interface'"'
  20. if [ "$i" -lt "$objects" ]; then
  21. echo ','
  22. else
  23. echo
  24. fi
  25. ((i++))
  26. done
  27. echo ']'
  28. }
  29. # SHELL list function
  30. shell_list() {
  31. echo "INTERFACE"
  32. echo "---------"
  33. for interface in $interfaces; do
  34. echo "$interface"
  35. done
  36. }
  37. # PLAIN list function
  38. plain_list() {
  39. for interface in $interfaces; do
  40. echo "$interface"
  41. done
  42. }
  43. # CSV list function
  44. csv_list() {
  45. echo "INTERFACE"
  46. for interface in $interfaces; do
  47. echo "$interface"
  48. done
  49. }
  50. #----------------------------------------------------------#
  51. # Action #
  52. #----------------------------------------------------------#
  53. # Defining interface list
  54. interfaces=$(cat /proc/net/dev |grep : |cut -f 1 -d : |tr -d ' ' |grep -v lo)
  55. # Listing data
  56. case $format in
  57. json) json_list ;;
  58. plain) plain_list ;;
  59. csv) csv_list ;;
  60. shell) shell_list;;
  61. esac
  62. #----------------------------------------------------------#
  63. # Vesta #
  64. #----------------------------------------------------------#
  65. exit