我正在构建一个程序来更改Ubuntu 18.04.1 LTS中计算机的IP地址。所以基本上我是用
创建一个YAML文件模板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}]
现在,我需要扫描此模板文件,并通过JSON调用或CLI中的参数添加另一个文件中的值。
因此,该应用将使用预先准备好的模板文件。例如yaml文件的示例模板。
需要3个参数 该模板文件,翻译文件(JSON)和输出
翻译文件也可以在命令行上解析
它基于2列,即IP等搜索变量及其新值
该实用程序将搜索{IP}
变量,并将其替换为第二列值。
在CLI上,它可能看起来像这样:
./ searchreplace -intemplate ='templateip.yaml'-translation ='{IP},xxx.x.x.x; {SUBNET},yyy.yyy.yyy.y} -outfile = / tmp / newipfile.yaml
我当前的代码对数据进行硬编码并更改IP,但我希望它位于单独的模板文件中。这是我当前的代码
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('\n')
IPaddress = strings.TrimSuffix(IPaddress, "\n")
//sanity check
testInput := net.ParseIP(IPaddress)
if testInput.To4() == nil {
fmt.Printf("%v is not a valid IPv4 address\n", testInput)
} else {
fmt.Printf("It is a valid IP address\n")
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('\n')
nic = strings.TrimSuffix(nic, "\n")
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('\n')
IP = strings.TrimSuffix(IP, "\n")
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('\n')
gateway = strings.TrimSuffix(gateway, "\n")
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('\n')
ns = strings.TrimSuffix(ns, "\n")
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\nnetwork:\n version: 2\n renderer: NetworkManager\n")
if err != nil {
fmt.Println(err.Error())
return //must return here for defer statements to be called
}
_, err = file.WriteString(" ethernets:\n " + nic + ":\n dhcp4: no\n dhcp6: no\n")
if err != nil {
fmt.Println(err.Error())
return //same as above
}
_, err = file.WriteString(" addresses: [" + IP + "]\n gateway4: " + gateway + "\n nameservers:\n 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("\nThe data you have entered is \n ")
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)
}
}
答案 0 :(得分:2)
我能够通过转换命令行输入来实现
./ 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
我使用了一个映射函数,以此将CLI映射到字符串
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
}
然后我仅对键进行排序并执行了搜索替换功能
keys := make([]string, 0, len(rv))
for key := range rv {
keys = append(keys, key)
}
sort.Strings(keys) //sort keys alphabetically
必须修剪键中的“-”部分
key0 = trimLeftChars(keys[0], 2)
使用的字符串替换功能为
r := strings.NewReplacer(key0, rv[keys[0]], key1, rv[keys[1]], key2, rv[keys[2]], key3, rv[keys[3]], key4, rv[keys[4]])