forked from jdolitsky/go-pivot-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·81 lines (75 loc) · 1.68 KB
/
main.go
File metadata and controls
executable file
·81 lines (75 loc) · 1.68 KB
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
package main
import (
"fmt"
"log"
"os"
"strings"
"syscall"
"github.com/elliotchance/sshtunnel"
sshconfig "github.com/kevinburke/ssh_config"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
type config struct {
host string
method ssh.AuthMethod
target string
listen string
}
func main() {
c := getConfig()
tunnel := sshtunnel.NewSSHTunnel(c.host, c.method, c.target, "")
tunnel.Local = sshtunnel.NewEndpoint(c.listen)
tunnel.Log = log.New(os.Stdout, "", log.Ldate|log.Lmicroseconds)
tunnel.Start()
}
func usage() {
fmt.Println("Usage: pivot-ssh <remote_host> <local_listener> <forward_to>")
fmt.Println()
fmt.Println(" Example:")
fmt.Println()
fmt.Println(" $ pivot-ssh 10.11.1.123 127.0.0.1:8080 10.1.1.55:80")
fmt.Println()
os.Exit(1)
}
func getConfig() config {
args := os.Args
if len(args) != 4 {
usage()
}
host := args[1]
listen := args[2]
target := args[3]
if host == "" || listen == "" || target == "" {
usage()
}
if user := sshconfig.Get(host, "User"); user == "" {
log.Fatalf("SSH config did not contain user for host %s\n", host)
os.Exit(1)
} else {
host = user + "@" + host
}
port := "22"
if portOverride := sshconfig.Get(host, "Port"); portOverride != "" {
port = portOverride
}
host = host + ":" + port
var method ssh.AuthMethod
if key := sshconfig.Get(host, "IdentityFile"); key != "" && !strings.HasSuffix(key, "identity") {
method = sshtunnel.PrivateKeyFile(key)
} else {
fmt.Print("Enter SSH password: ")
b, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Fatal(err)
}
fmt.Println()
method = ssh.Password(string(b))
}
return config{
host: host,
method: method,
target: target,
listen: listen,
}
}