Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bytes"
- "fmt"
- "golang.org/x/crypto/ssh"
- "html/template"
- "log"
- "net"
- "net/http"
- "os"
- "strconv"
- "strings"
- "sync"
- )
- func handleError(err error,priority bool)bool{
- if err!=nil{
- if priority{
- panic(err)
- }else{
- log.Println(err)
- return false
- }
- }
- return true
- }
- type Lore struct {
- Lore []string
- Dir string
- }
- type Data struct {
- User,Password,Host string
- Port int
- }
- type Lores struct {
- Lore []Lore
- }
- func (l *Lores) init(){
- l.Lore=make([]Lore,3)
- l.Lore[0],l.Lore[1],l.Lore[2]=Lore{Lore:make([]string,0)},Lore{Lore:make([]string,0)},Lore{Lore:make([]string,0)}
- }
- func createConfig(data *Data) *ssh.ClientConfig{
- config:=&ssh.ClientConfig{
- Config: ssh.Config{},
- User: data.User,
- Auth: []ssh.AuthMethod{
- ssh.Password(data.Password),
- },
- HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
- return error(nil)
- },
- }
- return config
- }
- func main(){
- var(
- conns=make([]*ssh.Client,3)
- lores Lores
- wg sync.WaitGroup
- data=make([]*Data,0)
- )
- execute :=func (cmd string,conn *ssh.Client,lore *Lore){
- session,err:=conn.NewSession()
- handleError(err,false)
- handleError(err,true)
- var buf bytes.Buffer
- session.Stdout=&buf
- session.Stderr=os.Stderr
- err=session.Run(cmd)
- handleError(err,false)
- if cmd=="df -h"{
- t:=strings.Split(buf.String()," ")
- lore.Lore = append(lore.Lore, t[26])
- }else{
- lore.Lore = append(lore.Lore, buf.String())
- }
- wg.Done()
- }
- http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
- http.ServeFile(writer,request,"index.html")
- })
- http.HandleFunc("/SSHAuthentication/result", func(writer http.ResponseWriter, request *http.Request) {
- for i:=1;i<=3;i++{
- index:=strconv.Itoa(i)
- atoi, err := strconv.Atoi(request.FormValue("port" + index))
- handleError(err,true)
- data = append(data, &Data{
- User: request.FormValue("login" + index),
- Password: request.FormValue("password" + index),
- Host: request.FormValue("ip" + index),
- Port: atoi,
- })
- }
- lores.init()
- var err error
- config1:=createConfig(data[0])
- config2:=createConfig(data[1])
- config3:=createConfig(data[2])
- conns[0],err=ssh.Dial("tcp",fmt.Sprintf("%s:%d",data[0].Host,data[0].Port),config1)
- handleError(err,true)
- conns[1],err=ssh.Dial("tcp",fmt.Sprintf("%s:%d",data[1].Host,data[1].Port),config2)
- handleError(err,true)
- conns[2],err=ssh.Dial("tcp",fmt.Sprintf("%s:%d",data[2].Host,data[2].Port),config3)
- handleError(err,true)
- http.Redirect(writer,request,"/console",http.StatusSeeOther)
- })
- http.HandleFunc("/console", func(writer http.ResponseWriter, request *http.Request) {
- t,_:=template.ParseFiles("try.html")
- type A struct {
- Lore1,Lore2,Lore3 Lore
- Data1,Data2,Data3 string
- }
- a:=A{
- Lore1: lores.Lore[0],
- Lore2: lores.Lore[1],
- Lore3: lores.Lore[2],
- Data1: data[0].User,
- Data2: data[1].User,
- Data3: data[2].User,
- }
- _=t.Execute(writer,a)
- })
- http.HandleFunc("/SSHData/parseData", func(writer http.ResponseWriter, request *http.Request) {
- cmd := request.FormValue("cmd")
- if cmd == "clear" {
- for i := range lores.Lore {
- lores.Lore[i].Lore = make([]string, 0)
- }
- cmd = ""
- }
- wg.Add(3)
- for i := range conns {
- go execute(cmd, conns[i], &lores.Lore[i])
- }
- wg.Wait()
- if cmd == "exit" {
- http.Redirect(writer, request, "/", http.StatusSeeOther)
- } else {
- http.Redirect(writer, request, "/console", http.StatusSeeOther)
- }
- })
- err:=http.ListenAndServe("localhost:9000",nil)
- if err!=nil{
- log.Fatal(err)
- }
- }
- //////////////
Advertisement
Add Comment
Please, Sign In to add comment