I am building a program to change IP address of the machine in Ubuntu 18.04.1 LTS. So basically I am creating a YAML file template with
network:
ethernets:
{INTERFACE}:
dhcp4: no
addresses: [{IP}/24]
gateway4: {GATEWAY}
optional: true
nameservers:
addresses: [{DNS}]
{INTERFACE2}:
dhcp4: no
addresses: [{IP2}/24]
gateway4: {GATEWAY2}
optional: true
nameservers:
addresses: [{DNS2}]
Now I need to scan this template file and add the values from an another file either through a JSON call or as parameters in CLI.
So the app will take a template file that has been pre-prepared. Such as the example template for yaml file.
It will take 3 parameters this template file, a translation file(JSON), and the output
the translation file could be parsed on the commandline also
It is based on 2 columns, the search variable such as IP and its new value
The utility will search for the {IP}
variable and replace it with the second column value.
On the CLI it might look like this:
./searchreplace -intemplate='templateip.yaml' -translation='{IP},xxx.x.x.x; {SUBNET},yyy.yyy.yyy.y} -outfile=/tmp/newipfile.yaml
My current code hard codes the data and changes the IP but I want it to be in a separate template file. This is my current code
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"os/exec"
"regexp"
"strings"
)
var pathdelete = "/etc/netplan/01-network-manager-all.yaml"
var ipv4Regex = regexp.MustCompile(`^(?:[0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]+$`)
var gatewayRegex = regexp.MustCompile(`^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`)
var nsRegex = regexp.MustCompile(`^(?:[0-9]\.){3}[0-9]{1,3}\,(?:[0-9]\.){3}[0-9]{1,3}$`)
var nicRegex = regexp.MustCompile(`e([a-z]+)`)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter the IP address: ")
IPaddress, _ := reader.ReadString('
')
IPaddress = strings.TrimSuffix(IPaddress, "
")
//sanity check
testInput := net.ParseIP(IPaddress)
if testInput.To4() == nil {
fmt.Printf("%v is not a valid IPv4 address
", testInput)
} else {
fmt.Printf("It is a valid IP address
")
writeFile()
readFile()
}
}
func writeFile() {
err01 := os.Truncate(pathdelete, 0)
if err01 != nil {
log.Fatal(err01)
}
//reading Network Cards
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter the Ethernet Card (for example : 'ens0N' or 'eth0') ")
nic, _ := reader.ReadString('
')
nic = strings.TrimSuffix(nic, "
")
if nicRegex.MatchString(nic) {
} else {
log.Fatal("Please check the name of the NIC Card you have entered")
os.Exit(1)
}
//reading IP Address and DNS
fmt.Print("Enter the IP address along with the DNS in the format xxx.xxx.x.xxx/yy ")
IP, _ := reader.ReadString('
')
IP = strings.TrimSuffix(IP, "
")
if ipv4Regex.MatchString(IP) {
} else {
log.Fatal("Please check the IP address you have entered")
os.Exit(1)
}
//reading Gateway
fmt.Print("Enter the gateway in the format xxx.xxx.x.x ")
gateway, _ := reader.ReadString('
')
gateway = strings.TrimSuffix(gateway, "
")
if gatewayRegex.MatchString(gateway) {
} else {
log.Fatal("Please check the Gateway you have entered")
os.Exit(1)
}
//reading Name servers
fmt.Print("Enter the nameservers seperated by comma(,) in the format x.x.x.x,y.y.y.y ")
ns, _ := reader.ReadString('
')
ns = strings.TrimSuffix(ns, "
")
if nsRegex.MatchString(ns) {
} else {
log.Fatal("Please check the Name Servers you have entered")
os.Exit(1)
}
// open file using READ & WRITE permission
var file, err = os.OpenFile(pathdelete, os.O_RDWR, 0644)
checkError(err)
defer file.Close()
// write some text to file
_, err = file.WriteString("# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
")
if err != nil {
fmt.Println(err.Error())
return //must return here for defer statements to be called
}
_, err = file.WriteString(" ethernets:
" + nic + ":
dhcp4: no
dhcp6: no
")
if err != nil {
fmt.Println(err.Error())
return //same as above
}
_, err = file.WriteString(" addresses: [" + IP + "]
gateway4: " + gateway + "
nameservers:
addresses: [" + ns + "]")
if err != nil {
fmt.Println(err.Error())
return //same as above
}
// save changes
err = file.Sync()
if err != nil {
fmt.Println(err.Error())
return //same as above
}
applyCmd := exec.Command("bash", "-c", "sudo netplan apply")
_, err2 := applyCmd.Output()
if err2 != nil {
panic(err2)
}
}
func readFile() {
// re-open file
var file, err = os.OpenFile(pathdelete, os.O_RDWR, 0644)
checkError(err)
defer file.Close()
// read file
var text = make([]byte, 1024)
n, err := file.Read(text)
if n > 0 {
fmt.Println("
The data you have entered is
")
fmt.Println(string(text))
}
//if there is an error while reading
//just print however much was read if any
//at return file will be closed
}
func checkError(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}