/
psh.go
172 lines (147 loc) · 3.54 KB
/
psh.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"golang.org/x/crypto/ssh"
"github.com/vallard/psh/nr"
"github.com/vallard/psh/server"
)
const maxSSHSessions int = 20
var wg sync.WaitGroup
func errorFunction(errMessage string) {
fmt.Printf("%s\n", errMessage)
os.Exit(1)
}
// prints the usage of the tool and exits with the value past in.
func useage(rc int) {
fmt.Printf("psh <noderange> command args args ...\n")
os.Exit(rc)
}
func main() {
if len(os.Args) < 2 {
useage(1)
}
// the first argument is the range or group of nodes
nodes, err := nr.GetNodeRange(os.Args[1])
if err != nil {
errorFunction(err.Error())
}
// open the file for ssh stuff.
cmd := getCommand(os.Args[2:])
//fmt.Printf("Running command: %s\n", cmd)
ch := make(chan string, maxSSHSessions)
wg.Add(len(nodes))
for _, server := range nodes {
go runcmd(server, cmd, ch)
}
/*for range nodes {
fmt.Print(<-ch)
}*/
wg.Wait()
}
func runcmd(server server.Server, cmd string, ch chan<- string) {
defer wg.Done()
auth := PublicKeyFile(server.Key)
if auth == nil {
fmt.Println("Invalid server key")
return
}
sshConfig := &ssh.ClientConfig{
User: server.User,
Auth: []ssh.AuthMethod{
auth,
},
}
// create connection
s := getServerAndPort(server)
connection, err := ssh.Dial("tcp", s, sshConfig)
if err != nil {
ch <- fmt.Sprint(err)
return
}
// create session
session, err := connection.NewSession()
if err != nil {
ch <- fmt.Sprint(err)
return
}
defer session.Close()
// create terminal
/*
modes := ssh.TerminalModes{
ssh.ECHO: 0,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
if err = session.RequestPty("xterm", 80, 40, modes); err != nil {
ch <- fmt.Sprintf("request for pseudo terminal failed: %s", err)
return
}
*/
stdout, err := session.StdoutPipe()
if err != nil {
//ch <- fmt.Errorf("Unable to setup stdout for session: %v", err)
ch <- fmt.Sprintf("Unable to setup stdout for session: %v", err)
return
}
stderr, err := session.StderrPipe()
if err != nil {
//ch <- fmt.Errorf("Unable to setup stderr for session: %v", err)
ch <- fmt.Sprintf("Unable to setup stderr for session: %v", err)
return
}
err = session.Run(cmd)
if err != nil {
// might need to do something here...
//fmt.Printf("%s: %v\n", err)
//return
}
// check the stdout for data and display.
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
fmt.Printf("%s: %s\n", server.Host, scanner.Text())
}
if err = scanner.Err(); err != nil {
fmt.Printf("%s: %v\n", server.Host, err)
}
// check the error output for errors.
errScanner := bufio.NewScanner(stderr)
for errScanner.Scan() {
fmt.Printf("%s: %s\n", server.Host, errScanner.Text())
}
if err = errScanner.Err(); err != nil {
fmt.Printf("%s: %v\n", server.Host, err)
}
return
}
func getServerAndPort(server server.Server) string {
if strings.Contains(server.IP, ":") {
return server.IP
}
s := []string{server.IP, "22"}
return strings.Join(s, ":")
}
// get the key file. cred: http://blog.ralch.com/tutorial/golang-ssh-connection/
func PublicKeyFile(file string) ssh.AuthMethod {
file = nr.ExpandShell(file)
fmt.Println("File", file)
buffer, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("couldn't read file: %s\n", file)
return nil
}
key, err := ssh.ParsePrivateKey(buffer)
if err != nil {
fmt.Printf("couldn't parse file: %s\n", file)
return nil
}
return ssh.PublicKeys(key)
}
// get the command from the command line
func getCommand(arr []string) string {
return strings.Join(arr, " ")
}