sh 스크립트의 컬(curl)은 아마도 공백 때문일 것입니다.

sh 스크립트의 컬(curl)은 아마도 공백 때문일 것입니다.

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

문자 @로 데이터를 시작하는 경우 나머지는 데이터를 읽는 파일 이름이어야 합니다. 또는 - 컬을 사용하여 stdin에서 데이터를 읽으려는 경우에는 다음과 같습니다.

답변2

@Archemar와 @dave_thompson_085의 도움을 받아 작동하는 솔루션을 공식화할 수 있었습니다.

보내거나 받을 때 공백과 줄 바꿈 문제를 해결하기 위해 위와 "실제" 줄 바꿈을 포함하는 파일을 만들었습니다. 콘텐츠를 "양식"으로 보내면 형식이 그대로 유지된다는 사실을 알 때까지 새 파일을 컬로 파이프하면 여전히 동일한 문제가 발생했습니다.

이와 같은 문제가 자동화된 방식으로 해결되는 것을 보는 것은 정말 멋진 일입니다.

모두 다시 한번 감사드립니다

#!/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

관련 정보