title: Send File via libssh (ssh_scp_push_file && ssh_scp_write)
body: <p>Disclaimer: <strong>I'm googling my way through this</strong></p>
  
  <ul>
  <li>Using C++</li>
  <li>Both ends are Linux</li>
  <li>I can see the SSH connection on the far end via TCPDUMP</li>
  <li>No build errors or return codes other than 0 (debug says its all good)</li>
  <li>TCPDUMP doesn't look like its transferring enough data (it is a 2MB file*)</li>
  <li>Connect / Authenticate / Authorize are happening properly as far as I can tell </li>
  </ul>
  
  <p>When i go to the far end looking for the file that was supposedly copied over, it doesn't exist.  I've tried updatedb &amp; locate "file", but it's nowhere to be found.</p>
  
  <p>Thank you in advance for any insight possible!</p>
  
  <p>SSH Functions (auth &amp; connect) cleared out for "Minimal, Complete, and Verifiable example" since they seem to be working fine*</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fstream&gt;
  #include &lt;curl/curl.h&gt;
  #include &lt;boost/regex.hpp&gt;
  #include &lt;boost/system/error_code.hpp&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;x86_64-linux-gnu/sys/stat.h&gt;
  #define LIBSSH_STATIC 1
  #include &lt;libssh/libssh.h&gt;
  
  using namespace std;
  
  int sourcePull(void)
  {
      CURL *curl; 
      FILE *fp;
      CURLcode res;
      char *url = "https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset";
      char outfilename[FILENAME_MAX] = "fireHOL";
      curl = curl_easy_init();
      if (curl)
      {
          fp = fopen(outfilename, "wb");
          curl_easy_setopt(curl, CURLOPT_URL, url);
          curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
          curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
          res = curl_easy_perform(curl);
          curl_easy_cleanup(curl);
          fclose(fp);
      }
      return 0;
  }
  
  int sourceParse()
  {
      //Regex for IP's in sourcePull result
      ifstream infile("fireHOL");
      string ipAddress;
      boost::regex expr1("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
      boost::regex expr2("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\/([0-9]|[1-2][0-9]|3[0-2]))$");
      boost::smatch matches;
  
          while (infile &gt;&gt; ipAddress)
          {
              if (boost::regex_match(ipAddress, matches, expr1)) 
              {
                  ofstream checkpoint;
                  checkpoint.open("samp_batch.txt", ios::out | ios::app);
                  checkpoint &lt;&lt; "add -t 86400 -a d -l r -o icewall ip -s " &lt;&lt; ipAddress &lt;&lt; endl;
                  checkpoint &lt;&lt; "add -t 86400 -a r -l r -o icewall ip -d " &lt;&lt; ipAddress &lt;&lt; endl;
              }
  
              if (boost::regex_match(ipAddress, matches, expr2))
              {
                  ofstream checkpoint;
                  checkpoint.open("samp_batch.txt", ios::out | ios::app);
                  checkpoint &lt;&lt; "add -t 86400 -a d -l r -o icewall ip -s " &lt;&lt; ipAddress &lt;&lt; endl;
                  checkpoint &lt;&lt; "add -t 86400 -a r -l r -o icewall ip -d " &lt;&lt; ipAddress &lt;&lt; endl;
              }
  
          }
  
          ofstream terminate;
          terminate.open("samp_batch.txt", ios::out | ios::app);
          terminate &lt;&lt; "EOF";
  
      return 0;
  }
  
  int fileSize()
  {
      int size;
      streampos begin, end;
      ifstream inputFile("samp_batch", ios::binary);
      begin = inputFile.tellg();
      inputFile.seekg(0, ios::end);
      end = inputFile.tellg();
      inputFile.close();
      return size;
  }
  
  int ssh()
  {
      ssh_session my_ssh_session;
      ssh_scp scp;
      int port = 22;
      int rc;
      int method;
      char password[128] = { 0 };
      char *banner;
      //open session &amp; set options
      my_ssh_session = ssh_new();
      if (my_ssh_session == NULL)
          exit(-1);
      ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "XXX.XXX.XXX.XXX");
      ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &amp;port);
      ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "security");
  
      //connect to server
      rc = ssh_connect(my_ssh_session);
      if (rc != SSH_OK)
      {
          fprintf(stderr, "Error connecting to host: %s\n", ssh_get_error(my_ssh_session));
          ssh_free(my_ssh_session);
          exit(-1);
      }
  
      //verify the servers identity
      if (verify_knownHost(my_ssh_session) &lt; 0)
      {
          fprintf(stdout, "unkown host\n");
          ssh_disconnect(my_ssh_session);
          ssh_free(my_ssh_session);
          exit(-1);
      }
  
  
      // Try to authenticate
      rc = ssh_userauth_none(my_ssh_session, NULL);
      if (rc == SSH_AUTH_ERROR) {
          error(my_ssh_session);
          return rc;
      }
  
      method = ssh_auth_list(my_ssh_session);
      while (rc != SSH_AUTH_SUCCESS) {
          // Try to authenticate with public key first
          if (method &amp; SSH_AUTH_METHOD_PUBLICKEY) {
              rc = ssh_userauth_autopubkey(my_ssh_session, NULL);
              if (rc == SSH_AUTH_ERROR) {
                  error(my_ssh_session);
                  return rc;
              }
              else if (rc == SSH_AUTH_SUCCESS) {
                  break;
              }
          }
  
      return rc;
  
      //SCP samp_batch file here
      const int length = fileSize();
      const char *fileName = "samp_batch";
  
  
      ifstream inputFile(fileName);
      constexpr size_t bufferSize = 1024 * 1024 * 1024;
      unique_ptr&lt;char[]&gt; buffer(new char[bufferSize]);
      const void* cvp = &amp;buffer;
  
      rc = ssh_scp_push_file(scp, "samp_batch", length, 0777);
      if (rc != SSH_OK)
      {
          fprintf(stderr, "Can't open remote file: %s\n", ssh_get_error(my_ssh_session));
          ssh_free(my_ssh_session);
          exit(-1);
  
      }
  
      while (inputFile)
      {
  
          inputFile.read(buffer.get(), 1024 * 1024 * 1024);
  
          rc = ssh_scp_write(scp, cvp, bufferSize);
          if (rc != SSH_OK)
          {
              fprintf(stderr, "Cant write to remote file: %s\n", ssh_get_error(my_ssh_session));
              ssh_free(my_ssh_session);
              exit(-1);
  
          }
      }
  
      return SSH_OK;
  
      ////execute remote command here
  
  
      //ssh_free(my_ssh_session);
  }
  
  int main()
  {
  
      sourcePull();
      sourceParse();
      ssh();
  
  
      return 0;
  }
  </code></pre>