此jail.conf文件。我想用sed更改[maxretry = 6]到maxretry = 5来制作人偶文件。

[ssh]

enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 6

[dropbear]

enabled  = false
port     = ssh
filter   = sshd
logpath  = /var/log/dropbear
maxretry = 6


我能怎么做?

我的木偶命令。
exec {“ fail2banadd”:
  命令=>'/ bin / sed -i“ / [ssh] /,/ [dropbear] / s / maxretry = 6 / maxretry = 5 /” /etc/fail2ban/jail.conf',
}

我得到这个错误。

=>默认值:错误:/ Stage [main] // Exec [fail2banadd] /返回:从未运行更改为0失败:/ bin / sed
-i“ / [ssh] /,/ [dropbear] / s / maxretry = 6 / maxretry = 5 /” /etc/fail2ban/jail.conf返回2 inste
/tmp/vagrant-puppet-3/manifests/init.pp中的[0]之一的d:76

最佳答案

版本1

sed解决方案仅更改[ssh][dropbear]之间的maxretry值:

$ sed  -i"" '/\[ssh\]/,/\[dropbear\]/ s/maxretry = 6/maxretry = 5/' jail.conf


输出如下:

[ssh]

enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 5

[dropbear]

enabled  = false
port     = ssh
filter   = sshd
logpath  = /var/log/dropbear
maxretry = 6


版本2

此变体删除了maxretry[ssh]之间的[dropbear]条目,并将所有其他maxretry = 6条目更改为maxretry = 5

sed  -i"" 's/maxretry = 6/maxretry = 5/; /\[ssh\]/,/\[dropbear\]/ {/maxretry/d}' jail.conf

关于linux - 如何在sed的木偶文件[block]之间更改内容?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24988242/

10-10 22:39