Executing SFTP/FTP command in libcurl without actu

2020-07-22 18:01发布

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();

2条回答
我想做一个坏孩纸
2楼-- · 2020-07-22 18:15

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
?>
查看更多
相关推荐>>
3楼-- · 2020-07-22 18:36

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

查看更多
登录 后发表回答