Created
April 2, 2025 15:38
-
-
Save ExploiTR/fd353c6c67b0ef2e289b42a27478ab01 to your computer and use it in GitHub Desktop.
C++ SFTP Upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <fstream> | |
/** | |
* Creates an SFTP batch file and executes it to upload files | |
* | |
* @param serverAddress SFTP server address | |
* @param serverPort SFTP server port | |
* @param identityFile Path to the PEM file for authentication | |
* @param filesToUpload List of files to upload | |
* @param remoteDirectory Target directory on the server | |
* @return True if upload was successful, false otherwise | |
*/ | |
bool executeSftpUpload( | |
const std::string& serverAddress, | |
int serverPort, | |
const std::string& identityFile, | |
const std::vector<std::string>& filesToUpload, | |
const std::string& remoteDirectory) { | |
// Create batch file | |
const std::string batchFilePath = "sftp_commands.txt"; | |
std::ofstream batchFile(batchFilePath); | |
if (!batchFile) { | |
std::cerr << "Failed to create batch file" << std::endl; | |
return false; | |
} | |
// Write commands to change directory and upload files | |
batchFile << "cd " << remoteDirectory << std::endl; | |
for (const auto& file : filesToUpload) { | |
batchFile << "put \"" << file << "\"" << std::endl; | |
} | |
batchFile.close(); | |
// Build SFTP command | |
std::string sftpCommand = "sftp -i \"" + identityFile + "\" -P " + std::to_string(serverPort) + | |
" -b " + batchFilePath + " -o StrictHostKeyChecking=no " + serverAddress; | |
std::cout << "Executing: " << sftpCommand << std::endl; | |
// Execute command | |
int commandResult = system(sftpCommand.c_str()); | |
// Check if command was successful | |
if (commandResult != 0) { | |
std::cout << "Failed to upload files. Trying to update known hosts..." << std::endl; | |
// Try to update known hosts | |
std::string keyscanCommand = "ssh-keyscan -p " + std::to_string(serverPort) + " " + serverAddress + | |
" >> %USERPROFILE%\\.ssh\\known_hosts"; | |
system(keyscanCommand.c_str()); | |
// Try again | |
std::cout << "Retrying upload..." << std::endl; | |
commandResult = system(sftpCommand.c_str()); | |
if (commandResult != 0) { | |
std::cerr << "Upload failed after updating known hosts" << std::endl; | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* Main function to handle the file upload process with logging | |
* | |
* @param serverAddress SFTP server address | |
* @param serverPort SFTP server port | |
* @param identityFile Path to the PEM file for authentication | |
* @param filesToUpload List of files to upload | |
* @param remoteDirectory Target directory on the server | |
* @return True if upload was successful, false otherwise | |
*/ | |
bool uploadFilesToSftpServer( | |
const std::string& serverAddress, | |
int serverPort, | |
const std::string& identityFile, | |
const std::vector<std::string>& filesToUpload, | |
const std::string& remoteDirectory) { | |
std::cout << "Starting upload to " << serverAddress << ":" << serverPort << std::endl; | |
std::cout << "Remote directory: " << remoteDirectory << std::endl; | |
std::cout << "Files to upload: " << filesToUpload.size() << std::endl; | |
bool uploadResult = executeSftpUpload( | |
serverAddress, | |
serverPort, | |
identityFile, | |
filesToUpload, | |
remoteDirectory | |
); | |
if (uploadResult) { | |
std::cout << "Files uploaded successfully" << std::endl; | |
} else { | |
std::cerr << "Failed to upload files" << std::endl; | |
} | |
return uploadResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment