command_helper.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package command
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. )
  13. func OnlyExec(cmdStr string) {
  14. cmd := exec.Command("/bin/bash", "-c", cmdStr)
  15. stdout, err := cmd.StdoutPipe()
  16. if err != nil {
  17. return
  18. }
  19. defer stdout.Close()
  20. if err := cmd.Start(); err != nil {
  21. return
  22. }
  23. cmd.Wait()
  24. return
  25. }
  26. func ExecResultStrArray(cmdStr string) []string {
  27. cmd := exec.Command("/bin/bash", "-c", cmdStr)
  28. stdout, err := cmd.StdoutPipe()
  29. if err != nil {
  30. fmt.Println(err)
  31. return nil
  32. }
  33. defer stdout.Close()
  34. if err = cmd.Start(); err != nil {
  35. fmt.Println(err)
  36. return nil
  37. }
  38. // str, err := ioutil.ReadAll(stdout)
  39. networklist := []string{}
  40. outputBuf := bufio.NewReader(stdout)
  41. for {
  42. output, _, err := outputBuf.ReadLine()
  43. if err != nil {
  44. if err.Error() != "EOF" {
  45. fmt.Printf("Error :%s\n", err)
  46. }
  47. break
  48. }
  49. networklist = append(networklist, string(output))
  50. }
  51. cmd.Wait()
  52. return networklist
  53. }
  54. func ExecResultStr(cmdStr string) string {
  55. cmd := exec.Command("/bin/bash", "-c", cmdStr)
  56. println(cmd.String())
  57. stdout, err := cmd.StdoutPipe()
  58. if err != nil {
  59. fmt.Println(err)
  60. return ""
  61. }
  62. defer stdout.Close()
  63. if err := cmd.Start(); err != nil {
  64. fmt.Println(err)
  65. return ""
  66. }
  67. str, err := ioutil.ReadAll(stdout)
  68. cmd.Wait()
  69. if err != nil {
  70. fmt.Println(err)
  71. return ""
  72. }
  73. return string(str)
  74. }
  75. // 执行 lsblk 命令
  76. func ExecLSBLK() []byte {
  77. output, err := exec.Command("lsblk", "-O", "-J", "-b").Output()
  78. if err != nil {
  79. fmt.Println("lsblk", err)
  80. return nil
  81. }
  82. return output
  83. }
  84. // 执行 lsblk 命令
  85. func ExecLSBLKByPath(path string) []byte {
  86. output, err := exec.Command("lsblk", path, "-O", "-J", "-b").Output()
  87. if err != nil {
  88. fmt.Println("lsblk", err)
  89. return nil
  90. }
  91. return output
  92. }
  93. // exec smart
  94. func ExecSmartCTLByPath(path string) []byte {
  95. timeout := 3
  96. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
  97. defer cancel()
  98. output, err := exec.CommandContext(ctx, "smartctl", "-a", path, "-j").Output()
  99. if err != nil {
  100. fmt.Println("smartctl", err)
  101. return nil
  102. }
  103. return output
  104. }
  105. func ExecEnabledSMART(path string) {
  106. exec.Command("smartctl", "-s on", path).Output()
  107. }
  108. func ExecuteScripts(scriptDirectory string) {
  109. if _, err := os.Stat(scriptDirectory); os.IsNotExist(err) {
  110. fmt.Printf("No post-start scripts at %s\n", scriptDirectory)
  111. return
  112. }
  113. files, err := os.ReadDir(scriptDirectory)
  114. if err != nil {
  115. fmt.Printf("Failed to read from script directory %s: %s\n", scriptDirectory, err.Error())
  116. return
  117. }
  118. for _, file := range files {
  119. if file.IsDir() {
  120. continue
  121. }
  122. scriptFilepath := filepath.Join(scriptDirectory, file.Name())
  123. f, err := os.Open(scriptFilepath)
  124. if err != nil {
  125. fmt.Printf("Failed to open script file %s: %s\n", scriptFilepath, err.Error())
  126. continue
  127. }
  128. f.Close()
  129. scanner := bufio.NewScanner(f)
  130. scanner.Scan()
  131. shebang := scanner.Text()
  132. interpreter := "/bin/sh"
  133. if strings.HasPrefix(shebang, "#!") {
  134. interpreter = shebang[2:]
  135. }
  136. cmd := exec.Command(interpreter, scriptFilepath)
  137. fmt.Printf("Executing post-start script %s using %s\n", scriptFilepath, interpreter)
  138. cmd.Stdout = os.Stdout
  139. cmd.Stderr = os.Stderr
  140. err = cmd.Run()
  141. if err != nil {
  142. fmt.Printf("Failed to execute post-start script %s: %s\n", scriptFilepath, err.Error())
  143. }
  144. }
  145. fmt.Println("Finished executing post-start scripts.")
  146. }