On Wed, 26 Dec 2007 21:36:48 +0000 (UTC), gazelle@[EMAIL PROTECTED]
(Kenny McCormack) wrote:
>In article <ppf5n3pkc4v6elkicjp8arsrff7rp6vrtg@[EMAIL PROTECTED]
>,
>Grant <bugsplatter@[EMAIL PROTECTED]
> wrote:
>...
>>Move the first tick:
>># awk /$VAR/ '{ print $0 }' myfile
>
>Others will chime in and say that this is a bad idea (security, etc,
etc).
>That is VAR has some weird value (with "rm -rf" and such in it), it will
>cause something horrible to happen, etc, etc.
>
>But the fact is, it is perfectly safe for normal user code; you can
>usually control it OK, and, more to the point, the alternative, using
>"-v" is just as vulnerable to the same hack(s).
>
>The only truly safe way to do it is to pass it in the environment.
>Well, that's safe in an AWK sense, but of course, you then have to worry
>about the syntax of your "ex****t foo=..." statement...
>
>But the real answer is that you almost certainly don't need to do this
>at all, if you are using AWK correctly. I've found that most of the
>time, when this question is asked, the answer is "Learn and use AWK
>correctly, and you won't need to do it".
Okay, I have this running for some weeks as a cron job:
# pull rx and tx bytes from /proc/net/dev:
# rx tx
# ppp0:22921966 17725 0 0 0 0 0 0 444056 10437 0 0 0 0 0 0
#
# output line: date rx_bytes tx_bytes, or date 0 0 when interface down
#
CONF="/etc/netdraw.conf"
INTERFACE=$(awk '$1=="interface"{print $2}' $CONF)
SAMPLEFILE=$(awk '$1=="samplefile"{print $2}' $CONF)
#
printf "%s " $(date +%F.%H:%M) >> $SAMPLEFILE
awk /$INTERFACE:/'{f++;sub(/:/," ");print $2,$10};END{if(!f)print 0,0}' \
/proc/net/dev >> $SAMPLEFILE
Bash, if it matters, yes I can do the whole thing in awk but it looks even
uglier?
BEGIN {
ARGV[ARGC++] = "/etc/netdraw.conf" # grab configuration file
ARGV[ARGC++] = "/proc/net/dev" # grab interface status
}
/^$/ { next }
/^#/ { next }
# read configuration
NR == FNR { conf[$1] = $2 }
NR == FNR && $1 == "samplefile" {
out = $2
printf "%s ", strftime("%F.%H:%M") >> out
}
# read network status
NR != FNR && $0 ~ conf["interface"] ":" {
++f
sub(/:/, " ")
print $2, $10 > out
}
END {
if (!f)
print 0, 0 > out
close(out)
}
Grant.
--
http://bugsplatter.mine.nu/


|