!636 import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.xfer.InMemorySourceFile; import net.schmizz.sshj.xfer.scp.SCPFileTransfer; main { psvm { String hostKey = ""; String remoteDir = "/root/www-8080/speech/"; String remoteFilePath = remoteDir + "bla.php"; String host = "tinybrain.de", user = "root"; System.out.println("Uploading to: " + remoteFilePath); //System.out.print("Password for " + user + "@" + host + ": "); char[] pw = loadTextFile("input/password.txt", null).toCharArray(); final SSHClient client = new SSHClient(); client.loadKnownHosts(); if (!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); } static boolean isEmpty(String s) { return s == null || s.length() == 0; } }