宜蘭縣教育支援平台 會員登入 會員註冊 我的i教書

« 上一篇 | 下一篇 »

從 /var/lib/dhcpd/dhcpd.leases 取出要的網路卡卡號
# cat /var/lib/dhcpd/dhcpd.leases | grep hard | awk '{print $3}'
d0:df:c7:f9:5c:ef;
00:26:51:0d:53:40;
00:11:93:88:7a:00;
00:15:58:6b:7d:da;
00:15:58:6b:7d:da;
00:15:58:6b:7d:da;
00:15:58:6b:7d:da;

經過處理,只取出想要的部分
# cat /var/lib/dhcpd/dhcpd.leases | grep hard | awk '{print $3}' | sed 's/.$//' | sort | uniq
00:11:93:88:7a:00
00:15:58:6b:7d:da
00:26:51:0d:53:40
d0:df:c7:f9:5c:ef

整理完的資料檔
# cat /root/mac
server1,00:11:93:88:7a:00,192.168.1.11
server2,00:15:58:6b:7d:da,192.168.1.12
server3,00:26:51:0d:53:40,192.168.1.13
server4,d0:df:c7:f9:5c:ef,192.168.1.14


使用 awk
# cat /usr/local/bin/mkdhcpd
#!/bin/bash
cat /root/mac | awk -F"," 'BEGIN{OFS=" "}{print $1,$2,$3 }' | while read i j k
do
   echo "host $i {"
   echo "  hardware ethernet $j;"
   echo "  fixed-address $k;"
   echo "}"
done

使用 sed
# cat /usr/local/bin/mkdhcpd
#!/bin/bash
cat /root/mac | sed 's/,/ /g' | while read i j k
do
   echo "host $i {"
   echo "  hardware ethernet $j;"
   echo "  fixed-address $k;"
   echo "}"
done

測試
# sh /usr/local/bin/mkdhcpd
host server1 {
  hardware ethernet 00:11:93:88:7a:00;
  fixed-address 192.168.1.11;
}
host server2 {
  hardware ethernet 00:15:58:6b:7d:da;
  fixed-address 192.168.1.12;
}
host server3 {
  hardware ethernet 00:26:51:0d:53:40;
  fixed-address 192.168.1.13;
}
host server4 {
  hardware ethernet d0:df:c7:f9:5c:ef;
  fixed-address 192.168.1.14;
}

如果你的網誌不支援引用網址自動搜尋功能,那麼你可以使用下列的直接引用網址來引用本文章:

http://blog.ilc.edu.tw/blog/trackback.php?id=470952