v-list-sys-interfaces 1.8 KB

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