Last active
April 9, 2016 20:37
-
-
Save mikemackintosh/c42c297b0df56db3d58b71a7cd625fe6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "os/exec" | |
| "strings" | |
| ) | |
| var attributes deviceAttributes | |
| // deviceAttributes holds | |
| type deviceAttributes struct { | |
| DeviceType string | |
| SerialNumber string | |
| UserName string | |
| HostName string | |
| EthernetAddress []string | |
| } | |
| // main function, executes all teh code | |
| func main() { | |
| // Query for the attribute data | |
| attributes.DeviceType = extractDeviceType() | |
| attributes.SerialNumber = extractSerialNumber() | |
| attributes.UserName = extractUserName() | |
| attributes.HostName = extractHostName() | |
| attributes.EthernetAddress = extractEthernetAddress() | |
| fmt.Println("#########################################") | |
| fmt.Println("# #") | |
| fmt.Println("# DeviceAttributes by \033[38;5;154m@mikemackintosh\033[0m #") | |
| fmt.Println("# #") | |
| fmt.Println("#########################################") | |
| fmt.Println("\n\nFound The following device type") | |
| fmt.Println("================================") | |
| fmt.Printf("\t\033[38;5;32m%s\033[0m\n", attributes.DeviceType) | |
| fmt.Println("\n\nFound The following Serial Number") | |
| fmt.Println("================================") | |
| fmt.Printf("\t\033[38;5;32m%s\033[0m\n", attributes.SerialNumber) | |
| fmt.Println("\n\nFound The following Username") | |
| fmt.Println("================================") | |
| fmt.Printf("\t\033[38;5;32m%s\033[0m\n", attributes.UserName) | |
| fmt.Println("\n\nFound The following HostName") | |
| fmt.Println("================================") | |
| fmt.Printf("\t\033[38;5;32m%s\033[0m\n", attributes.HostName) | |
| fmt.Println("\n\nFound The following Ethernet Addresses") | |
| fmt.Println("================================") | |
| for _, address := range attributes.EthernetAddress { | |
| fmt.Printf("\t\033[38;5;32m%s\033[0m\n", address) | |
| } | |
| fmt.Println() | |
| } | |
| // GetCommandOutput runs a command and returns it's output | |
| func GetCommandOutput(cmd string) (string, error) { | |
| out, err := exec.Command("bash", "-c", cmd).Output() | |
| if err != nil { | |
| return "", err | |
| } | |
| return strings.TrimSpace(string(out)), nil | |
| } | |
| // extractDeviceType gets the real type of endpoint | |
| // by extracting data out of sysctl and system hackery | |
| func extractDeviceType() string { | |
| // Get the shorthand device type from sysctl | |
| cmd := "sysctl hw.model | cut -d\":\" -f 2 | awk '$1=$1'" | |
| model, err := GetCommandOutput(cmd) | |
| if err != nil { | |
| log.Printf("--> [ERR] Meta: Unable to obtain device model: %s", err) | |
| return "N/A" | |
| } | |
| // Parse the .plist to find the human readable format of the device | |
| cmd = fmt.Sprintf("/usr/libexec/PlistBuddy -c \"Print %s:_LOCALIZABLE_:marketingModel\" /System/Library/PrivateFrameworks/ServerInformation.framework/Versions/A/Resources/English.lproj/SIMachineAttributes.plist", model) | |
| parsedModel, err := GetCommandOutput(cmd) | |
| if err != nil { | |
| log.Printf("--> [ERR] Meta: Unable to obtain marketing model name: %s", err) | |
| return model | |
| } | |
| // Set the device model | |
| return parsedModel | |
| } | |
| // extractSerialNumber gets the mfr serial number of the endpoint | |
| func extractSerialNumber() string { | |
| // This command is the quickest way to get the serial number of a device | |
| cmd := "/usr/sbin/ioreg -c IOPlatformExpertDevice | /usr/bin/head -30 | /usr/bin/grep IOPlatformSerialNumber | /usr/bin/awk '{print $4}'" | |
| serial, err := GetCommandOutput(cmd) | |
| // If there was an error, we should report | |
| if err != nil { | |
| fmt.Printf("--> [ERR] Meta: Unable to obtain system serial: %s", err) | |
| return "ERR" | |
| } | |
| // If we don't have a serial, return N/A | |
| if serial == "" { | |
| return "N/A" | |
| } | |
| // Return with a quote-stripped serial | |
| return strings.Replace(string(serial), "\"", "", 2) | |
| } | |
| // ExtractUserName gets the primary system user name | |
| func extractUserName() string { | |
| // using system_profiler we can get the user name | |
| cmd := "system_profiler SPSoftwareDataType | grep \"User Name\" | cut -d: -f2 | cut -d'(' -f1" | |
| username, err := GetCommandOutput(cmd) | |
| // If there was an error, we should report | |
| if err != nil { | |
| fmt.Printf("--> [ERR] Meta: Unable to obtain system user name: %s", err) | |
| return "ERR" | |
| } | |
| // If we don't have a username, return N/A | |
| if username == "" { | |
| return "N/A" | |
| } | |
| return string(username) | |
| } | |
| // extractHostName gets the system hostname from system_profiler | |
| func extractHostName() string { | |
| // using system_profiler we can get the device name | |
| cmd := "system_profiler SPSoftwareDataType | grep \"Computer Name\" | cut -d: -f2" | |
| hostname, err := GetCommandOutput(cmd) | |
| // If there was an error, we should report | |
| if err != nil { | |
| fmt.Printf("ERROR: Unable to obtain system hostname: %s", err) | |
| return "ERR" | |
| } | |
| // If we don't have a hostname, return N/A | |
| if hostname == "" { | |
| return "N/A" | |
| } | |
| return string(hostname) | |
| } | |
| // extractEthernetAddress will obtain all the MAC addresses from | |
| // physical interfaces on the endpoint | |
| func extractEthernetAddress() []string { | |
| // using networksetup we can list all the hardware interfaces and MAC addresses | |
| cmd := "networksetup -listallhardwareports | grep Ethernet | awk '{print $3}'" | |
| output, err := GetCommandOutput(cmd) | |
| // If there was an error, we should report | |
| if err != nil { | |
| fmt.Printf("--> [ERR] Meta: Unable to obtain system ethernet addresses: %s", err) | |
| return []string{"ERR"} | |
| } | |
| // If we don't have a username, return N/A | |
| if output == "" { | |
| return []string{"N/A"} | |
| } | |
| return strings.Split(output,"\n") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment