Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

64
LINES

< > BotCompany Repo | #2000401 // TinyBrainSCPUploader.java

New Tinybrain snippet

package ma.ma01;

import drjava.util.StringUtil;
import net.luaos.tb.tb07.PasswordUtil;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.xfer.InMemorySourceFile;
import net.schmizz.sshj.xfer.scp.SCPFileTransfer;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TinyBrainSCPUploader {
  public static void uploadIndexPHP(String hostKey, String url, String text) throws IOException {
    Matcher matcher = Pattern.compile("http://tinybrain.de:8080/(.*/)").matcher(url);
    if (!matcher.matches())
      throw new RuntimeException("URL not matched: " + url);

    String remoteDir = "/root/www-8080/" + matcher.group(1);
    String remoteFilePath = remoteDir + "index.php";
    String host = "tinybrain.de", user = "root";

    System.out.println("Uploading to: " + remoteFilePath);
    System.out.print("Password for " + user + "@" + host + ": ");
    char[] pw = PasswordUtil.readPasswordFromConsole();

    final SSHClient client = new SSHClient();
    client.loadKnownHosts();
    if (!StringUtil.isEmpty(hostKey))
      client.addHostKeyVerifier(hostKey);
    client.connect(host);
    try {
      client.authPassword(user, pw);
      client.newSFTPClient().mkdirs(remoteDir);
      uploadBySCP(client, remoteFilePath, text);
      System.out.println("File copied!");
    } finally {
      client.disconnect();
    }
  }

  private static void uploadBySCP(SSHClient client, String remoteFilePath, String text) throws IOException {
    uploadBySCP(client, remoteFilePath, text.getBytes("UTF-8"));
  }

  private static void uploadBySCP(SSHClient client, String remoteFilePath, final byte[] data) throws IOException {
    SCPFileTransfer fileTransfer = client.newSCPFileTransfer();
    fileTransfer.upload(new InMemorySourceFile() {
      public String getName() {
        return "index.php";
      }

      public long getLength() {
        return data.length;
      }

      public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(data);
      }
    }, remoteFilePath);
  }
}

download  show line numbers   

Snippet is not live.

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
71 stefan for reference on how to use the scp client 2015-06-29 20:04:36

add comment

Snippet ID: #2000401
Snippet name: TinyBrainSCPUploader.java
Eternal ID of this version: #2000401/1
Text MD5: 2315588924c612094b76442f43a4d5fa
Author: stefan
Category: javax
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-06-29 20:04:09
Source code size: 2217 bytes / 64 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1048 / 139
Referenced in: [show references]