/
example_set_host_key_callback_shell.go
73 lines (58 loc) · 1.53 KB
/
example_set_host_key_callback_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
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2020 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 any HostKeyCallback use Example file.
// Change the value of the variable and compile to make sure that you can actually connect.
//
// This file uses key authentication. Please replace as appropriate.
package main
import (
"fmt"
"os"
sshlib "github.com/blacknon/go-sshlib"
"golang.org/x/crypto/ssh"
)
var (
host = "target.com"
port = "22"
user = "user"
privkey = "~/.ssh/sshenc_key"
keypass = "password"
termlog = "./test_termlog"
)
func main() {
// Create sshlib.Connect
con := &sshlib.Connect{
// If you use x11 forwarding, please set to true.
ForwardX11: false,
// If you use ssh-agent forwarding, please set to true.
// And after, run `con.ConnectSshAgent()`.
ForwardAgent: false,
}
// set any HostKeyCallback.
con.HostKeyCallback = ssh.InsecureIgnoreHostKey()
// Create ssh.AuthMethods
authMethod, err := sshlib.CreateAuthMethodPublicKey(privkey, keypass)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// If you use ssh-agent forwarding, uncomment it.
// con.ConnectSshAgent()
// Connect ssh server
err = con.CreateClient(host, port, user, []ssh.AuthMethod{authMethod})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 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)
}