-
Notifications
You must be signed in to change notification settings - Fork 0
/
xscp.go
181 lines (147 loc) · 3.66 KB
/
xscp.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
173
174
175
176
177
178
179
180
181
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"github.com/fatih/color"
"jjz.io/xscp/auth"
"jjz.io/xscp/utils"
"path"
"path/filepath"
"runtime"
"sync"
"golang.org/x/crypto/ssh"
"os"
"strings"
)
var (
hostFile string
priKey string
username string
isCli bool
shell string
)
func main() {
// 解析参数
parseArgs()
// 打开hosts文件
hosts, err := os.Open(hostFile)
if err != nil {
fmt.Printf("Exit with open host file error: %s\n", err.Error())
return
}
clientConfig, _ := auth.PrivateKey(username, priKey, ssh.InsecureIgnoreHostKey())
var result string
scanner := bufio.NewScanner(hosts)
var hostList []string
for scanner.Scan() {
host := scanner.Text()
// 跳过空行和注释
if host == "" || strings.HasPrefix(host, "#") {
continue
}
// 默认22端口
if len(strings.Split(host, ":")) == 1 {
host = host + ":22"
}
hostList = append(hostList, host)
}
wg := sync.WaitGroup{}
wg.Add(len(hostList))
greed := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
for _, host := range hostList {
host := host
go func() {
if isCli {
result, err = doShell(host, &clientConfig, shell)
} else {
result, err = doScp(host, &clientConfig, flag.Arg(0), flag.Arg(1))
}
if err == nil {
fmt.Printf("%s - %s \n%s\n", greed("[SUCCESS]"), yellow(host), result)
} else {
fmt.Printf("%s - %s \nexited with %s\n", red("[FAILURE]"), yellow(host), err.Error())
}
wg.Done()
}()
}
wg.Wait()
}
func parseArgs() {
hostFileEnv := os.Getenv("XSCP_HOST_FILE")
flag.StringVar(&hostFile, "f", hostFileEnv, "hosts file")
priKeyEnv := os.Getenv("XSCP_PRI_KEY")
flag.StringVar(&priKey, "k", priKeyEnv, "private key")
usernameEnv := os.Getenv("XSCP_USERNAME")
flag.StringVar(&username, "u", usernameEnv, "username")
// 执行一条命令
flag.BoolVar(&isCli, "c", false, "exec shell")
/*host := flag.String("h", "", "host")
overwrite := flag.Bool("o", false, "overwrite if exist")*/
flag.Parse()
if !isCli && flag.NArg() != 2 {
flag.PrintDefaults()
return
}
if isCli {
shell = strings.Join(flag.Args(), " ")
}
}
func doScp(host string, clientConfig *ssh.ClientConfig, localFile string, remotePath string) (string, error) {
// 获取文件
file, err := os.Open(localFile)
if err != nil {
return "", err
}
stat, err := file.Stat()
if err != nil {
return "", err
}
if stat.IsDir() {
return "", errors.New("不支持远程复制目录")
}
// ssh连接
client := auth.NewClient(host, clientConfig)
defer client.Close()
err = client.Connect()
if err != nil {
return "", err
}
// todo: 远程目录不存在则创建
// todo: 复制目录
// 获取文件名
var filename string
switch runtime.GOOS {
case "windows":
filename = path.Base(filepath.ToSlash(localFile))
case "linux":
filename = path.Base(localFile)
}
// todo 目前只支持向linux服务器拷贝
if []rune(remotePath)[len(remotePath)-1] != '/' {
remotePath = remotePath + "/"
}
err = client.CopyFile(file, remotePath+filename, utils.ConvertPerm(stat.Mode().String()))
if err != nil {
return "", err
}
hint := "copy: " + file.Name() + " --> " + remotePath + filename + "\n"
return hint, nil
}
func doShell(host string, clientConfig *ssh.ClientConfig, shell string) (string, error) {
client := auth.NewClient(host, clientConfig)
defer client.Close()
err := client.Connect()
if err != nil {
return "", err
}
// todo 执行类似sudo命令时要求输入密码,需提供交互式shell
buf, err := client.Session.CombinedOutput(shell)
if err != nil {
return "", errors.New(string(buf))
}
return string(buf), nil
}