doulun1915
2018-10-15 11:46
浏览 51
已采纳

如何创建带有接口名称的特定部分的文件,然后再从另一个文件中进行解析以使用GO中的数据修改接口?

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)
    }
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushang1880 2018-10-22 14:46
    已采纳

    I was able to carry this out by converting the command line input

    ./program --xyz.yaml --{interface} eth0 --IPaddress 192.168.1.233/24 --{gateway} 192.168.0.1 --DNS 8.8.3.8,8.8.8.3

    I used a mapping function to map the CLI to strings with this

    func manyRandomArg() map[string]string {
        rv := make(map[string]string)
        for ix, x := range os.Args {
            if x[:2] == "--" {
                rv[x] = os.Args[ix+1]
            }
        }
        return rv
    }
    

    Then I sorted only the keys and carried out the Search Replace function

    keys := make([]string, 0, len(rv))
        for key := range rv {
            keys = append(keys, key)
        }
        sort.Strings(keys) //sort keys alphabetically
    

    Had to trim the '--' part in the key

    key0 = trimLeftChars(keys[0], 2)
    

    The string replace function used was

    r := strings.NewReplacer(key0, rv[keys[0]], key1, rv[keys[1]], key2, rv[keys[2]], key3, rv[keys[3]], key4, rv[keys[4]])
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题