Issue
I have a bash script for which I send data to the syslog-ng logger. In my case, the problem is that the number of characters exceeds 1024 and are therefore truncated. I have managed to generate a file with several lines but it continues to send the full block.
I want to send the dhcp.leases
file to the logger and retrieve it from node-red to rework them.
logger -t "wrtdhcpleasesreport" "$(echo "$(dumpWrtPresenceDhcp)")"
#/bin/sh
WRTPRESENCE_DHCP_LEASES="/tmp/wrtpresence_dhcp.leases"
DHCP_LEASES="/tmp/dhcp.leases"
#
dumpWrtPresenceDhcp ()
{
echo "$(WrtPresenceDhcp)"
sleep 1
cat "${WRTPRESENCE_DHCP_LEASES}"
return
}
WrtPresenceDhcp ()
{
# Creating a file
touch "${WRTPRESENCE_DHCP_LEASES}"
touch "${WRTPRESENCE_DHCP_LEASES}.new"
# Prints the dhcp.leases file in 1 line separated by | in wrtpresence_dhcp.leases.new.
cat "${DHCP_LEASES}" | awk '{print $2";"$3";"$4}' | tr '\n' '|' > "${WRTPRESENCE_DHCP_LEASES}.new"
# Move the wrtpresence_dhcp.leases.new file to wrtpresence_dhcp.leases.
mv "${WRTPRESENCE_DHCP_LEASES}.new" "${WRTPRESENCE_DHCP_LEASES}"
#Print 6 characters per line to get around the 1024 character limit.
awk '(NR % 6 == 1) {print; for(i=1; i<6 && getline ; i++) { print }; printf "\n"}' RS='|' ORS='|' "${WRTPRESENCE_DHCP_LEASES}" > "${WRTPRESENCE_DHCP_LEASES}.new"
# Move the wrtpresence_dhcp.leases.new file to wrtpresence_dhcp.leases.
mv "${WRTPRESENCE_DHCP_LEASES}.new" "${WRTPRESENCE_DHCP_LEASES}"
return
}
logger -t "wrtdhcpleasesreport" "$(echo "$(dumpWrtPresenceDhcp)")"
exit 0
My 1st test.The result was cool but I hadn't noticed that it stopped at 1024 characters.
Nov 5 11:15:01 WifiAP-01 wrtdhcpleasesreport:
ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;h
The code posted here, cool I have several lines but as it sends in 1 time it does not change anything in the limit.
Nov 5 11:17:01 WifiAP-01 wrtdhcpleasesreport:
ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|
ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|
ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.1
I want to be able to do this.
Nov 5 11:17:01 WifiAP-01 wrtdhcpleasesreport: ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|
Nov 5 11:17:01 WifiAP-01 wrtdhcpleasesreport: ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|
Nov 5 11:17:01 WifiAP-01 wrtdhcpleasesreport: ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|
Nov 5 11:17:01 WifiAP-01 wrtdhcpleasesreport: ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|ab:ab:ab:ab:ab:ab;192.168.x.xxx;hostname|
I would like to send
logger -t "wrtdhcpleasesreport" "$(echo "$(dumpWrtPresenceDhcp)")" line1
logger -t "wrtdhcpleasesreport" "$(echo "$(dumpWrtPresenceDhcp)")" line2
logger -t "wrtdhcpleasesreport" "$(echo "$(dumpWrtPresenceDhcp)")" line3...
Example file /tmp/dhcp.leases
(details)
1 line equals 1 device line [0]= milliseconds(x1000) line [1]= mac address line [2]= ip line [3]= hostname
0 ab:ab:ab:ab:ab:ab 192.168.1.12 Computer-1 *
1699250507 ab:ab:ab:ab:ab:ab 192.168.1.17 Computer-2 01:ab:ab:ab:ab:ab:ab
0 ab:ab:ab:ab:ab:ab 192.168.1.123 TV-1 ab:ab:ab:ab:ab:ab
1699243309 ab:ab:ab:ab:ab:ab 192.168.1.13 Iphone *
0 be:ab:ab:ab:ab:ab 192.168.1.34 Android-Children be:ab:ab:ab:ab:ab
0 fd:ab:ab:ab:ab:ab 192.168.1.56 Computer-3 *
0 cd:ab:ab:ab:ab:ab 192.168.1.124 Computer-4 01:cd:ab:ab:ab:ab:ab
Aware of my coding limitations, I've adapted my code according to what I've read left and right. My code isn't optimum, that's for sure, but I'm learning at the same time. That's the positive side.
Testing the solution
/tmp/test
(I've matched the number of characters in my actual file)
0 00:01:00:00:00:00 192.168.1.01 one-one-one-oneo *
0 00:02:00:00:00:00 192.168.1.02 Two-two-twoo 01:00:02:00:00:00:00
0 00:03:00:00:00:00 192.168.1.003 three-three-thr 01:00:03:00:00:00:00
0 00:04:00:00:00:00 192.168.1.04 four-four-four-fou *
0 00:05:00:00:00:00 192.168.1.005 five-five-fiv 01:00:05:00:00:00:00
0 00:06:00:00:00:00 192.168.1.006 six-six-s *
0 00:07:00:00:00:00 192.168.1.07 seven-seven-sevenn *
0 00:08:00:00:00:00 192.168.1.008 eight-eight-e *
0 00:09:00:00:00:00 192.168.1.09 nine-nine-ninenin 01:00:09:00:00:00:00
0 00:10:00:00:00:00 192.168.1.10 ten-ten-ten-t 01:00:09:00:00:00:00
0 00:11:00:00:00:00 192.168.1.011 eleven-eleven 01:00:09:00:00:00:00
0 00:12:00:00:00:00 192.168.1.12 twelve-twelve-twe 01:00:09:00:00:00:00
0 00:13:00:00:00:00 192.168.1.13 thirteen-thirte *
0 00:14:00:00:00:00 192.168.1.14 fourteen-fourteen *
0 00:15:00:00:00:00 192.168.1.015 fifteen-fifteen-fi *
0 00:16:00:00:00:00 192.168.1.4 sixteen-sixteen-sixt *
0 00:17:00:00:00:00 192.168.1.17 seventeen-seven 01:00:09:00:00:00:00
0 00:18:00:00:00:00 192.168.1.018 eighteen-eig 01:00:09:00:00:00:00
0 00:19:00:00:00:00 192.168.1.019 nineteen-ninet *
0 00:20:00:00:00:00 192.168.1.020 twenty-twenty-twe 01:00:09:00:00:00:00
0 00:21:00:00:00:00 192.168.1.021 twentyone 01:00:09:00:00:00:00
0 00:22:00:00:00:00 192.168.1.22 twentytwo-twentytwo 01:00:09:00:00:00:00
0 00:23:00:00:00:00 192.168.1.6 twentythree-twentyt 01:00:09:00:00:00:00
0 00:24:00:00:00:00 192.168.1.24 twentyfour-twentyfo 01:00:09:00:00:00:00
0 00:25:00:00:00:00 192.168.1.25 twentyfiv *
0 00:26:00:00:00:00 192.168.1.26 twentysix-twenty 01:00:09:00:00:00:00
0 00:27:00:00:00:00 192.168.1.27 twentyseven-twent 01:00:09:00:00:00:00
0 00:28:00:00:00:00 192.168.1.28 twentyeigh 01:00:09:00:00:00:00
0 00:29:00:00:00:00 192.168.1.29 twentynine-twentyn 01:00:09:00:00:00:00
0 00:30:00:00:00:00 192.168.1.30 thirty-thirty-thir 01:00:09:00:00:00:00
0 00:31:00:00:00:00 192.168.1.31 thirtyone-thirty 01:00:09:00:00:00:00
1707029378 00:32:00:00:00:00 192.168.1.32 thirtytwo-t 01:00:09:00:00:00:00
0 00:33:00:00:00:00 192.168.1.33 thirtythre 01:00:09:00:00:00:00
0 00:34:00:00:00:00 192.168.1.4 thirtyfour-t 01:00:09:00:00:00:00
Command:
</tmp/test awk '{print $2,$3,$4}' OFS=';' ORS='|\r' |
tr -d '\r' | logger -t wrtdhcpleasesreport
Result:
Nov 6 12:22:58 WifiAP-01 wrtdhcpleasesreport: 00:01:00:00:00:00;192.168.1.01;one-one-one-oneo|00:02:00:00:00:00;192.168.1.02;Two-two-twoo|00:03:00:00:00:00;192.168.1.003;three-three-thr|00:04:00:00:00:00;192.168.1.04;four-four-four-fou|00:05:00:00:00:00;192.168.1.005;five-five-fiv|00:06:00:00:00:00;192.168.1.006;six-six-s|00:07:00:00:00:00;192.168.1.07;seven-seven-sevenn|00:08:00:00:00:00;192.168.1.008;eight-eight-e|00:09:00:00:00:00;192.168.1.09;nine-nine-ninenin|00:10:00:00:00:00;192.168.1.10;ten-ten-ten-t|00:11:00:00:00:00;192.168.1.011;eleven-eleven|00:12:00:00:00:00;192.168.1.12;twelve-twelve-twe|00:13:00:00:00:00;192.168.1.13;thirteen-thirte|00:14:00:00:00:00;192.168.1.14;fourteen-fourteen|00:15:00:00:00:00;192.168.1.015;fifteen-fifteen-fi|00:16:00:00:00:00;192.168.1.4;sixteen-sixteen-sixt|00:17:00:00:00:00;192.168.1.17;seventeen-seven|00:18:00:00:00:00;192.168.1.018;eighteen-eig|00:19:00:00:00:00;192.168.1.019;nineteen-ninet|00:20:00:00:00:00;192.168.1.020;twenty-twenty-twe|00:21:00:00:00:00;192.168.1.021;
Nov 6 12:22:58 WifiAP-01 wrtdhcpleasesreport: twentytwo-twentytwo|00:23:00:00:00:00;192.168.1.6;twentythree-twentyt|00:24:00:00:00:00;192.168.1.24;twentyfour-twentyfo|00:25:00:00:00:00;192.168.1.25;twentyfiv|00:26:00:00:00:00;192.168.1.26;twentysix-twenty|00:27:00:00:00:00;192.168.1.27;twentyseven-twent|00:28:00:00:00:00;192.168.1.28;twentyeigh|00:29:00:00:00:00;192.168.1.29;twentynine-twentyn|00:30:00:00:00:00;192.168.1.30;thirty-thirty-thir|00:31:00:00:00:00;192.168.1.31;thirtyone-thirty|00:32:00:00:00:00;192.168.1.32;thirtytwo-t|00:33:00:00:00:00;192.168.1.33;thirtythre|00:34:00:00:00:00;192.168.1.4;thirtyfour-t|
Data missing between line 21 and line 22. It should stop at line 20 and start again at line 21 to have a full break.
@Ed Morton Off topic To help you understand the project. I have a router and an access point running Openwrt. I want to monitor my wifi devices live under homeassistant. There are many solutions but each with varying levels of reliability. I found a script on the internet that met my expectations but it is not suitable for homeassistant. So I looked for the easiest way to do this. The code is based on reading logs. So I found a solution to connect my Openwrt logs to homeassistant via node-red. By default in Openwrt when a device connects or disconnects it creates an entry with its mac address. Node-red reception and processing. To have a beautiful device in homeassistant you need a name, an id. When the device connects I only know the mac address. The best solution to automate all of this and have a nice name is to look for the hostname in the dhcp.leases file. Problem when homeassistant restarts or if node-red misses some information it will consider that everyone is absent since I am already connected.The script allows you to send all devices connected to the network every 5 minutes. I take this opportunity to send the dhcp.leases information. Now with all the information I am able in home assistant to automatically create a device with information. On which device it is connected router or access point, on which antenna, its name, its mac address, its ip address.....
Solution
Taking a stab at it.
Given that you said in a comment:
I was going for 6 lines of dhcp.leases per send
below are 3 options, pick one depending on whatever your input and desired output format are.
Option 1:
Given this sample input, copied from https://stackoverflow.com/a/2143661/1745001 and then doubled in length so it contains more than 6 leases:
$ cat /tmp/dhcp.leases
lease 192.168.20.4 {
starts 6 2009/06/27 00:40:00;
ends 6 2009/06/27 12:40:00;
hardware ethernet 00:00:00:00:00:00;
uid 00:00:00:00:00:00;
client-hostname "examle-workstation1";
}
lease 192.168.20.5 {
starts 6 2009/06/27 00:40:00;
ends 6 2009/06/27 12:40:00;
hardware ethernet 00:00:00:00:00:00;
}
lease 192.168.20.6 {
starts 6 2009/06/27 00:40:00;
ends 6 2009/06/27 12:40:00;
hardware ethernet 00:00:00:00:00:01;
uid 00:00:00:00:00:01;
client-hostname "examle-workstation2";
}
lease 192.168.20.7 {
starts 6 2009/06/27 00:40:00;
ends 6 2009/06/27 12:40:00;
hardware ethernet 01:00:00:00:00:00;
}
lease 192.168.20.4 {
starts 6 2009/06/27 00:40:00;
ends 6 2009/06/27 12:40:00;
hardware ethernet 00:00:00:00:00:00;
uid 00:00:00:00:00:00;
client-hostname "examle-workstation1";
}
lease 192.168.20.5 {
starts 6 2009/06/27 00:40:00;
ends 6 2009/06/27 12:40:00;
hardware ethernet 00:00:00:00:00:00;
}
lease 192.168.20.6 {
starts 6 2009/06/27 00:40:00;
ends 6 2009/06/27 12:40:00;
hardware ethernet 00:00:00:00:00:01;
uid 00:00:00:00:00:01;
client-hostname "examle-workstation2";
}
lease 192.168.20.7 {
starts 6 2009/06/27 00:40:00;
ends 6 2009/06/27 12:40:00;
hardware ethernet 01:00:00:00:00:00;
}
This will call logger
once per second with up to 6 leases at a time as it's last argument:
$ cat tst.sh
#!/usr/bin/env bash
dhcp_leases='/tmp/dhcp.leases'
WrtPresenceDhcp() {
awk -v maxLeases=6 -v OFS=';' '
NF {
gsub(/^[[:space:]]+|[[:space:]]+$/,"")
lease = (linesCnt++ ? lease OFS : "") $0
}
$1 == "}" {
leases = (numLeases++ ? leases "|" : "") lease
if ( numLeases == maxLeases ) {
print leases
numLeases = 0
}
linesCnt = 0
next
}
END {
if ( numLeases ) {
print leases
}
}
' "$dhcp_leases"
}
while IFS= read -r leases; do
echo logger -t "wrtdhcpleasesreport" "$leases"
sleep 1
done < <(WrtPresenceDhcp)
$ ./tst.sh
logger -t wrtdhcpleasesreport lease 192.168.20.4 {;starts 6 2009/06/27 00:40:00;;ends 6 2009/06/27 12:40:00;;hardware ethernet 00:00:00:00:00:00;;uid 00:00:00:00:00:00;;client-hostname "examle-workstation1";;}|lease 192.168.20.5 {;starts 6 2009/06/27 00:40:00;;ends 6 2009/06/27 12:40:00;;hardware ethernet 00:00:00:00:00:00;;}|lease 192.168.20.6 {;starts 6 2009/06/27 00:40:00;;ends 6 2009/06/27 12:40:00;;hardware ethernet 00:00:00:00:00:01;;uid 00:00:00:00:00:01;;client-hostname "examle-workstation2";;}|lease 192.168.20.7 {;starts 6 2009/06/27 00:40:00;;ends 6 2009/06/27 12:40:00;;hardware ethernet 01:00:00:00:00:00;;}|lease 192.168.20.4 {;starts 6 2009/06/27 00:40:00;;ends 6 2009/06/27 12:40:00;;hardware ethernet 00:00:00:00:00:00;;uid 00:00:00:00:00:00;;client-hostname "examle-workstation1";;}|lease 192.168.20.5 {;starts 6 2009/06/27 00:40:00;;ends 6 2009/06/27 12:40:00;;hardware ethernet 00:00:00:00:00:00;;}
logger -t wrtdhcpleasesreport lease 192.168.20.6 {;starts 6 2009/06/27 00:40:00;;ends 6 2009/06/27 12:40:00;;hardware ethernet 00:00:00:00:00:01;;uid 00:00:00:00:00:01;;client-hostname "examle-workstation2";;}|lease 192.168.20.7 {;starts 6 2009/06/27 00:40:00;;ends 6 2009/06/27 12:40:00;;hardware ethernet 01:00:00:00:00:00;;}
Option 2
If, on the other hand, what you show under the text
/tmp/test (I've matched the number of characters in my actual file)
in your question:
$ cat /tmp/dhcp.leases
0 00:01:00:00:00:00 192.168.1.01 one-one-one-oneo *
0 00:02:00:00:00:00 192.168.1.02 Two-two-twoo 01:00:02:00:00:00:00
0 00:03:00:00:00:00 192.168.1.003 three-three-thr 01:00:03:00:00:00:00
0 00:04:00:00:00:00 192.168.1.04 four-four-four-fou *
0 00:05:00:00:00:00 192.168.1.005 five-five-fiv 01:00:05:00:00:00:00
0 00:06:00:00:00:00 192.168.1.006 six-six-s *
0 00:07:00:00:00:00 192.168.1.07 seven-seven-sevenn *
0 00:08:00:00:00:00 192.168.1.008 eight-eight-e *
0 00:09:00:00:00:00 192.168.1.09 nine-nine-ninenin 01:00:09:00:00:00:00
0 00:10:00:00:00:00 192.168.1.10 ten-ten-ten-t 01:00:09:00:00:00:00
0 00:11:00:00:00:00 192.168.1.011 eleven-eleven 01:00:09:00:00:00:00
0 00:12:00:00:00:00 192.168.1.12 twelve-twelve-twe 01:00:09:00:00:00:00
0 00:13:00:00:00:00 192.168.1.13 thirteen-thirte *
0 00:14:00:00:00:00 192.168.1.14 fourteen-fourteen *
0 00:15:00:00:00:00 192.168.1.015 fifteen-fifteen-fi *
0 00:16:00:00:00:00 192.168.1.4 sixteen-sixteen-sixt *
0 00:17:00:00:00:00 192.168.1.17 seventeen-seven 01:00:09:00:00:00:00
0 00:18:00:00:00:00 192.168.1.018 eighteen-eig 01:00:09:00:00:00:00
0 00:19:00:00:00:00 192.168.1.019 nineteen-ninet *
0 00:20:00:00:00:00 192.168.1.020 twenty-twenty-twe 01:00:09:00:00:00:00
0 00:21:00:00:00:00 192.168.1.021 twentyone 01:00:09:00:00:00:00
0 00:22:00:00:00:00 192.168.1.22 twentytwo-twentytwo 01:00:09:00:00:00:00
0 00:23:00:00:00:00 192.168.1.6 twentythree-twentyt 01:00:09:00:00:00:00
0 00:24:00:00:00:00 192.168.1.24 twentyfour-twentyfo 01:00:09:00:00:00:00
0 00:25:00:00:00:00 192.168.1.25 twentyfiv *
0 00:26:00:00:00:00 192.168.1.26 twentysix-twenty 01:00:09:00:00:00:00
0 00:27:00:00:00:00 192.168.1.27 twentyseven-twent 01:00:09:00:00:00:00
0 00:28:00:00:00:00 192.168.1.28 twentyeigh 01:00:09:00:00:00:00
0 00:29:00:00:00:00 192.168.1.29 twentynine-twentyn 01:00:09:00:00:00:00
0 00:30:00:00:00:00 192.168.1.30 thirty-thirty-thir 01:00:09:00:00:00:00
0 00:31:00:00:00:00 192.168.1.31 thirtyone-thirty 01:00:09:00:00:00:00
1707029378 00:32:00:00:00:00 192.168.1.32 thirtytwo-t 01:00:09:00:00:00:00
0 00:33:00:00:00:00 192.168.1.33 thirtythre 01:00:09:00:00:00:00
0 00:34:00:00:00:00 192.168.1.4 thirtyfour-t 01:00:09:00:00:00:00
is actually your input format, then this would also call logger
with 6 leases at a time once per second:
$ cat tst.sh
#!/usr/bin/env bash
dhcp_leases='/tmp/dhcp.leases'
WrtPresenceDhcp() {
awk -v maxLeases=6 -v OFS=';' '
NF {
lease = $2 OFS $3 OFS $4
leases = (numLeases++ ? leases "|" : "") lease
if ( numLeases == maxLeases ) {
print leases
numLeases = 0
}
}
END {
if ( numLeases ) {
print leases
}
}
' "$dhcp_leases"
}
while IFS= read -r leases; do
echo logger -t "wrtdhcpleasesreport" "$leases"
sleep 1
done < <(WrtPresenceDhcp)
$ ./tst.sh
logger -t wrtdhcpleasesreport 00:01:00:00:00:00;192.168.1.01;one-one-one-oneo|00:02:00:00:00:00;192.168.1.02;Two-two-twoo|00:03:00:00:00:00;192.168.1.003;three-three-thr|00:04:00:00:00:00;192.168.1.04;four-four-four-fou|00:05:00:00:00:00;192.168.1.005;five-five-fiv|00:06:00:00:00:00;192.168.1.006;six-six-s
logger -t wrtdhcpleasesreport 00:07:00:00:00:00;192.168.1.07;seven-seven-sevenn|00:08:00:00:00:00;192.168.1.008;eight-eight-e|00:09:00:00:00:00;192.168.1.09;nine-nine-ninenin|00:10:00:00:00:00;192.168.1.10;ten-ten-ten-t|00:11:00:00:00:00;192.168.1.011;eleven-eleven|00:12:00:00:00:00;192.168.1.12;twelve-twelve-twe
logger -t wrtdhcpleasesreport 00:13:00:00:00:00;192.168.1.13;thirteen-thirte|00:14:00:00:00:00;192.168.1.14;fourteen-fourteen|00:15:00:00:00:00;192.168.1.015;fifteen-fifteen-fi|00:16:00:00:00:00;192.168.1.4;sixteen-sixteen-sixt|00:17:00:00:00:00;192.168.1.17;seventeen-seven|00:18:00:00:00:00;192.168.1.018;eighteen-eig
logger -t wrtdhcpleasesreport 00:19:00:00:00:00;192.168.1.019;nineteen-ninet|00:20:00:00:00:00;192.168.1.020;twenty-twenty-twe|00:21:00:00:00:00;192.168.1.021;twentyone|00:22:00:00:00:00;192.168.1.22;twentytwo-twentytwo|00:23:00:00:00:00;192.168.1.6;twentythree-twentyt|00:24:00:00:00:00;192.168.1.24;twentyfour-twentyfo
logger -t wrtdhcpleasesreport 00:25:00:00:00:00;192.168.1.25;twentyfiv|00:26:00:00:00:00;192.168.1.26;twentysix-twenty|00:27:00:00:00:00;192.168.1.27;twentyseven-twent|00:28:00:00:00:00;192.168.1.28;twentyeigh|00:29:00:00:00:00;192.168.1.29;twentynine-twentyn|00:30:00:00:00:00;192.168.1.30;thirty-thirty-thir
logger -t wrtdhcpleasesreport 00:31:00:00:00:00;192.168.1.31;thirtyone-thirty|00:32:00:00:00:00;192.168.1.32;thirtytwo-t|00:33:00:00:00:00;192.168.1.33;thirtythre|00:34:00:00:00:00;192.168.1.4;thirtyfour-t
Option 3
If your input is the same as shown for Option 1 above but you want output similar to that shown for Option 2 above, then this would also call logger
with 6 leases at a time once per second:
$ cat tst.sh
#!/usr/bin/env bash
dhcp_leases='/tmp/dhcp.leases'
WrtPresenceDhcp() {
awk -v maxLeases=6 -v OFS=';' '
{ gsub(/^[[:space:]]+|[;[:space:]]+$/,"") }
$1 == "lease" { ip = $2 }
$1 == "hardware" { mac = $3 }
$1 == "client-hostname" {
gsub(/^[^"]+"|"[^"]*$/,"")
host = $0
}
$1 == "}" {
lease = mac OFS ip OFS host
mac = ip = host = ""
leases = (numLeases++ ? leases "|" : "") lease
if ( numLeases == maxLeases ) {
print leases
numLeases = 0
}
next
}
END {
if ( numLeases ) {
print leases
}
}
' "$dhcp_leases"
}
while IFS= read -r leases; do
echo logger -t "wrtdhcpleasesreport" "$leases"
sleep 1
done < <(WrtPresenceDhcp)
$ ./tst.sh
logger -t wrtdhcpleasesreport 00:00:00:00:00:00;192.168.20.4;examle-workstation1|00:00:00:00:00:00;192.168.20.5;|00:00:00:00:00:01;192.168.20.6;examle-workstation2|01:00:00:00:00:00;192.168.20.7;|00:00:00:00:00:00;192.168.20.4;examle-workstation1|00:00:00:00:00:00;192.168.20.5;
logger -t wrtdhcpleasesreport 00:00:00:00:00:01;192.168.20.6;examle-workstation2|01:00:00:00:00:00;192.168.20.7;
For all options above, in the awk output I separated leases with |
s and fields within a lease with ;
s as that looks like it might be you want from some of the text blocks in your question. Obviously just remove echo
to actually call logger
.
If what you're trying to do is something other than one of the above, please edit your question to clarify.
Answered By - Ed Morton Answer Checked By - Mildred Charles (WPSolving Admin)