Executing SFTP/FTP command in libcurl without actual transfer

ぐ巨炮叔叔 提交于 2019-12-06 08:31:37

问题


Is it possible to execute SFTP command in libcurl without actual attempt of file transfer.

I set the command to execute (file deletion actually) using CURLOPT_QUOTE, but after executing the operation (and it executes successfully, file is deleted) curl is trying to upload or download file (based on CURLOPT_UPLOAD), so I get the CURLE_REMOTE_FILE_NOT_FOUND error error code.

What I tried so far - not setting the URL results in "malformed url error", setting CURLOPT_CONNECT_ONLY result in quote not executed as well. Is there any standard way?

curl_global_init(CURL_GLOBAL_DEFAULT);

char temp[2000];
_snprintf(STRPARAM(temp),"%s://%s:%d/%s","sftp",m_url,m_port,a_ftpPath);
curl_easy_setopt(m_curl, CURLOPT_URL,temp);
_snprintf(STRPARAM(temp),"%s:%s",m_name,m_pass);
curl_easy_setopt(m_curl, CURLOPT_USERPWD, temp);
curl_easy_setopt(m_curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PASSWORD);

curl_easy_setopt(m_curl, CURLOPT_UPLOAD, 0L);
curl_easy_setopt(m_curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(m_curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(m_curl, CURLOPT_HEADER, 1L);

struct curl_slist *headerlist=NULL;
_snprintf(STRPARAM(temp),"rm /%s",a_ftpPath);
headerlist = curl_slist_append(headerlist, temp);
curl_easy_setopt(m_curl, CURLOPT_QUOTE, headerlist); 

m_res = curl_easy_perform(m_curl);

curl_easy_setopt(m_curl, CURLOPT_QUOTE, NULL); 
curl_easy_setopt(m_curl, CURLOPT_HEADER, 0L);
curl_easy_setopt(m_curl, CURLOPT_NOBODY, 0L);
curl_slist_free_all (headerlist);

curl_global_cleanup();

回答1:


CURLOPT_NOBODY is the key for asking that no "body" is to get transferred.




回答2:


You could try phpseclib, a pure PHP SFTP implementation:

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

$sftp->delete('filename.remote'); // doesn't delete directories
// recursive delete
$sftp->delete('dirname.remote', true); // deletes a directory and all its contents
?>


来源:https://stackoverflow.com/questions/13509709/executing-sftp-ftp-command-in-libcurl-without-actual-transfer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!