I have an sh
script that is listening to log output.
When it finds an IP address under certain conditions, it need to send a curl
request out to that IP address (Receiver is a Polycom VoIP phone if it matters).
On the receiver, I am getting:
PushParserC::parseDoc: Expecting <PolycomIPPhone>: $'<PolycomIPPhone><Data
where as a correct line produces this:
wappPushHandlerC::Handle: <PolycomIPPhone><Data priority="Critical">Key:Setup
and on a -v
dump of curl
I get:
curl: (6) Could not resolve host: priority="Critical">Key....
< should be an IP address
In both cases, it looks like communication is being cut the at white space in my script. Most solutions get rid of the whitespace, but that breaks the XHTML that is expected.
My script is below
#!/bin/sh
tail -0f /var/log/somelogfile.log | while read line
do
if echo $line | grep "\[WARNING\]" | grep -q "SIP auth failure" ; then
# Log detected sip authentication error to file
STR="$(echo $line | awk '{print "Date:",$1,"Time:",$2,"Login:",$14,"IP:",$17}')" >> logger.txt
# Get the found private IP address out of the errored line
IP="$(echo $STR | rev | cut -d" " -f1 | rev)"
# Provide output to the user of the IP to brute
echo "Target IP: " $IP
# Content Type header
CT="Content-Type: application/x-com-polycom-spipx"
# The XHTML to send to the phone in question for forced factory reset
XHTML="curl -v --digest -u Push:Push -d $'<PolycomIPPhone><Data priority=\"Critical\">Key:Setup\nKey:Dialpad2\nKey:Dialpad9\nKey:Dialpad9\nKey:Dialpad9\nKey:Softkey4\nKey:Dialpad1\nKey:Dialpad5\nKey:Dialpad5</Data></PolycomIPPhone>' --header \"$CT\" $IP/push"
# print out URL for test
echo $XHTML
RESPONSE=`$XHTML`
echo
echo $RESPONSE
fi
done
# This is an example of the fuctional code that works straight in the terminal.
# curl --digest -u Push:Push -d $'<PolycomIPPhone><Data priority="Critical">Key:Setup\nKey:Dialpad2\nKey:Dialpad9\nKey:Dialpad9\nKey:Dialpad9\nKey:Softkey4\nKey:Dialpad1\nKey:Dialpad5\nKey:Dialpad5</Data></PolycomIPPhone>' --header "Content-Type: application/x-com-polycom-spipx" xx.xx.xx.xx/push && echo
Other solutions remove whitespace which isn't possible in this context or encode. Neither of which work in this application though!
Thanks!
$XHTML
and then execute its contents, but that tokenizes at all whitespace -- unless you use doublequotes"
in which case it doesn't tokenize at all which also fails. The easy solution is do the command directly in the script not in a variable; if you want to see it as well use the builtinset -x
. An alternative inbash ksh zsh
is to use an array variable with each intended token as an element and execute (and display)"${array[@]}"
-- but insh
as you shebang'ed (contrary to your tag) this is a good bit harder. – dave_thompson_085 Feb 22 '17 at 16:12[[ $line == *\[WARNING\]* && $line == *SIP\ auth\ failure* ]]
instead of twogrep
s, and${line##* }
instread ofrev-cut-rev
. And yourSTR=$(..) >>logger.txt
doesn't actually write anything to the file. – dave_thompson_085 Feb 22 '17 at 16:35\n
character to send using @Archemar 's answer? – Ari Feb 23 '17 at 07:14tail -f file.log | awk -f scriptfile.awk
. See Why is using a shell loop to process text considered bad practice? and learn thesystem()
function in Awk. – Wildcard Feb 24 '17 at 02:45