import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.common.IOUtils; import net.schmizz.sshj.connection.channel.direct.Session; public class main { static String host = "tinybrain.de"; static String user = "root"; static boolean pwAuth = true; static String command = "uptime"; public static void main(String[] args) throws Exception { final SSHClient client = new SSHClient(); client.loadKnownHosts(); client.addHostKeyVerifier("5b:fd:de:ae:5b:d7:03:d2:8a:0d:0a:b6:7d:20:d0:d4"); //"tinybrain.de"; boolean pwAuth = true; client.connect(host); try { if (pwAuth) { System.out.print("User: " + user + ". Password: "); String pw = readLineHidden(); print("*"); client.authPassword(user, pw); } else { client.authPublickey(user); } final Session session = client.startSession(); print("Started session"); try { print("Executing command: " + command); final Session.Command cmd = session.exec(command); print("Output: " + IOUtils.readFully(cmd.getInputStream()).toString()); print("Joining command"); cmd.join(10, TimeUnit.SECONDS); print("Done!"); } finally { session.close(); } } finally { client.disconnect(); } } static BufferedReader readLine_reader; static String readLineHidden() { try { if (readLine_reader == null) readLine_reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); return readLine_reader.readLine(); } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} static volatile StringBuffer local_log = new StringBuffer(); // not redirected static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot // in bytes - will cut to half that static volatile int print_log_max = 1024*1024; static volatile int local_log_max = 100*1024; static void print() { print(""); } // slightly overblown signature to return original object... static A print(A o) { String s = String.valueOf(o) + "\n"; StringBuffer loc = local_log; StringBuffer buf = print_log; int loc_max = print_log_max; if (buf != loc && buf != null) { print_append(buf, s, print_log_max); loc_max = local_log_max; } if (loc != null) print_append(loc, s, loc_max); System.out.print(s); return o; } static void print(long l) { print(String.valueOf(l)); } static void print(char c) { print(String.valueOf(c)); } static void print_append(StringBuffer buf, String s, int max) { synchronized(buf) { buf.append(s); max /= 2; if (buf.length() > max) try { int newLength = max/2; int ofs = buf.length()-newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } } } }