-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
180 lines (176 loc) · 4.04 KB
/
main.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
// Copyright (c) 2017-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/pkg/sftp"
"github.com/txthinking/hancock"
"github.com/urfave/cli/v2"
)
func main() {
app := cli.NewApp()
app.Name = "sshexec"
app.Version = "20240925"
app.Usage = "Run command on remote server"
app.Authors = []*cli.Author{
{
Name: "Cloud",
Email: "[email protected]",
},
}
app.Copyright = "https://www.txthinking.com"
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "server",
Aliases: []string{"s"},
Usage: "Server address, like: 1.2.3.4:22",
},
&cli.StringFlag{
Name: "user",
Aliases: []string{"u"},
Usage: "user",
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "password",
},
&cli.StringFlag{
Name: "key",
Aliases: []string{"k"},
Usage: "private key",
},
&cli.StringFlag{
Name: "command",
Aliases: []string{"c"},
Usage: "command will be run on remote server, ignore upload/download",
},
&cli.BoolFlag{
Name: "sudo",
Usage: "run command with sudo, does not apply to download and upload",
},
&cli.StringFlag{
Name: "upload",
Usage: "upload file to remote server",
},
&cli.StringFlag{
Name: "download",
Usage: "download file from remote server",
},
&cli.StringFlag{
Name: "to",
Usage: "dst with upload/download",
},
&cli.IntFlag{
Name: "timeout",
Usage: "seconds",
Value: 60,
},
}
app.Action = func(c *cli.Context) error {
if c.String("server") == "" || c.String("user") == "" {
cli.ShowAppHelp(c)
return nil
}
if c.String("command") == "" && c.String("upload") == "" && c.String("download") == "" {
cli.ShowAppHelp(c)
return nil
}
if (c.String("upload") != "" || c.String("download") != "") && c.String("to") == "" {
cli.ShowAppHelp(c)
return nil
}
var b []byte
if c.String("key") != "" {
var err error
b, err = os.ReadFile(c.String("key"))
if err != nil {
return err
}
}
i, err := hancock.NewInstance(c.String("server"), c.String("user"), c.String("password"), b, c.Int("timeout"))
if err != nil {
return err
}
defer i.Client.Close()
if c.String("command") != "" {
s, err := i.Client.NewSession()
if err != nil {
return err
}
defer s.Close()
s.Stdout = os.Stdout
s.Stderr = os.Stderr
cmd := c.String("command")
if c.Bool("sudo") {
cmd = fmt.Sprintf("sudo -H -u root sh -c 'PATH=/root/.nami/bin:$PATH %s'", cmd)
}
err = s.Run(cmd)
if err != nil {
return err
}
return nil
}
if c.String("upload") != "" {
src, err := os.Open(c.String("upload"))
if err != nil {
return err
}
defer src.Close()
sc, err := sftp.NewClient(i.Client)
if err != nil {
return err
}
defer sc.Close()
dst, err := sc.Create(c.String("to"))
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return err
}
return nil
}
if c.String("download") != "" {
sc, err := sftp.NewClient(i.Client)
if err != nil {
return err
}
defer sc.Close()
src, err := sc.Open(c.String("download"))
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(c.String("to"))
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return err
}
return nil
}
return nil
}
if err := app.Run(os.Args); err != nil {
log.Println(err)
os.Exit(1)
}
}