-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshutil.go
70 lines (58 loc) · 1.37 KB
/
sshutil.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
package sshutil
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"net"
"time"
"golang.org/x/crypto/ssh"
)
var (
bitSize = 2048
port = "22"
)
// SSHClient is a wrapper around the ssh.Client
type SSHClient struct {
conn *ssh.Client
config *ssh.ClientConfig
}
// GenerateNewSSHKeys generates a new SSH key pair
func GenerateNewSSHKeys() ([]byte, []byte, error) {
// Generate private key
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
return nil, nil, err
}
// Generate private key PEM
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyPEM := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: privateKeyBytes,
},
)
// Generate public key
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return nil, nil, err
}
// Generate public key bytes
publicKeyBytes := ssh.MarshalAuthorizedKey(pub)
// Return the private key PEM and public key bytes
return privateKeyPEM, publicKeyBytes, nil
}
// Check if host is responding
func IsResponding(ip string) bool {
// Set timeout to 5 seconds
timeout := time.Second * 5
// Try to connect to the host
conn, err := net.DialTimeout("tcp", net.JoinHostPort(ip, port), timeout)
if err != nil {
return false
}
defer conn.Close()
// Return true if the connection is established
return conn != nil
}