Created
September 10, 2017 00:32
-
-
Save danesparza/1cf986cda40085c8a6d4d6cc141daa16 to your computer and use it in GitHub Desktop.
Find hostname information and format for change
This file contains 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" | |
"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