2

我正在尝试创建一个脚本来替换01-network-manager-all.yaml文件中给定网络接口的参数。

该文件看起来像这样:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp0s3:
      dhcp4: no
      dhcp6: no
      addresses: [195.10.10.10/24]
      gateway4: 195.10.10.10
      nameservers:
        addresses: [1.1.1.1]
    enp0s8:
      dhcp4: no
      dhcp6: no
      addresses: [196.10.10.10/24]
      gateway4: 196.10.10.10
      nameservers:
        addresses: [1.1.1.1]
    enp0s9:
      dhcp4: no
      dhcp6: no
      addresses: [197.10.10.10/24]
      gateway4: 197.10.10.10
      nameservers:
        addresses: [1.1.1.1]

假设我想替换有关的行enp0s8并让其他行保持原样。

输出看起来像这样:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp0s3:
      dhcp4: no
      dhcp6: no
      addresses: [195.10.10.10/24]
      gateway4: 195.10.10.10
      nameservers:
        addresses: [1.1.1.1]
    enp0s8:
      dhcp4: no
      dhcp6: no
      addresses: [196.250.250.125/24]
      gateway4: 196.250.250.125
      nameservers:
        addresses: [8.8.8.8]
    enp0s9:
      dhcp4: no
      dhcp6: no
      addresses: [197.10.10.10/24]
      gateway4: 197.10.10.10
      nameservers:
        addresses: [1.1.1.1]

在这种情况下,我正在寻找一个sed- 或任何其他命令 - 将替换 and 之间的所有enp0s8内容enp0s9

我不是一个非常有经验的 Bash 用户,任何提示将不胜感激。

我这样做的糟糕尝试

sed -e '/^ *enp0s8:/,/^ *^enp0s9:/s/$CONFIG_TO_ADD/' "/etc/netplan/01-network-manager-all.yaml"
4

4 回答 4

3

我会使用理解 YAML 而不是纯文本操作的工具。例如,红宝石:

$ ruby -e '
    require "yaml"
    data = YAML.safe_load_file(ARGV[0])
    net = data["network"]["ethernets"]["enp0s8"]
    net["addresses"] = ["196.250.250.125/24"]
    net["gateway4"] = "196.250.250.125"
    net["nameservers"]["addresses"] = ["8.8.8.8"]
    puts data.to_yaml
' file.yml
---
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp0s3:
      dhcp4: false
      dhcp6: false
      addresses:
      - 195.10.10.10/24
      gateway4: 195.10.10.10
      nameservers:
        addresses:
        - 1.1.1.1
    enp0s8:
      dhcp4: false
      dhcp6: false
      addresses:
      - 196.250.250.125/24
      gateway4: 196.250.250.125
      nameservers:
        addresses:
        - 8.8.8.8
    enp0s9:
      dhcp4: false
      dhcp6: false
      addresses:
      - 197.10.10.10/24
      gateway4: 197.10.10.10
      nameservers:
        addresses:
        - 1.1.1.1
于 2021-07-05T14:27:12.073 回答
2

使用 yq :

yq eval '.network.ethernets.enp0s8 |= (.addresses = ["196.250.250.125/24"] |
                                       .gateway4  = "196.250.250.125" |
                                       .nameservers.addresses = ["8.8.8.8"]
                                      )' file.yml
于 2021-07-05T16:26:36.173 回答
2
$ cat new
      dhcp4: no
      dhcp6: no
      addresses: [196.250.250.125/24]
      gateway4: 196.250.250.125
      nameservers:
        addresses: [8.8.8.8]

$ awk '
    NR==FNR { new=new ORS $0; next }
    /^    [^ ]/ { if ( f=($1=="enp0s8:") ) print $0 new }
    !f
' new file
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp0s3:
      dhcp4: no
      dhcp6: no
      addresses: [195.10.10.10/24]
      gateway4: 195.10.10.10
      nameservers:
        addresses: [1.1.1.1]
    enp0s8:
      dhcp4: no
      dhcp6: no
      addresses: [196.250.250.125/24]
      gateway4: 196.250.250.125
      nameservers:
        addresses: [8.8.8.8]
    enp0s9:
      dhcp4: no
      dhcp6: no
      addresses: [197.10.10.10/24]
      gateway4: 197.10.10.10
      nameservers:
        addresses: [1.1.1.1]
于 2021-07-05T14:35:01.090 回答
1

以防万一有人感兴趣。

ed如果可用/可接受,请使用。

#!/bin/sh

ethernets=enp0s8
gateway=196.250.250.125
address=196.250.250.125/24
name_server_address=8.8.8.8

ed -s file.yaml <<-EOF
  g/$ethernets/;/nameservers/s/^\\([[:blank:]]*gateway4:[^ ]*\\).*/\\1 $gateway/\\
  -1s|^\\([[:blank:]]*addresses: \[\\).*\\(\]\\)$|\\1$address\\2|
  g/$ethernets/;/nameservers:/;/addresses:/s|^\\([[:blank:]]*addresses: \\[\\).*\\(\\]\\)$|\\1$name_server_address\\2|
  ,p
  Q
EOF

  • 更改是否应在其他部分/块进行更改的最后一位数字,该部分/块现在enp0s指向。enp0s8

  • 不是yaml解析器/编辑器,也不像其他解决方案那样健壮,但它可以完成这项工作。

于 2021-07-05T17:17:58.790 回答