Skip to content

Instantly share code, notes, and snippets.

@danesparza
Created September 10, 2017 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danesparza/1cf986cda40085c8a6d4d6cc141daa16 to your computer and use it in GitHub Desktop.
Save danesparza/1cf986cda40085c8a6d4d6cc141daa16 to your computer and use it in GitHub Desktop.
Find hostname information and format for change
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
// Get the hostname
name, err := os.Hostname()
if err != nil {
panic(err)
}
// Display the current hostname:
fmt.Printf("Current hostname: %v\n", name)
// Get hostname file information:
hostname, err := ioutil.ReadFile("/etc/hostname")
if err != nil {
panic(err)
}
// Replace the contents with the new hostname:
newhostname := strings.Replace(string(hostname[:]), "cerdo", "appliancemonitor-28e84f", -1)
fmt.Printf("New hostname information: \n%v\n\n", newhostname)
// Get hosts file information:
hosts, err := ioutil.ReadFile("/etc/hosts")
if err != nil {
panic(err)
}
// Replace the contents with the new hostname:
newhosts := strings.Replace(string(hosts[:]), "cerdo", "appliancemonitor-28e84f", -1)
fmt.Printf("New hosts information: \n%v\n\n", newhosts)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment