天天看點

curl和openssl交叉編譯和curl指令使用

curl依賴openssl,是以需要先編譯openssl:

編譯openssl:

下載下傳openssl:https://github.com/openssl/openssl/tree/OpenSSL_1_0_2r,下載下傳解壓。

參照:https://blog.csdn.net/ty3219/article/details/77717478 的方法一去編譯,分别執行:

./Configure --prefix=/home/nfsshare/hisi/curl/curl/openssl shared no-asm linux-armv4
make CC="arm-none-linux-gnueabi-gcc" AR="arm-none-linux-gnueabi-ar r" RANLIB="arm-none-linux-gnueabi-ranlib" LD="arm-none-linux-ld"
make install
           

結果出現:Relocations in generic ELF (EM: 40)錯誤,格式不正确:

/usr/bin/ld: /opt/wifi/tool/openssl/lib/libcrypto.a(ex_data.o): Relocations in generic ELF (EM: 40)
/usr/bin/ld: /opt/wifi/tool/openssl/lib/libcrypto.a(ex_data.o): Relocations in generic ELF (EM: 40)
/opt/wifi/tool/openssl/lib/libcrypto.a: could not read symbols: File in wrong format
collect2: ld 傳回 1
make: *** [hostapd] 錯誤 1
           

未找到合适和解決方法,于是放棄此方法,改用方法二:

執行config:

make distclean
./config --prefix=/home/nfsshare/hisi/curl/curl/openssl -shared no-asm
           

然後 vi Makefile 修改 CC=arm-linux-gcc 并将 -m64 參數去掉,然後 make,make install,就安裝完成了。

接下來編譯curl:

從 https://curl.haxx.se/download/curl-7.67.0.tar.gz 下載下傳源碼,執行:

./configure --prefix=/home/nfsshare/hisi/curl/curl --host=arm-none-linux CC=arm-linux-gcc --with-ssl=/home/nfsshare/hisi/curl/curl/openssl
make
make install
           

于是curl編譯就完成了,将編譯出來的curl不需要的目錄檔案(.a、include等)清理掉,拷貝到裝置上就可以開始執行了。

curl使用:

首先設定環境變量:

export LD_LIBRARY_PATH=/mnt/curl/lib:/mnt/curl/openssl/lib:$LD_LIBRARY_PATH
           

發送GET請求比較簡單:

./curl http://192.168.1.3/a.asp
           

發送POST請求:

./curl -d "username=admin&z999=z999&password=admin&Login=&platform=pc&langSelection=zhcn" http://192.168.1.3/goform/login
./curl -d "na" http://192.168.1.3/goform/get_web_hotspots_list
           

更複雜的:

./curl -H "Content-Type:application/json" -X POST -d '{"user": "admin", "passwd":"admin"}' http://192.168.1.3/login
           

當然也可以代碼控制,參考:

// Write callback.
size_t WriteCallback(char *ptr, size_t size, size_t nitems, void *userdata) {
  std::string* str = reinterpret_cast<std::string*>(userdata);
   str->append(ptr, size * nitems);
   return size * nitems;
}
bool Post(const std::string & url, const std::string & data, std::string & content)
{
    // Check parameters.
    if (url.empty()) {
       assert(false);
       return false;
    }
    content.clear();
    // Initialize curl.
    CURL* curl = Curl::curl_easy_init();
    if (!curl) {
       assert(false);
       return false;
    }
    Curl::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    Curl::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    Curl::curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
    Curl::curl_easy_setopt(curl, CURLOPT_POST, 1L);
    Curl::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
    Curl::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
    Curl::curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1L);
    Curl::curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    Curl::curl_easy_setopt (curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
   //Curl::curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 1000L);//逾時設定
    // Get.
    CURLcode status = Curl::curl_easy_perform(curl);
    int code = 0;
    Curl::curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
    // Cleanup.
    Curl::curl_easy_cleanup(curl);
    if (CURLE_OK != status) {
       LOG_ERROR("curl_easy_perform failed: " << status);
       return false;
    }
    if (code != 200) {
       LOG_INFO("response code: " << code);
       return false;
    }
    return true;
}
           

注:編譯出現You need Perl 5.,則需要安裝peral5,參照:

cd perl-5.28.0
./Configure -des -Dprefix=$HOME/localperl
make
make install
           

注2:對于不是自己的頁面,可以用 Wireshark 抓包,找POST,GET等包請求的内容,如:

curl和openssl交叉編譯和curl指令使用

繼續閱讀