forked from blacknon/go-sshlib
/
example_portforward_shell.go
60 lines (47 loc) · 1.2 KB
/
example_portforward_shell.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
// Copyright (c) 2021 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Shell connection and port forwarding Example file.
// Change the value of the variable and compile to make sure that you can actually connect.
//
// This file uses password authentication. Please replace as appropriate.
package main
import (
"fmt"
"os"
"github.com/abakum/go-sshlib"
"golang.org/x/crypto/ssh"
)
var (
host = "target.com"
port = "22"
user = "user"
password = "password"
localAddr = "localhost:10022"
remoteAddr = "localhost:22"
termlog = "./test_termlog"
)
func main() {
// Create sshlib.Connect
con := &sshlib.Connect{}
// Create ssh.AuthMethod
authMethod := sshlib.CreateAuthMethodPassword(password)
// Connect ssh server
err := con.CreateClient(host, port, user, []ssh.AuthMethod{authMethod})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// PortForward
con.TCPLocalForward(localAddr, remoteAddr)
// Set terminal log
con.SetLog(termlog, false)
// Create session
session, err := con.CreateSession()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Start ssh shell
con.Shell(session)
}