Shell-Script

從 n 行 txt 文件中編寫 n curl POST 腳本並有條件地保存 HTTP 請求響應“200 OK”的輸出

  • October 4, 2021

鑑於此 cURL“POST”請求:

$ curl -i -s -k -X $'POST' \
-H $'Host: api.host.it' \
-H $'Content-Length: 205' \
-H $'Sec-Ch-Ua: \"Chromium\";v=\"93\", \" Not;A Brand\";v=\"99\"' \
-H $'Messageid: 9d6dd58d2df24d0aa410245a' \
-H $'Sessionid: ada9e560ed204e85a25e5475' \
-H $'Devicetype: ANDROID' \
-H $'Interactiondate-Date: 2021-09-27' \
-H $'Interactiondate-Time: 20:32:37.758' \
-H $'Sec-Ch-Ua-Mobile: ?0' \
-H $'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36' \
-H $'Content-Type: application/json;charset=UTF-8' \
-H $'Accept: application/json' \
-H $'Sourcesystem: WEB' \
-H $'Businessid: bbc0a98dc23a4a84968c42e4' \
-H $'Channel: HOSTWEBCO' \
-H $'Transactionid: 3F941666A8414D3C874AC77B' \
-H $'Sec-Ch-Ua-Platform: \"Linux\"' -H $'Origin: https://www.host.com' \
-H $'Sec-Fetch-Site: same-site' \
-H $'Sec-Fetch-Mode: cors' \
-H $'Sec-Fetch-Dest: empty' \
-H $'Referer: https://www.host.com/' \
-H $'Accept-Encoding: gzip, deflate' \
-H $'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8' \
-H $'Connection: close' \
--data-binary $'{\"mount\":25,
                \"Method\":\"SA\",
                \"redirectUrlKo\":\"https://www.host.com/scarica?esito=KO\",
                \"redirectUrlOk\":\"https://www.host.com/scarica?esito=OK\",
                \"toMsisdn\":\"PARAMETER\",
                \"txReqDescription\":\"scarica Online\"}' \
$'https://api.host.com/api/charge/public/init'

我需要創建一個 Bash 或 Python 腳本,為 file 中的每一行執行 cURL 命令,將每一行作為 cURL選項numbers.txt內顯示的 PARAMETER 佔位符的輸入。--data-binary在每個請求之後,僅當 HTTP 請求響應程式碼為“200 OK”時,才curl應將輸出定向到文件。output.txt

我知道 cURL 接受文件輸入,--data@file.txt但在此之前我還有其他欄位,它不起作用。

在下面的 shell 腳本中,我解決了您的 3 個要求:

  • PARAMETER從文件中讀取連續的值numbers.txt,它們在一行中列出一個值。
  • 對於每個檢索到的值,PARAMETER測試您的 HTTP 伺服器請求是否返回程式碼“200”
  • 將 cURL 的連續輸出附加到output.txt提供的 HTTP 返回程式碼為“200 OK”

就引號和轉義引號而言,我沒有修改您的語法。這可能是案例或特定於您的環境,我無法對其進行測試。將以下 shell 腳本放在一個文件中,myscript.sh並使用終端 cmd: 使該文件可執行chmod ug+x myscript.sh

$ cat myscript.sh

$$ output $$

#!/usr/bin/sh    
output="$(/usr/bin/date +%Y%m%d_%H%M%S)""_output.txt"
while read -r dyn_param; do
   set -- -isk \
   -X 'POST' \
   -H 'Host: api.host.it' \
   -H 'Content-Length: 205' \
   -H 'Sec-Ch-Ua: \"Chromium\";v=\"93\", \" Not;A Brand\";v=\"99\"' \
   -H 'Messageid: 9d6dd58d2df24d0aa410245a' \
   -H 'Sessionid: ada9e560ed204e85a25e5475' \
   -H 'Devicetype: ANDROID' \
   -H 'Interactiondate-Date: 2021-09-27' \
   -H 'Interactiondate-Time: 20:32:37.758' \
   -H 'Sec-Ch-Ua-Mobile: ?0' \
   -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36' \
   -H 'Content-Type: application/json;charset=UTF-8' \
   -H 'Accept: application/json' \
   -H 'Sourcesystem: WEB' \
   -H 'Businessid: bbc0a98dc23a4a84968c42e4' \
   -H 'Channel: HOSTWEBCO' \
   -H 'Transactionid: 3F941666A8414D3C874AC77B' \
   -H 'Sec-Ch-Ua-Platform: \"Linux\"' \
   -H 'Origin: https://www.host.com' \
   -H 'Sec-Fetch-Site: same-site' \
   -H 'Sec-Fetch-Mode: cors' \
   -H 'Sec-Fetch-Dest: empty' \
   -H 'Referer: https://www.host.com/' \
   -H 'Accept-Encoding: gzip, deflate' \
   -H 'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8' \
   -H 'Connection: close'
   
   set -- "$@" \
--data-binary '{"mount":25,"Method":"SA","redirectUrlKo":"https://www.host.com/ricarica?esito=KO","redirectUrlOk":"https://www.host.com/ricarica?esito=OK","toMsisdn":'\""$dyn_param"\"',"txReqDescription":"scarica Online"}'

httpcode=$(curl -o curl_tmp_dump -w "%{http_code}" "$@" https://api.host.com/api/recharge/public/init 2>/dev/null)
[ "$httpcode" -eq 200 ] && \cat curl_tmp_dump >> "$output"
\rm -f curl_tmp_dump

done < numbers.txt

exit 0

引用自:https://unix.stackexchange.com/questions/671023