elevener
elevener

Reputation: 1097

Executing SFTP/FTP command in libcurl without actual transfer

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

Upvotes: 3

Views: 4276

Answers (2)

user1469439
user1469439

Reputation:

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
?>

Upvotes: 2

Daniel Stenberg
Daniel Stenberg

Reputation: 58024

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

Upvotes: 5

Related Questions