| 1 |
# easyssh-proxy |
| 2 |
|
| 3 |
[](https://pkg.go.dev/github.com/appleboy/easyssh-proxy) |
| 4 |
[](https://github.com/appleboy/easyssh-proxy/actions/workflows/lint.yml) |
| 5 |
[](https://codecov.io/gh/appleboy/easyssh-proxy) |
| 6 |
[](https://goreportcard.com/report/github.com/appleboy/easyssh-proxy) |
| 7 |
[](https://sourcegraph.com/github.com/appleboy/easyssh-proxy?badge) |
| 8 |
|
| 9 |
easyssh-proxy provides a simple implementation of some SSH protocol features in Go. |
| 10 |
|
| 11 |
## Feature |
| 12 |
|
| 13 |
This project is forked from [easyssh](https://github.com/hypersleep/easyssh) but add some features as the following. |
| 14 |
|
| 15 |
* [x] Support plain text of user private key. |
| 16 |
* [x] Support key path of user private key. |
| 17 |
* [x] Support Timeout for the TCP connection to establish. |
| 18 |
* [x] Support SSH ProxyCommand. |
| 19 |
|
| 20 |
```bash |
| 21 |
+--------+ +----------+ +-----------+ |
| 22 |
| Laptop | <--> | Jumphost | <--> | FooServer | |
| 23 |
+--------+ +----------+ +-----------+ |
| 24 |
|
| 25 |
OR |
| 26 |
|
| 27 |
+--------+ +----------+ +-----------+ |
| 28 |
| Laptop | <--> | Firewall | <--> | FooServer | |
| 29 |
+--------+ +----------+ +-----------+ |
| 30 |
192.168.1.5 121.1.2.3 10.10.29.68 |
| 31 |
``` |
| 32 |
|
| 33 |
## Usage |
| 34 |
|
| 35 |
You can see `ssh`, `scp`, `ProxyCommand` on `examples` folder. |
| 36 |
|
| 37 |
### ssh |
| 38 |
|
| 39 |
See [example/ssh/ssh.go](./example/ssh/ssh.go) |
| 40 |
|
| 41 |
[embedmd]:# (example/ssh/ssh.go go) |
| 42 |
```go |
| 43 |
package main |
| 44 |
|
| 45 |
import ( |
| 46 |
"fmt" |
| 47 |
"time" |
| 48 |
|
| 49 |
"github.com/appleboy/easyssh-proxy" |
| 50 |
) |
| 51 |
|
| 52 |
func main() { |
| 53 |
// Create MakeConfig instance with remote username, server address and path to private key. |
| 54 |
ssh := &easyssh.MakeConfig{ |
| 55 |
User: "appleboy", |
| 56 |
Server: "example.com", |
| 57 |
// Optional key or Password without either we try to contact your agent SOCKET |
| 58 |
// Password: "password", |
| 59 |
// Paste your source content of private key |
| 60 |
// Key: `-----BEGIN RSA PRIVATE KEY----- |
| 61 |
// MIIEpAIBAAKCAQEA4e2D/qPN08pzTac+a8ZmlP1ziJOXk45CynMPtva0rtK/RB26 |
| 62 |
// 7XC9wlRna4b3Ln8ew3q1ZcBjXwD4ppbTlmwAfQIaZTGJUgQbdsO9YA== |
| 63 |
// -----END RSA PRIVATE KEY----- |
| 64 |
// `, |
| 65 |
KeyPath: "/Users/username/.ssh/id_rsa", |
| 66 |
Port: "22", |
| 67 |
Timeout: 60 * time.Second, |
| 68 |
|
| 69 |
// Parse PrivateKey With Passphrase |
| 70 |
Passphrase: "1234", |
| 71 |
|
| 72 |
// Optional fingerprint SHA256 verification |
| 73 |
// Get Fingerprint: ssh.FingerprintSHA256(key) |
| 74 |
// Fingerprint: "SHA256:mVPwvezndPv/ARoIadVY98vAC0g+P/5633yTC4d/wXE" |
| 75 |
|
| 76 |
// Enable the use of insecure ciphers and key exchange methods. |
| 77 |
// This enables the use of the the following insecure ciphers and key exchange methods: |
| 78 |
// - aes128-cbc |
| 79 |
// - aes192-cbc |
| 80 |
// - aes256-cbc |
| 81 |
// - 3des-cbc |
| 82 |
// - diffie-hellman-group-exchange-sha256 |
| 83 |
// - diffie-hellman-group-exchange-sha1 |
| 84 |
// Those algorithms are insecure and may allow plaintext data to be recovered by an attacker. |
| 85 |
// UseInsecureCipher: true, |
| 86 |
} |
| 87 |
|
| 88 |
// Call Run method with command you want to run on remote server. |
| 89 |
stdout, stderr, done, err := ssh.Run("ls -al", 60*time.Second) |
| 90 |
// Handle errors |
| 91 |
if err != nil { |
| 92 |
panic("Can't run remote command: " + err.Error()) |
| 93 |
} else { |
| 94 |
fmt.Println("don is :", done, "stdout is :", stdout, "; stderr is :", stderr) |
| 95 |
} |
| 96 |
} |
| 97 |
``` |
| 98 |
|
| 99 |
### scp |
| 100 |
|
| 101 |
See [example/scp/scp.go](./example/scp/scp.go) |
| 102 |
|
| 103 |
[embedmd]:# (example/scp/scp.go go) |
| 104 |
```go |
| 105 |
package main |
| 106 |
|
| 107 |
import ( |
| 108 |
"fmt" |
| 109 |
|
| 110 |
"github.com/appleboy/easyssh-proxy" |
| 111 |
) |
| 112 |
|
| 113 |
func main() { |
| 114 |
// Create MakeConfig instance with remote username, server address and path to private key. |
| 115 |
ssh := &easyssh.MakeConfig{ |
| 116 |
User: "appleboy", |
| 117 |
Server: "example.com", |
| 118 |
Password: "123qwe", |
| 119 |
Port: "22", |
| 120 |
} |
| 121 |
|
| 122 |
// Call Scp method with file you want to upload to remote server. |
| 123 |
// Please make sure the `tmp` floder exists. |
| 124 |
err := ssh.Scp("/root/source.csv", "/tmp/target.csv") |
| 125 |
|
| 126 |
// Handle errors |
| 127 |
if err != nil { |
| 128 |
panic("Can't run remote command: " + err.Error()) |
| 129 |
} else { |
| 130 |
fmt.Println("success") |
| 131 |
} |
| 132 |
} |
| 133 |
``` |
| 134 |
|
| 135 |
### SSH ProxyCommand |
| 136 |
|
| 137 |
See [example/proxy/proxy.go](./example/proxy/proxy.go) |
| 138 |
|
| 139 |
[embedmd]:# (example/proxy/proxy.go go /\tssh :=/ /\t}$/) |
| 140 |
```go |
| 141 |
ssh := &easyssh.MakeConfig{ |
| 142 |
User: "drone-scp", |
| 143 |
Server: "localhost", |
| 144 |
Port: "22", |
| 145 |
KeyPath: "./tests/.ssh/id_rsa", |
| 146 |
Proxy: easyssh.DefaultConfig{ |
| 147 |
User: "drone-scp", |
| 148 |
Server: "localhost", |
| 149 |
Port: "22", |
| 150 |
KeyPath: "./tests/.ssh/id_rsa", |
| 151 |
}, |
| 152 |
} |
| 153 |
``` |
| 154 |
|
| 155 |
### SSH Stream Log |
| 156 |
|
| 157 |
See [example/stream/stream.go](./example/stream/stream.go) |
| 158 |
|
| 159 |
[embedmd]:# (example/stream/stream.go go /func/ /^}$/) |
| 160 |
```go |
| 161 |
func main() { |
| 162 |
// Create MakeConfig instance with remote username, server address and path to private key. |
| 163 |
ssh := &easyssh.MakeConfig{ |
| 164 |
Server: "localhost", |
| 165 |
User: "drone-scp", |
| 166 |
KeyPath: "./tests/.ssh/id_rsa", |
| 167 |
Port: "22", |
| 168 |
Timeout: 60 * time.Second, |
| 169 |
} |
| 170 |
|
| 171 |
// Call Run method with command you want to run on remote server. |
| 172 |
stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream("for i in {1..5}; do echo ${i}; sleep 1; done; exit 2;", 60*time.Second) |
| 173 |
// Handle errors |
| 174 |
if err != nil { |
| 175 |
panic("Can't run remote command: " + err.Error()) |
| 176 |
} else { |
| 177 |
// read from the output channel until the done signal is passed |
| 178 |
isTimeout := true |
| 179 |
loop: |
| 180 |
for { |
| 181 |
select { |
| 182 |
case isTimeout = <-doneChan: |
| 183 |
break loop |
| 184 |
case outline := <-stdoutChan: |
| 185 |
fmt.Println("out:", outline) |
| 186 |
case errline := <-stderrChan: |
| 187 |
fmt.Println("err:", errline) |
| 188 |
case err = <-errChan: |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// get exit code or command error. |
| 193 |
if err != nil { |
| 194 |
fmt.Println("err: " + err.Error()) |
| 195 |
} |
| 196 |
|
| 197 |
// command time out |
| 198 |
if !isTimeout { |
| 199 |
fmt.Println("Error: command timeout") |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
``` |