[lug] Bash Scripting Ping
Chan Kar Heng
chankarheng at gmail.com
Sun Nov 8 12:26:51 MST 2020
Hi.
I suspect what you want is something similar to below, which I've not fully tested. But in below, ping output seems to go to a stdout buffer of 4k. if ping -O -D x.x.x.x 1>&2, then output goes to stderr which is not buffered but << here document operator reads only from stdin.
Some folks use some unbuffer command to unbuffer ping output. Can't try this from my phone since i can't put the unbuffer command into my phone.
Note: ping_line_output would likely be visible in 2nd loop as it's part of the main process & not the forked process.
while read ping_line_output
do
# do not need to do anything in this loop
#echo "[###${ping_line_output}###]" # for debug
true
done <<EndOfStream
`ping -O -D 127.0.0.1`
EndOfStream &
PINGLOOPPID="$!"
continue_loop=1
while [ continue_loop -gt 0 ]
do
if [ ${$ping_line_output} -gt 0 ]; then
# do something with
# ping_line_output or PINGLOOPPID or continue_loop
ping_line_output=""
fi
sleep 0.25
done
Best regards,
kh
On 9 November 2020 02:50:18 GMT+08:00, "D. Stimits" <stimits at comcast.net> wrote:
>
>>
>> How's Jed's suggestion different from what you need?
>>
>A single ping is a single line of text, followed by termination of the
>process providing the text. I am interested in parsing sections of a
>continuous, never ending string of text whenever a newline is found.
>Each newline providing a "token" to process, but the text stream never
>ending.
>
>How do I trigger something to occur on a subset of a continuous string
>of text delimited by newlines without actually ending that continuous
>source of a text stream? That is the brain teaser, to arbitrarily pick
>and process subsets of a continuous stream of characters in bash. It
>does not need to be "ping", that is just the case that is puzzling me.
>
>Perhaps an updated pseudo code:
>stream = `ping` # ping never ends, but embeds newlines.
>while (stream.subset_token_via_newline); do
>do stuff with newline delimited token, not interrupting the stream;
>done
>
>Versus:
>while (true); do
>token = something_generating_a_single_token;
>do something with token;
>done
>
>> Best regards,
>> kh
>>
>>
>> On 9 November 2020 02:27:21 GMT+08:00, "D. Stimits"
><stimits at comcast.net> wrote:
>>
>> > >
>> >
>> > > > > On 11/08/2020 11:21 AM Jed S. Baer
><blug at jbaer.cotse.net> wrote:
>> > >
>> > >
>> > > On Sun, 8 Nov 2020 10:53:09 -0700 (MST)
>> > > D. Stimits wrote:
>> > >
>> > >
>> > > > > > > I am curious about something in bash
>scripting which does not seem to be
>> > > > particularly easy. I wanted to script "ping -O
>-D address" such that each
>> > > > line gets processed by some logic if the ping
>fails. Redirecting ping to
>> > > > a variable does not work because it only
>"returns" when the ping exits
>> > > > (each line is not an exit). Even if I were to
>fork and exec, the forked
>> > > > process would itself have the same problem.
>> > > >
>> > > > Is there some simple/clever way to process each
>line of a ping in bash
>> > > > without killing off the ping itself? My goal
>was to send it through some
>> > > > database and statistics type processing as
>success/failure lines occur.
>> > > >
>> > > > > > > I'm not sure of quite what you want. Is it
>necessary to process this in
>> > > real time? And you say "...if ping fails", but then
>"... as success/failure
>> > > lines occur."
>> > >
>> > > > >
>> >
>> > I'd like to know if there is a way to pipe the output of
>ping such that each line is equivalent to a loop iteration. Pseudo
>code:
>> > while (ping text line); do
>> > line | edit line; # e.g., substituting microseconds
>for date.
>> > print edit line if conditions are met; # e.g., a few
>lines of fail prior to print.
>> > done
>> >
>> > I'm not actually concerned that this is ping so much as I
>am trying to figure out how a newline from a continuous command can be
>used to trigger a loop iteration. It has become something of a "brain
>teaser" for me. It is easy if the ping itself ends with a single ping.
>It is also easy if you run ping all day and only then process the
>results upon exit of ping. I am now just really curious how the newline
>delimited continuous output of ping can be used to trigger something
>else despite the continuous output itself not stopping.
>> >
>> > >
>> >
>> > > > > The super simple thing is a bash read loop. But
>you won't know the exit
>> > > status until the ping finishes.
>> > >
>> > > $ ping -c 3 -D -O n.n.n.n | while read LINE;
>> > >
>> > > > > > > do
>> > > > echo $LINE
>> > > > done
>> > > >
>> > > > > > > Bash read will do token separation using IFS,
>so you could do
>> > > | while read FIELD1 FIELD2 etc
>> > >
>> > > Offhand, if the exit status of ping must be known
>before processing, then
>> > > redirect ping output to a file, check status, then
>you can still do a bash
>> > > read loop to process the file. If needed, the bash
>mktemp command will
>> > > generate a unique tmp filename for your ping output.
>> > >
>> > > --
>> > > All operating systems suck, but Linux just sucks less
>> > > - Linus Torvalds
>> > > ---------------------------------------------
>> > > Web Page: http://lug.boulder.co.us
>> > > Mailing List:
>http://lists.lug.boulder.co.us/mailman/listinfo/lug
>> > > Join us on IRC: irc.hackingsociety.org port=6667
>channel=#hackingsociety
>> > >
>> > > > > ---------------------------------------------
>> >
>> > Web Page: http://lug.boulder.co.us
>> > Mailing List:
>http://lists.lug.boulder.co.us/mailman/listinfo/lug
>> > Join us on IRC: irc.hackingsociety.org port=6667
>channel=#hackingsociety
>> >
>> > > _______________________________________________
>> Web Page: http://lug.boulder.co.us
>> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>> Join us on IRC: irc.hackingsociety.org port=6667
>channel=#hackingsociety
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20201109/ecbeca62/attachment.html>
More information about the LUG
mailing list