Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elliotchance/sshtunnel
🚇 Ultra simple SSH tunnelling for Go programs.
https://github.com/elliotchance/sshtunnel
golang ssh ssh-tunnel sshtunnel
Last synced: 2 days ago
JSON representation
🚇 Ultra simple SSH tunnelling for Go programs.
- Host: GitHub
- URL: https://github.com/elliotchance/sshtunnel
- Owner: elliotchance
- License: mit
- Created: 2019-01-18T02:28:41.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-06T12:08:09.000Z (7 months ago)
- Last Synced: 2024-12-13T11:11:44.319Z (9 days ago)
- Topics: golang, ssh, ssh-tunnel, sshtunnel
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 311
- Watchers: 9
- Forks: 61
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🚇 sshtunnel
Ultra simple SSH tunnelling for Go programs.
## Installation
```bash
go get -u github.com/elliotchance/sshtunnel
```Or better with `dep`:
```bash
dep ensure -add github.com/elliotchance/sshtunnel
```## Example
```go
// Setup the tunnel, but do not yet start it yet.
tunnel := sshtunnel.NewSSHTunnel(
// User and host of tunnel server, it will default to port 22
// if not specified.
"[email protected]",// Pick ONE of the following authentication methods:
sshtunnel.PrivateKeyFile("path/to/private/key.pem"), // 1. private key
ssh.Password("password"), // 2. password
sshtunnel.SSHAgent(), // 3. ssh-agent// The destination host and port of the actual server.
"dqrsdfdssdfx.us-east-1.redshift.amazonaws.com:5439",
// The local port you want to bind the remote port to.
// Specifying "0" will lead to a random port.
"8443",
)// You can provide a logger for debugging, or remove this line to
// make it silent.
tunnel.Log = log.New(os.Stdout, "", log.Ldate | log.Lmicroseconds)// Start the server in the background. You will need to wait a
// small amount of time for it to bind to the localhost port
// before you can start sending connections.
go tunnel.Start()
time.Sleep(100 * time.Millisecond)// NewSSHTunnel will bind to a random port so that you can have
// multiple SSH tunnels available. The port is available through:
// tunnel.Local.Port// You can use any normal Go code to connect to the destination server
// through localhost. You may need to use 127.0.0.1 for some libraries.
//
// Here is an example of connecting to a PostgreSQL server:
conn := fmt.Sprintf("host=127.0.0.1 port=%d username=foo", tunnel.Local.Port)
db, err := sql.Open("postgres", conn)// ...
```