Bash脚本从服务器A下载HTML并将其POST到服务器B.

时间:2015-03-26 21:53:42

标签: bash escaping

本地网络中有一个网站。我想编写一个bash脚本,它将从本地网站下载HTML并将其发送到外部服务器,PHP脚本可以从$_POST读取它:

html=$(wget --post-data="str=data" -qO- http://192.168.1.8/reg/index.php)
#so far so good, and $html contains data I want to send to my website
wget --post-data="html=$html" -qO- http://mywebsite.com/test.php

test.php中,数据总是格式不正确。

有没有办法正确逃脱$html,还是应该完全改变我对问题的处理方法?

1 个答案:

答案 0 :(得分:0)

  1. 在发送请求之前使用Base64对HTML进行编码:

    html=$( wget --post-data="str=data" -qO- http://192.168.1.8/reg/index.php | base64 )
    
  2. 对您的POST数据进行URL编码。

    不幸的是,wget不会为您的帖子数据进行网址编码。如果 可用,您可以使用curl为其提供URL编码 POST请求:

    curl -S -d "str=${html}" --data-urlencode -- http://mywebsite.com/test.php
    

    如果curl不可用,则需要替换+中的所有$html 在发布到%2B之前,/以及所有%2Ftest.php匹配,例如 使用:

    html=$( sed -e 's/+/%2B/g' -e 's/\//%2F/g' <<< "$html" )
    
  3. test.php中,您之前已将$_POST['str']传递给base64_decode() 使用它,见其中 documentation