Skip to content

Commit

Permalink
fix(cli): Resolve FQDN on Linux machines with short hostnames (#600)
Browse files Browse the repository at this point in the history
If os.Hostname() returns a short hostname instead of a FQDN, lookup the
FQDN using net.LookupHost() and net.LookupAddr()

Resolves #189

Co-authored-by: Ganesh Raikhelkar <[email protected]>
  • Loading branch information
webD97 and graikhel-intel authored Aug 6, 2024
1 parent 69319c9 commit a2df614
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions internal/amt/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,42 @@
package amt

import (
"net"
"os"
"strings"
)

func (amt AMTCommand) GetOSDNSSuffix() (string, error) {
hostname, err := os.Hostname()
fqdn, err := getFQDN()
if err != nil {
return "", err
}
splitName := strings.SplitAfterN(hostname, ".", 2)
splitName := strings.SplitAfterN(fqdn, ".", 2)
if len(splitName) == 2 {
return splitName[1], nil
}
return hostname, err
return fqdn, err
}

func getFQDN() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}

if strings.Contains(hostname, ".") {
return hostname, nil
}

addrs, err := net.LookupHost(hostname)
if err != nil {
return "", err
}

names, err := net.LookupAddr(addrs[0])
if err != nil {
return "", err
}

return strings.TrimSuffix(names[0], "."), nil
}

0 comments on commit a2df614

Please sign in to comment.