我有这个日志:
30-12-2022 23:24:32 : URI: p=2&pwr=0&noipcheck
30-12-2022 23:28:43 : URI: p=2&pwr=1110&noipcheck&debug
30-12-2022 23:28:51 : URI: p=2&pwr=0&noipcheck&debug
30-12-2022 23:31:57 : URI: p=2&pwr=2.200000
30-12-2022 23:50:02 : URI: p=2&pwr=0
31-12-2022 06:59:35 : URI: p=2&pwr=3
31-12-2022 07:04:35 : URI: p=2&pwr=0
31-12-2022 10:39:31 : URI: p=2&pwr=2.900000
31-12-2022 10:53:31 : URI: p=2&pwr=1507.900000
31-12-2022 10:53:36 : URI: p=2&pwr=1326.400000
31-12-2022 10:53:41 : URI: p=2&pwr=1410.200000
31-12-2022 10:53:46 : URI: p=2&pwr=2.900000
我现在怎样才能写一个脚本
simulate <hostname> <mydate> <starttimestamp> <endtimestamp> <logfilename>
其中日期被过滤<mydate>
<starttimestamp>
<endtimestamp>
,并且 URI 应使用 调用<hostname>
。
我在制作时遇到问题,sleep
因为需要提前阅读第二行,以便我知道时差。
铁 simulate https://www.xxxx.at 31-12-2022 10:00:00 10:55:00 log.txt
在unix下的bash中如何做到这一点?
然后它应该调用,但根据日志时间,每次调用之间的https://www.xxxx.at?<URI>
最近时间之后有时间延迟。<starttimestamp>
铁:
now:
`curl` "https://www.xxxx.at?p=2&pwr=2.900000"
`sleep` timedifference [31-12-2022 10:53:31 - 31-12-2022 10:39:31]
`curl` "https://www.xxxx.at?p=2&pwr=1507.900000"
...
`sleep` timedifference [31-12-2022 10:53:46 - 31-12-2022 10:53:41]
`curl` "https://www.xxxx.at?p=2&pwr=2.900000"
答案1
我现在可以在一个 awk 脚本 [named mysim.awk
] 中完成此操作:
BEGIN { FS="- :" }
{
split($1,a,"[: -]")
secs = mktime(a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6])
cmd[NR] = a[11]
t[NR] = a[6] + 60 * (a[5] + 60 * a[4])
dt[NR] =a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6]
}
END {
split(sdt,b,"[-: ]")
split(edt,bb,"[-: ]")
startsecs = mktime(b[3]" "b[2]" "b[1]" "b[4]" "b[5]" "b[6])
endsecs = mktime(bb[3]" "bb[2]" "bb[1]" "bb[4]" "bb[5]" "bb[6])
print sdt"[->"startsecs"] bis "edt"[->"endsecs"]"
for (c = 1; c <= NR-1; c++)
{
if ((mktime(dt[c]) >= startsecs) && (mktime(dt[c]) <= endsecs))
{
d = t[c+1] - t[c]
if (d < 0) d += 86400
h = d / 3600
d %= 3600
m = d / 60
s = d % 60
printf "sleep %02d ; %s%s \n",s,"curl \"https://xxxxx.at/sendsms.php?noresponse&noipcheck&nosms&debug&",cmd[c]"\""
}
}
}
从 LINUX 提示符调用 startdatetime 和 enddatetime fe:
awk -v sdt="31-12-2022 11:00:00" -v edt="31-12-2022 13:00:00" -f mysim.awk mylog.txt
或者通过 bash 脚本(名为runsimulate.sh
] ,如下所示:
#!/bin/bash
# call f.e. ./runsimulate.sh "02-01-2023 14:00:00" "02-01-2023 14:50:00" <logfilename>
awk -f mysim.awk -v sdt="$1" -v edt="$2" $3