├── bash └── completion-go.sh ├── README.md └── main.go /bash/completion-go.sh: -------------------------------------------------------------------------------- 1 | #/usr/bin/env bash 2 | _netbox_completions() 3 | { 4 | ARGS=`$GOPATH/bin/netbox-bash-completion -netbox -netbox-api-token -netbox-device ${COMP_WORDS[1]} -tld "" 2> /dev/null` 5 | 6 | if [ $? -eq 0 ]; then 7 | COMPREPLY=($(compgen -W "$ARGS" -- ${COMP_WORDS[COMP_CWORD]} )) 8 | fi 9 | } 10 | 11 | complete -F _netbox_completions ssh 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Netbox-Autocompletion 2 | Looks up devices from Netbox and offers autocompletion for SSH 3 | 4 | ### Usage 5 | ``` 6 | Usage of ./netbox-bash-completion: 7 | -netbox string 8 | Netbox BaseURL (default "https://netbox.local") 9 | -netbox-api-token string 10 | Mandatory: Netbox API Token 11 | -netbox-device string 12 | Device String to search for 13 | -tld string 14 | Default TLD for devices (default "local") 15 | ``` 16 | 17 | ### Add this to your .bashrc 18 | ```source $GOTPATH/src/github.com/awlx/netbox-bash-completion/bash/completion-go.sh``` 19 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "flag" 6 | "fmt" 7 | "io/ioutil" 8 | "net/http" 9 | "time" 10 | ) 11 | 12 | var ( 13 | netbox = flag.String("netbox", "https://netbox.local", "Netbox BaseURL") 14 | netboxAPIToken = flag.String("netbox-api-token", "", "Mandatory: Netbox API Token") 15 | netboxDevice = flag.String("netbox-device", "", "Device String to search for") 16 | tld = flag.String("tld", ".local", "Default TLD for devices") 17 | ) 18 | 19 | // NetboxResult the whole Json Reply 20 | type NetboxResult struct { 21 | Count int `json:"count"` 22 | Next interface{} `json:"next"` 23 | Previous interface{} `json:"previous"` 24 | Results []NetboxResultList `json:"results"` 25 | } 26 | 27 | // NetboxResultList all Results of the json call 28 | type NetboxResultList struct { 29 | ID int `json:"id"` 30 | Name string `json:"name,omitempty"` 31 | Address string `json:"address,omitempty"` 32 | } 33 | 34 | func main() { 35 | flag.Parse() 36 | if *netboxAPIToken == "" { 37 | panic("Please provide a netbox-api-token") 38 | } 39 | 40 | netboxClient := http.Client{ 41 | Timeout: time.Second * 2, 42 | } 43 | 44 | devices := make(map[string]bool) 45 | 46 | devices = getAllDevices(netboxClient, *netboxDevice) 47 | for device := range devices { 48 | fmt.Println(fmt.Sprintf("%s%s", device, *tld)) 49 | } 50 | } 51 | 52 | func getAllDevices(netboxClient http.Client, searchString string) (netboxDevices map[string]bool) { 53 | deviceMap := make(map[string]bool) 54 | 55 | deviceURL := fmt.Sprintf("%s/api/dcim/devices/?q=%s", *netbox, searchString) 56 | vmURL := fmt.Sprintf("%s/api/virtualization/virtual-machines/?q=%s", *netbox, searchString) 57 | 58 | // Query devices 59 | req, err := http.NewRequest(http.MethodGet, deviceURL, nil) 60 | if err != nil { 61 | panic(err) 62 | } 63 | 64 | req.Header.Set("Authorization", fmt.Sprintf("Token %s", *netboxAPIToken)) 65 | 66 | res, err := netboxClient.Do(req) 67 | if err != nil { 68 | panic(err) 69 | } 70 | 71 | body, err := ioutil.ReadAll(res.Body) 72 | defer res.Body.Close() 73 | if err != nil { 74 | panic(err) 75 | } 76 | 77 | var netboxDeviceList NetboxResult 78 | 79 | bodyErr := json.Unmarshal(body, &netboxDeviceList) 80 | 81 | if bodyErr != nil { 82 | fmt.Println("Json Unmarshal failed with: ", err) 83 | } 84 | 85 | for i := 0; i < len(netboxDeviceList.Results); i++ { 86 | if _, device := deviceMap[netboxDeviceList.Results[i].Name]; !device { 87 | deviceMap[netboxDeviceList.Results[i].Name] = true 88 | } 89 | } 90 | 91 | // Query VMs 92 | req, err = http.NewRequest(http.MethodGet, vmURL, nil) 93 | if err != nil { 94 | panic(err) 95 | } 96 | 97 | req.Header.Set("Authorization", fmt.Sprintf("Token %s", *netboxAPIToken)) 98 | 99 | res, err = netboxClient.Do(req) 100 | if err != nil { 101 | panic(err) 102 | } 103 | 104 | body, err = ioutil.ReadAll(res.Body) 105 | defer res.Body.Close() 106 | if err != nil { 107 | panic(err) 108 | } 109 | 110 | var netboxVMList NetboxResult 111 | 112 | bodyErr = json.Unmarshal(body, &netboxVMList) 113 | 114 | if bodyErr != nil { 115 | fmt.Println("Json Unmarshal failed with: ", err) 116 | } 117 | 118 | for i := 0; i < len(netboxVMList.Results); i++ { 119 | if _, device := deviceMap[netboxVMList.Results[i].Name]; !device { 120 | deviceMap[netboxVMList.Results[i].Name] = true 121 | } 122 | } 123 | 124 | return (deviceMap) 125 | } 126 | --------------------------------------------------------------------------------