v-list-sys-interfaces 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. source $HESTIA/func/main.sh
  16. # JSON list function
  17. json_list() {
  18. objects=$(echo "$interfaces" |wc -l)
  19. i=1
  20. echo '['
  21. for interface in $interfaces; do
  22. echo -n ' "'$interface'"'
  23. if [ "$i" -lt "$objects" ]; then
  24. echo ','
  25. else
  26. echo
  27. fi
  28. ((i++))
  29. done
  30. echo ']'
  31. }
  32. # SHELL list function
  33. shell_list() {
  34. echo "INTERFACE"
  35. echo "---------"
  36. for interface in $interfaces; do
  37. echo "$interface"
  38. done
  39. }
  40. # PLAIN list function
  41. plain_list() {
  42. for interface in $interfaces; do
  43. echo "$interface"
  44. done
  45. }
  46. # CSV list function
  47. csv_list() {
  48. echo "INTERFACE"
  49. for interface in $interfaces; do
  50. echo "$interface"
  51. done
  52. }
  53. #----------------------------------------------------------#
  54. # Action #
  55. #----------------------------------------------------------#
  56. # Defining interface list
  57. interfaces=$(cat /proc/net/dev |grep : |cut -f 1 -d : |tr -d ' ' |grep -v lo)
  58. # Listing data
  59. case $format in
  60. json) json_list ;;
  61. plain) plain_list ;;
  62. csv) csv_list ;;
  63. shell) shell_list;;
  64. esac
  65. #----------------------------------------------------------#
  66. # Hestia #
  67. #----------------------------------------------------------#
  68. exit