Guest User

Untitled

a guest
Dec 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bytes"
  5. "fmt"
  6. "golang.org/x/crypto/ssh"
  7. "html/template"
  8. "log"
  9. "net"
  10. "net/http"
  11. "os"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. )
  16.  
  17. func handleError(err error,priority bool)bool{
  18. if err!=nil{
  19. if priority{
  20. panic(err)
  21. }else{
  22. log.Println(err)
  23. return false
  24. }
  25. }
  26. return true
  27. }
  28.  
  29. type Lore struct {
  30. Lore []string
  31. Dir string
  32. }
  33.  
  34. type Data struct {
  35. User,Password,Host string
  36. Port int
  37. }
  38.  
  39. type Lores struct {
  40. Lore []Lore
  41. }
  42.  
  43. func (l *Lores) init(){
  44. l.Lore=make([]Lore,3)
  45. l.Lore[0],l.Lore[1],l.Lore[2]=Lore{Lore:make([]string,0)},Lore{Lore:make([]string,0)},Lore{Lore:make([]string,0)}
  46. }
  47.  
  48. func createConfig(data *Data) *ssh.ClientConfig{
  49. config:=&ssh.ClientConfig{
  50. Config: ssh.Config{},
  51. User: data.User,
  52. Auth: []ssh.AuthMethod{
  53. ssh.Password(data.Password),
  54. },
  55. HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
  56. return error(nil)
  57. },
  58. }
  59. return config
  60. }
  61.  
  62. func main(){
  63. var(
  64. conns=make([]*ssh.Client,3)
  65. lores Lores
  66. wg sync.WaitGroup
  67. data=make([]*Data,0)
  68. )
  69.  
  70. execute :=func (cmd string,conn *ssh.Client,lore *Lore){
  71. session,err:=conn.NewSession()
  72. handleError(err,false)
  73.  
  74. handleError(err,true)
  75.  
  76. var buf bytes.Buffer
  77.  
  78. session.Stdout=&buf
  79. session.Stderr=os.Stderr
  80.  
  81. err=session.Run(cmd)
  82. handleError(err,false)
  83.  
  84. if cmd=="df -h"{
  85. t:=strings.Split(buf.String()," ")
  86. lore.Lore = append(lore.Lore, t[26])
  87. }else{
  88. lore.Lore = append(lore.Lore, buf.String())
  89. }
  90. wg.Done()
  91. }
  92.  
  93.  
  94. http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
  95. http.ServeFile(writer,request,"index.html")
  96. })
  97. http.HandleFunc("/SSHAuthentication/result", func(writer http.ResponseWriter, request *http.Request) {
  98.  
  99. for i:=1;i<=3;i++{
  100. index:=strconv.Itoa(i)
  101. atoi, err := strconv.Atoi(request.FormValue("port" + index))
  102. handleError(err,true)
  103. data = append(data, &Data{
  104. User: request.FormValue("login" + index),
  105. Password: request.FormValue("password" + index),
  106. Host: request.FormValue("ip" + index),
  107. Port: atoi,
  108. })
  109. }
  110.  
  111. lores.init()
  112.  
  113. var err error
  114. config1:=createConfig(data[0])
  115. config2:=createConfig(data[1])
  116. config3:=createConfig(data[2])
  117.  
  118. conns[0],err=ssh.Dial("tcp",fmt.Sprintf("%s:%d",data[0].Host,data[0].Port),config1)
  119. handleError(err,true)
  120. conns[1],err=ssh.Dial("tcp",fmt.Sprintf("%s:%d",data[1].Host,data[1].Port),config2)
  121. handleError(err,true)
  122. conns[2],err=ssh.Dial("tcp",fmt.Sprintf("%s:%d",data[2].Host,data[2].Port),config3)
  123. handleError(err,true)
  124.  
  125. http.Redirect(writer,request,"/console",http.StatusSeeOther)
  126. })
  127.  
  128. http.HandleFunc("/console", func(writer http.ResponseWriter, request *http.Request) {
  129. t,_:=template.ParseFiles("try.html")
  130.  
  131. type A struct {
  132. Lore1,Lore2,Lore3 Lore
  133. Data1,Data2,Data3 string
  134. }
  135.  
  136. a:=A{
  137. Lore1: lores.Lore[0],
  138. Lore2: lores.Lore[1],
  139. Lore3: lores.Lore[2],
  140. Data1: data[0].User,
  141. Data2: data[1].User,
  142. Data3: data[2].User,
  143. }
  144.  
  145. _=t.Execute(writer,a)
  146. })
  147.  
  148. http.HandleFunc("/SSHData/parseData", func(writer http.ResponseWriter, request *http.Request) {
  149. cmd := request.FormValue("cmd")
  150. if cmd == "clear" {
  151. for i := range lores.Lore {
  152. lores.Lore[i].Lore = make([]string, 0)
  153. }
  154. cmd = ""
  155. }
  156. wg.Add(3)
  157. for i := range conns {
  158. go execute(cmd, conns[i], &lores.Lore[i])
  159. }
  160. wg.Wait()
  161. if cmd == "exit" {
  162. http.Redirect(writer, request, "/", http.StatusSeeOther)
  163. } else {
  164. http.Redirect(writer, request, "/console", http.StatusSeeOther)
  165. }
  166. })
  167.  
  168. err:=http.ListenAndServe("localhost:9000",nil)
  169. if err!=nil{
  170. log.Fatal(err)
  171. }
  172. }
  173.  
  174.  
  175. //////////////
Advertisement
Add Comment
Please, Sign In to add comment