Make SFTP class more C++ like
This commit is contained in:
parent
f853f01481
commit
9c2ad13d03
@ -5,6 +5,7 @@
|
|||||||
#define TYRO_COMMON_H
|
#define TYRO_COMMON_H
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -16,8 +16,30 @@ SFTP::~SFTP() {
|
|||||||
freeaddrinfo(&host_info);
|
freeaddrinfo(&host_info);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
string SFTP::getFingerprint()
|
||||||
|
{
|
||||||
|
// Get the host fingerprint to check against known hosts
|
||||||
|
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
||||||
|
|
||||||
|
cerr << "Fingerprint: ";
|
||||||
|
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
||||||
|
}
|
||||||
|
cerr << endl;
|
||||||
|
|
||||||
|
return (string)fingerprint;
|
||||||
|
}
|
||||||
|
|
||||||
string SFTP::getFile(const char *path)
|
string SFTP::getFile(const char *path)
|
||||||
{
|
{
|
||||||
|
if ( ! fingerprint)
|
||||||
|
{
|
||||||
|
cerr << "Check fingerprint before grabbing files!" << endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
string output = "";
|
string output = "";
|
||||||
|
|
||||||
LIBSSH2_SFTP_HANDLE *sftp_handle;
|
LIBSSH2_SFTP_HANDLE *sftp_handle;
|
||||||
@ -25,7 +47,8 @@ string SFTP::getFile(const char *path)
|
|||||||
|
|
||||||
if ( ! sftp_handle)
|
if ( ! sftp_handle)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Unable to open file with SFTP: %ld\n", libssh2_sftp_last_error(sftp_session));
|
cerr << "Unable to open file with SFTP: ";
|
||||||
|
cerr << libssh2_sftp_last_error(sftp_session) << endl;
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +83,7 @@ void SFTP::ssh_connect(const char *host, const char *user, const char *pass, con
|
|||||||
|
|
||||||
if (status != 0)
|
if (status != 0)
|
||||||
{
|
{
|
||||||
cout << "getaddrinfo error" << gai_strerror(status) << endl;
|
cerr << "getaddrinfo error" << gai_strerror(status) << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start libssh2
|
// Start libssh2
|
||||||
@ -68,7 +91,7 @@ void SFTP::ssh_connect(const char *host, const char *user, const char *pass, con
|
|||||||
|
|
||||||
if (rc != 0)
|
if (rc != 0)
|
||||||
{
|
{
|
||||||
cout << "Libssh2 initialization failed " << "(" << rc << ")" << endl;
|
cerr << "Libssh2 initialization failed " << "(" << rc << ")" << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +99,7 @@ void SFTP::ssh_connect(const char *host, const char *user, const char *pass, con
|
|||||||
|
|
||||||
if (connect(sock, host_info_list->ai_addr, host_info_list->ai_addrlen) != 0)
|
if (connect(sock, host_info_list->ai_addr, host_info_list->ai_addrlen) != 0)
|
||||||
{
|
{
|
||||||
cout << "Failed to connect to socket." << endl;
|
cerr << "Failed to connect to socket." << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,22 +117,12 @@ void SFTP::ssh_connect(const char *host, const char *user, const char *pass, con
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the host fingerprint to check against known hosts
|
|
||||||
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
|
|
||||||
|
|
||||||
fprintf(stderr, "Fingerprint: ");
|
|
||||||
for (int i = 0; i < 20; i++)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
|
|
||||||
}
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
|
|
||||||
//@TODO do something with the handling of known hosts
|
//@TODO do something with the handling of known hosts
|
||||||
|
|
||||||
// Authenticate (by password)
|
// Authenticate (by password)
|
||||||
if (libssh2_userauth_password(session, user, pass))
|
if (libssh2_userauth_password(session, user, pass))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Authentication by password failed.\n");
|
cerr << "Authentication by password failed." << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,7 +133,7 @@ void SFTP::sftp_connect()
|
|||||||
|
|
||||||
if ( ! sftp_session)
|
if ( ! sftp_session)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Unable to start SFTP session \n");
|
cerr << "Unable to start SFTP session" << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ class SFTP {
|
|||||||
public:
|
public:
|
||||||
SFTP(const char *host, const char *user, const char *pass, const char *port="22");
|
SFTP(const char *host, const char *user, const char *pass, const char *port="22");
|
||||||
~SFTP();
|
~SFTP();
|
||||||
|
string getFingerprint();
|
||||||
string getFile(const char *path);
|
string getFile(const char *path);
|
||||||
private:
|
private:
|
||||||
struct addrinfo host_info;
|
struct addrinfo host_info;
|
||||||
|
@ -4,17 +4,18 @@
|
|||||||
|
|
||||||
|
|
||||||
TEST_CASE("ssh connections work", "[SFTP]") {
|
TEST_CASE("ssh connections work", "[SFTP]") {
|
||||||
SFTP *sftp = new SFTP("timshomepage.net", "test", "testpassword");
|
SFTP *sftp = new SFTP("shell.xshellz.com", "twarren", "of0fmk2dsj");
|
||||||
|
sftp->getFingerprint();
|
||||||
|
|
||||||
SECTION("sftp object is not null") {
|
SECTION("sftp object is not null") {
|
||||||
REQUIRE(sftp != NULL);
|
REQUIRE(sftp != NULL);
|
||||||
}
|
}
|
||||||
/*SECTION("can retreive a file") {
|
SECTION("can retreive a file") {
|
||||||
string file;
|
string file;
|
||||||
file = sftp->getFile("~/test.txt");
|
file = sftp->getFile("test.txt");
|
||||||
REQUIRE(file != "");
|
REQUIRE(file != "");
|
||||||
REQUIRE(file == "SFTP works!");
|
REQUIRE(file == "SFTP works!\n");
|
||||||
}*/
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user