-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuse.go
89 lines (75 loc) · 1.91 KB
/
cpuse.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
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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"golang.org/x/crypto/ssh"
)
func main() {
reader := bufio.NewReader(os.Stdin)
remoteHostName, userName, keyFile := getArgs()
cmdToRun := "ps aux | grep gunicorn"
if keyFile == "" {
// use private key
var err error
fmt.Print("Enter path to private key: ")
keyFile, err = reader.ReadString('\n')
if err != nil {
log.Fatal("Failed to read private key path: ", err)
}
keyFile = strings.TrimRight(keyFile, "\n")
}
key, err := ioutil.ReadFile(keyFile)
if err != nil {
log.Fatalf("Unable to read private key: %v", err)
}
// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatalf("Unable to parse private key: %v", err)
}
// a config shared between client and server
config := &ssh.ClientConfig{
User: userName,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
remoteHost := fmt.Sprintf("%s:%d", remoteHostName, 22)
log.Printf("Connecting to %s...\n", remoteHost)
client, err := ssh.Dial("tcp", remoteHost, config)
if err != nil {
log.Fatal("Failed to dial: ", err)
}
defer client.Close()
// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
session, err := client.NewSession()
if err != nil {
log.Fatal("Failed to create session: ", err)
}
defer session.Close()
// Once a Session is created, you can execute a single command on
// the remote side using the Run method.
var b bytes.Buffer
session.Stdout = &b
if err := session.Run(cmdToRun); err != nil {
log.Fatal("Failed to run: " + err.Error())
}
fmt.Println(b.String())
}
func getArgs() (string, string, string) {
if len(os.Args) > 3 {
return os.Args[1], os.Args[2], os.Args[3]
}
if len(os.Args) > 2 {
return os.Args[1], os.Args[2], ""
}
log.Fatal("usage incorrect")
return "", "", ""
}