-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (115 loc) · 2.98 KB
/
main.go
File metadata and controls
136 lines (115 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"time"
"golang.org/x/crypto/ssh"
)
type Connection struct {
Ip string
Username string
Project string
Password string
Command string
Description string
Port int64
}
//Connection configuration
type ClientConfig struct {
Host string //ip
Port int64 // Port
Username string //Username
Password string //Password
Client *ssh.Client //ssh client
LastResult string //Result of the last run
}
func main() {
// var err error
if len(os.Args) > 1 {
var server Connection
project := os.Args[1]
server = find(project)
cliConf := new(ClientConfig)
cliConf.createClient(server.Ip, server.Port, server.Username, server.Password)
fmt.Println(server.Description)
fmt.Println("===========================")
fmt.Println(cliConf.RunShell(server.Command))
} else {
fmt.Println("Sunucu ismi belirtiniz...!")
}
}
// Sunucu bilgileri
func find(project string) Connection {
connectionlist := read_connections()
for i := range connectionlist {
if connectionlist[i].Project == project {
return connectionlist[i]
}
}
var emty Connection
return emty
}
// Sunucularımız
func read_connections() []Connection {
content, err := ioutil.ReadFile("connections.json") // dosya yolunu belirmeniz gerekiyor
if err != nil {
log.Fatal("Error when opening file: ", err)
}
var payload []Connection
err = json.Unmarshal(content, &payload)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
}
return payload
}
// // SSH Komutu
// func make_ssh(server Connection) string {
// return "ssh " + server.Username + "@" + server.Ip + " -p " + server.Port
// }
func (cliConf *ClientConfig) createClient(host string, port int64, username, password string) {
var (
client *ssh.Client
err error
)
cliConf.Host = host
cliConf.Port = port
cliConf.Username = username
cliConf.Password = password
cliConf.Port = port
//Generally pass in four parameters: user, []ssh.AuthMethod{ssh.Password(password)}, HostKeyCallback, timeout,
config := ssh.ClientConfig{
User: cliConf.Username,
Auth: []ssh.AuthMethod{ssh.Password(password)},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Timeout: 10 * time.Second,
}
addr := fmt.Sprintf("%s:%d", cliConf.Host, cliConf.Port)
//Get client
if client, err = ssh.Dial("tcp", addr, &config); err != nil {
log.Fatalln("error occurred:", err)
}
cliConf.Client = client
}
func (cliConf *ClientConfig) RunShell(shell string) string {
var (
session *ssh.Session
err error
)
//Get session, this session is used to perform operations remotely
if session, err = cliConf.Client.NewSession(); err != nil {
log.Fatalln("error occurred:", err)
}
//Execute shell
if output, err := session.CombinedOutput(shell); err != nil {
log.Fatalln("error occurred:", err)
} else {
cliConf.LastResult = string(output)
}
return cliConf.LastResult
}