Compare commits

..

No commits in common. "master" and "JumpBoolOption" have entirely different histories.

7 changed files with 48 additions and 55 deletions

View File

@ -22,4 +22,4 @@ For Tab_Magic to do proper host detection, The following TXT record options are
* SSH_PORT - SSH Port will allow you to specify a custom SSH Port for the remote host. Valid: 1-65536 * SSH_PORT - SSH Port will allow you to specify a custom SSH Port for the remote host. Valid: 1-65536
* OS_FAMILY - Can be either "ESXi", "Ubiquiti", or "Windows". If ESXi, it will log in as root with no sudo opts. If Ubiquiti, it will not use sudo. If Windows, it will use the rdesktop command line tool. * OS_FAMILY - Can be either "ESXi", "Ubiquiti", or "Windows". If ESXi, it will log in as root with no sudo opts. If Ubiquiti, it will not use sudo. If Windows, it will use the rdesktop command line tool.
* REMOTE_USER - You will be able to specify a custom username for the remote host. * REMOTE_USER - This is not implemented yet, but you will be able to specify a custom username for the remote host.

View File

@ -10,25 +10,29 @@ import (
) )
func main() { func main() {
var c = flag.String("config", "", "Configuration to load") var c string
var user = flag.String("user", "", "user for aliases") var user string
var debug = flag.Bool("debug", false, "Enables Debugging Mode") var debug bool
flag.StringVar(&c, "config", "", "Configuration to load")
flag.StringVar(&user, "user", "", "user for aliases")
flag.BoolVar(&debug, "debug", false, "Enables Debugging Mode")
flag.Parse() flag.Parse()
// Stop the app if they're missing required flags. // Stop the app if they're missing required flags.
if *c == "" { if c == "" {
log.Fatal("You need to specify a configuration file.") log.Fatal("You need to specify a configuration file.")
} }
if *user == "" { if user == "" {
log.Fatal("You must specify a username.") log.Fatal("Username is not specified.")
} }
cfg, err := config.GetConfig(*c, *debug) cfg, err := config.GetConfig(c, debug)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
data := resolver.PerformZoneTransfer(cfg, *debug) data := resolver.PerformZoneTransfer(cfg)
shell.CreateShellAliases(data, *user, cfg, *debug) shell.CreateShellAliases(data, user, cfg)
} }

View File

@ -4,6 +4,6 @@
"domains": [ "kc.linuxrocker.com"], "domains": [ "kc.linuxrocker.com"],
"jumpHost": "jump01.kc.linuxrocker.com", "jumpHost": "jump01.kc.linuxrocker.com",
"useJump": false, "useJump": false,
"splitString": ".linuxrocker.com", "splitString": ".linuxrocker",
"windowsGeometry": "1600x900" "windowsGeometry": "1600x900"
} }

View File

@ -1,13 +1,11 @@
package model package model
// Results is for DNS results
type Results struct { type Results struct {
IP string `json:"ip"` IP string `json:"ip"`
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
TXT string `json:"TXT"` TXT string `json:"TXT`
} }
// UniqResults is for Unique DNS Results
type UniqResults struct { type UniqResults struct {
IP string `json:"ip"` IP string `json:"ip"`
Hostname string `json:"hostname"` Hostname string `json:"hostname"`

View File

@ -1,22 +1,19 @@
package resolver package resolver
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"log" "log"
"strings" "strings"
"git.linuxrocker.com/mattburchett/go_tab-magic/pkg/utils"
"git.linuxrocker.com/mattburchett/go_tab-magic/pkg/config" "git.linuxrocker.com/mattburchett/go_tab-magic/pkg/config"
"git.linuxrocker.com/mattburchett/go_tab-magic/pkg/model"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
// LookupName returns IPv4 address from A record or error. // LookupName returns IPv4 address from A record or error.
func lookupName(fqdn, serverAddr string, debug bool) (string, error) { func lookupName(fqdn, serverAddr string) (string, error) {
if debug {
defer utils.LogElapsedTime("Lookup Name")
}
m := &dns.Msg{} m := &dns.Msg{}
m.SetQuestion(dns.Fqdn(fqdn), dns.TypeA) m.SetQuestion(dns.Fqdn(fqdn), dns.TypeA)
in, err := dns.Exchange(m, serverAddr) in, err := dns.Exchange(m, serverAddr)
@ -33,11 +30,7 @@ func lookupName(fqdn, serverAddr string, debug bool) (string, error) {
return "", errors.New("no A record returned") return "", errors.New("no A record returned")
} }
// PerformZoneTransfer performs zone transfers and gathers a list from config.Domains func PerformZoneTransfer(config config.Config) []string {
func PerformZoneTransfer(config config.Config, debug bool) []string {
if debug {
defer utils.LogElapsedTime("Perform Zone Transfer")
}
data := make([]string, 0) data := make([]string, 0)
// Do the transfer // Do the transfer
@ -57,7 +50,7 @@ func PerformZoneTransfer(config config.Config, debug bool) []string {
case *dns.TXT: case *dns.TXT:
txt = string(v.Txt[0]) txt = string(v.Txt[0])
hostname = v.Hdr.Name hostname = v.Hdr.Name
cip, err := lookupName(strings.TrimRight(v.Hdr.Name, "."), server, debug) cip, err := lookupName(strings.TrimRight(v.Hdr.Name, "."), server)
if err != nil || cip == "" { if err != nil || cip == "" {
continue continue
} }
@ -66,7 +59,7 @@ func PerformZoneTransfer(config config.Config, debug bool) []string {
ip = v.A.String() ip = v.A.String()
hostname = v.Hdr.Name hostname = v.Hdr.Name
case *dns.CNAME: case *dns.CNAME:
cip, err := lookupName(v.Target, server, debug) cip, err := lookupName(v.Target, server)
if err != nil || cip == "" { if err != nil || cip == "" {
continue continue
} }
@ -79,6 +72,26 @@ func PerformZoneTransfer(config config.Config, debug bool) []string {
} }
} }
} }
// fmt.Println(data)
// resultsToJSON(data)
return data return data
} }
func resultsToJSON(data []string) {
jsonData := make([][]byte, 0)
for _, i := range data {
splitStrings := strings.Split(i, " ")
hostname := splitStrings[0]
ip := splitStrings[1]
txt := splitStrings[2]
dns := &model.Results{IP: ip, Hostname: hostname, TXT: txt}
b, err := json.Marshal(dns)
if err != nil {
fmt.Println(err)
}
jsonData = append(jsonData, b)
}
}

View File

@ -4,16 +4,10 @@ import (
"fmt" "fmt"
"strings" "strings"
"git.linuxrocker.com/mattburchett/go_tab-magic/pkg/utils"
"git.linuxrocker.com/mattburchett/go_tab-magic/pkg/config" "git.linuxrocker.com/mattburchett/go_tab-magic/pkg/config"
) )
// CreateShellAliases will create shell aliases and fmt.Println them func CreateShellAliases(data []string, username string, config config.Config) {
func CreateShellAliases(data []string, username string, config config.Config, debug bool) {
if debug {
defer utils.LogElapsedTime("Create Shell Aliases")
}
for _, i := range data { for _, i := range data {
splitStrings := strings.Split(i, " ") splitStrings := strings.Split(i, " ")
hostname := splitStrings[0] hostname := splitStrings[0]
@ -32,7 +26,7 @@ func CreateShellAliases(data []string, username string, config config.Config, de
prerac := "" prerac := ""
windowsGeometry := config.WindowsGeometry windowsGeometry := config.WindowsGeometry
host := strings.TrimSuffix(hostname, stringSplit) host := strings.TrimRight(hostname, stringSplit)
fqdn := hostname fqdn := hostname
greentext := "tput -T xterm setaf 2; " greentext := "tput -T xterm setaf 2; "
@ -46,7 +40,7 @@ func CreateShellAliases(data []string, username string, config config.Config, de
for _, i := range txtSplit { for _, i := range txtSplit {
if strings.Contains(i, "SSH_PORT") { if strings.Contains(i, "SSH_PORT") {
port := strings.TrimPrefix(i, "SSH_PORT=") port := strings.TrimLeft(i, "SSH_PORT=")
racOpts = fmt.Sprintf("-AXt -p %v -l", port) racOpts = fmt.Sprintf("-AXt -p %v -l", port)
} else if strings.Contains(i, "OS_FAMILY") { } else if strings.Contains(i, "OS_FAMILY") {
osFamily := strings.Split(i, "=") osFamily := strings.Split(i, "=")
@ -64,13 +58,12 @@ func CreateShellAliases(data []string, username string, config config.Config, de
sudo = "" sudo = ""
} }
} else if strings.Contains(i, "REMOTE_USER") { } else if strings.Contains(i, "REMOTE_USER") {
user := strings.TrimPrefix(i, "REMOTE_USER=") user := strings.TrimLeft(i, "REMOTE_USER=")
remoteUser = user remoteUser = user
} }
} }
if debug {
fmt.Println("Disabled Output due to Debug Mode") if useJump {
} else if useJump {
fmt.Printf("alias %v=\\'%v%v%v %v@%v \"%v %v %v %v %v\"'\n", host, message, prerac, hop, username, jump, rac, racOpts, remoteUser, fqdn, sudo) fmt.Printf("alias %v=\\'%v%v%v %v@%v \"%v %v %v %v %v\"'\n", host, message, prerac, hop, username, jump, rac, racOpts, remoteUser, fqdn, sudo)
} else { } else {
fmt.Printf("alias %v=\\'%v %v %v %v %v %v\n", host, message, rac, racOpts, remoteUser, fqdn, sudo) fmt.Printf("alias %v=\\'%v %v %v %v %v %v\n", host, message, rac, racOpts, remoteUser, fqdn, sudo)

View File

@ -1,15 +0,0 @@
package utils
import (
"log"
"time"
)
//LogElapsedTime provides a function that can be used to log the time elapsed between when this function is invoked, to the time the returned function is invoked.
//Adding "defer LogElapsedTime("my log")" at the beginning of any function will log the time it takes to execute that function.
func LogElapsedTime(what string) func() {
start := time.Now()
return func() {
log.Printf("%s, Total Time Taken: %v\n", what, time.Since(start))
}
}