使用脚本和 sed 更改 DHCP 租约表

使用脚本和 sed 更改 DHCP 租约表

亲爱的,

我在 dhcp 租约文件中有租约,如以下示例所示:

lease 192.168.79.15 {
  starts 0 2016/08/21 13:25:45;
  ends 0 2016/08/21 19:25:45;
  cltt 0 2016/08/21 13:25:45;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 8c:1a:bf:11:00:ea;
  uid "\001\214\032\277\021\000\352";
  option agent.circuit-id 0:17;
  option agent.remote-id c0:a8:9:5;
  client-hostname “android-8182e21c852776e7";

我需要能够在使用 将此文件发送到其他地方之前将选项 agent.remote-id 固定值更改为字符串,并且如果可能的话,还要将 circuit-id 字段从十六进制转换为二进制。想到要做的第一件事,我尝试制作如下脚本,但它不起作用:

#!/bin/bash

VAR1=“/var/lib/dhcp/dhcpd.leases”
VAR2=“c0:a8:9:2”
VAR3=“c0:a8:9:3”
VAR4=“c0:a8:9:4”
VAR5=“c0:a8:9:5”
VAR6=“c0:a8:9:6”
VAR7=“c0:a8:9:7”


sed -e ’s/${VAR2}/AOD-ADMIN-OFFICE/g; s/${VAR3}/GOLF-MAINTENANCE-BUILDING/g; s/${VAR4}/MARINA-DRY-STACK/g;s/${VAR5}/DC-SERVICE-ROOM/g;s/${VAR6}/ACPM-ADMIN-OFFICE/g;s/${VAR7}/SEA-WATER-OFFICE/g’ ${VAR1}    

答案1

第一个问题是变量赋值中的字符串使用了非 ASCII 引号字符。因此,sed尝试匹配“c0:a8:9:2”而不是c0:a8:9:2

除此之外,我真的不明白为此定义一大堆变量有什么意义;在我看来,使用这里的文件例如

sed -f- /var/lib/dhcp/dhcpd.leases << 'EOF'
s/c0:a8:9:2/AOD-ADMIN-OFFICE/
s/c0:a8:9:3/GOLF-MAINTENANCE-BUILDING/
s/c0:a8:9:4/MARINA-DRY-STACK/
s/c0:a8:9:5/DC-SERVICE-ROOM/
s/c0:a8:9:6/ACPM-ADMIN-OFFICE/
s/c0:a8:9:7/SEA-WATER-OFFICE/
EOF

您可以将s/c0:a8:9:2/AOD-ADMIN-OFFICE/行放在单独的命令文件中,然后使用sed -f cmdfile /var/lib/dhcp/dhcpd.leasesif 来调用它,这样可以使表更易于维护,即

cmdfile(或您选择的任何名称):

# list of agent.remote-id to physical location mappings
# for use in DHCP lease file processing script

s/c0:a8:9:2/AOD-ADMIN-OFFICE/
s/c0:a8:9:3/GOLF-MAINTENANCE-BUILDING/
s/c0:a8:9:4/MARINA-DRY-STACK/
s/c0:a8:9:5/DC-SERVICE-ROOM/
s/c0:a8:9:6/ACPM-ADMIN-OFFICE/
s/c0:a8:9:7/SEA-WATER-OFFICE/

然后在你的脚本中

sed -f cmdfile /var/lib/dhcp/dhcpd.leases

相关内容