-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (97 loc) · 2.61 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"os"
"os/exec"
"github.com/go-resty/resty/v2"
)
// NetboxAPIResponse represents the response from the Netbox API for a device or VM
type NetboxAPIResponse struct {
Results []struct {
PrimaryIP struct {
Address string `json:"address"`
} `json:"primary_ip"`
} `json:"results"`
}
func main() {
// Define flags
username := flag.String("u", "", "Username for SSH connection")
flag.Parse()
if flag.NArg() != 1 {
fmt.Println("Usage: nbssh [-u username] <hostname>")
return
}
hostname := flag.Arg(0)
// Configure Netbox API
// Read environment variables
netboxURL := os.Getenv("NETBOX_URL")
apiToken := os.Getenv("NETBOX_API_TOKEN")
if netboxURL == "" || apiToken == "" {
log.Fatalf("NETBOX_URL and NETBOX_API_TOKEN environment variables must be set")
}
// Create a Resty client
client := resty.New()
// Function to get the primary IP from Netbox
getPrimaryIP := func(endpoint, hostname string) (string, error) {
resp, err := client.R().
SetHeader("Authorization", "Token "+apiToken).
SetQueryParam("name", hostname).
SetQueryParam("limit", "1").
Get(endpoint)
if err != nil {
return "", fmt.Errorf("error fetching data from Netbox: %v", err)
}
if resp.StatusCode() != 200 {
return "", fmt.Errorf("non-200 response from Netbox: %s", resp.Status())
}
var apiResponse NetboxAPIResponse
if err := json.Unmarshal(resp.Body(), &apiResponse); err != nil {
return "", fmt.Errorf("error unmarshaling response: %v", err)
}
if len(apiResponse.Results) == 0 {
return "", nil // No result found
}
primaryIP := apiResponse.Results[0].PrimaryIP.Address
ip, _, err := net.ParseCIDR(primaryIP)
if err != nil {
return "", fmt.Errorf("error parsing primary IP address: %v", err)
}
return ip.String(), nil
}
// Check both devices and virtual machines
endpoints := []string{
netboxURL + "/api/dcim/devices/",
netboxURL + "/api/virtualization/virtual-machines/",
}
var primaryIP string
for _, endpoint := range endpoints {
ip, err := getPrimaryIP(endpoint, hostname)
if err != nil {
log.Fatalf("Error: %v", err)
}
if ip != "" {
primaryIP = ip
break
}
}
if primaryIP == "" {
log.Fatalf("Host %s not found in Netbox", hostname)
}
// Build the SSH command
var sshCmd *exec.Cmd
if *username == "" {
sshCmd = exec.Command("ssh", primaryIP)
} else {
sshCmd = exec.Command("ssh", *username+"@"+primaryIP)
}
sshCmd.Stdin = os.Stdin
sshCmd.Stdout = os.Stdout
sshCmd.Stderr = os.Stderr
if err := sshCmd.Run(); err != nil {
log.Fatalf("Error initiating SSH connection: %v", err)
}
}