-
Notifications
You must be signed in to change notification settings - Fork 3
/
ssh.go
46 lines (34 loc) · 933 Bytes
/
ssh.go
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
package main
import (
"custom-browser-ssh-terminal/config"
"golang.org/x/crypto/ssh"
)
func sshCmd(server config.Server, command config.Command) string {
client, session, err := connectToHost(server.UserName, server.HostName+":"+server.Port, server.Password)
if err != nil {
return "Connection is refused"
}
out, err := session.CombinedOutput(command.RealCmd)
if err != nil {
return "Command runtime error."
}
client.Close()
return string(out)
}
func connectToHost(user, host string, pass string) (*ssh.Client, *ssh.Session, error) {
sshConfig := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.Password(pass)},
}
sshConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
client, err := ssh.Dial("tcp", host, sshConfig)
if err != nil {
return nil, nil, err
}
session, err := client.NewSession()
if err != nil {
client.Close()
return nil, nil, err
}
return client, session, nil
}