我有一个sh
正在监听日志输出的脚本。当它在某些条件下找到 IP 地址时,需要curl
向该 IP 地址发送请求(如果重要的话,接收器是 Polycom VoIP 电话)。
在接收器上,我得到:
PushParserC::parseDoc: Expecting <PolycomIPPhone>: $'<PolycomIPPhone><Data
正确的行会产生以下结果:
wappPushHandlerC::Handle: <PolycomIPPhone><Data priority="Critical">Key:Setup
在-v
转储中curl
我得到:
curl: (6) Could not resolve host: priority="Critical">Key....
< 应该是 IP 地址
在这两种情况下,我的脚本中的空白区域似乎都被切断了。大多数解决方案都会消除空格,但这会破坏预期的 XHTML。
我的脚本如下
#!/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
其他解决方案会删除在此上下文中不可能的空白或进行编码。但在这个应用程序中这两个都不起作用!
谢谢!
答案1
对于-d
选择,我会尝试
echo '$<PolycomIPPhone><Data priority=\"Critical\">....' | curl -d@- (other options)
根据man curl
如果您以字母@开头数据,则其余部分应该是从中读取数据的文件名,或者 - 如果您希望curl从stdin读取数据。
答案2
使用@Archemar 和@dave_thompson_085 的帮助,我能够制定一个可行的解决方案。
为了解决发送或接收时的空格和换行问题,我创建了一个包含上述内容和“实际”换行符的文件。将新文件通过管道传输到curl中仍然给我带来了同样的问题,直到我发现将内容作为“表单”发送,从而保持格式完整。
看到这样的问题以自动化的方式得到解决绝对是很酷的。
再次感谢大家
#!/bin/bash
# Sends a data file to the phone using curl
tail -0f /var/log/freeswitch/freeswitch.log | while read line
do
if [[ $line == *\[WARNING\]* && $line == *SIP\ auth\ failure* ]] ; then
STR="$(echo $line | awk '{print "Date:",$1,"Time:",$2,"IP:",$17,"Login:",$14}')" # Pull important lines from log
IP="${line##* }" # Store private IP address of failed auth
CT="Content-Type: application/x-com-polycom-spipx" # Content Type header
OCCUR=`grep "$IP" logger.txt | wc -l` # Count of how many failed attempts to auth
# Provide output to the terminal
echo -n "TargetIP:" $IP
# If there are less than 6 attemps at authenticating, send commands to phone
if [ $OCCUR -le 5 ] ; then
echo " Formatting with" $OCCUR "previous attepts."
# Log the parsed string from the log with attempt status
echo "Status: FORMAT" $STR >> logger.txt
# Send user a HTML warning screen
curl -s --digest -u push:push -d "<PolycomIPPhone><Data priority='"critical"'><h1> PHONE WILL UPDATE </h1>Please do not interact with the phone. It will now format and restart. Contact IT support for any concerns</Data></PolycomIPPhone>" --header $CT $IP/push > /dev/null
# Allow for user read time
sleep 8
# Send instruction set phone in question for forced factory reset
curl -s --digest -u push:push --form "[email protected]" --header "$CT" $IP/push > /dev/null
# If there are 6 failed attempts, log to file for further action
elif [ $OCCUR -eq 6 ] ; then
echo " Too many attempts. Logging..."
echo "Status: FAILED" $STR >> logger.txt
# Beyond 6 attemps, no action will be taken
else
echo " IGNORED"
fi
fi
done