Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 1.34 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Sending a TAB control character in Java
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4.  
  5. import ch.ethz.ssh2.Connection;
  6. import ch.ethz.ssh2.Session;
  7.  
  8.  
  9. public class Main {
  10.     public static void main(String[] args) throws Exception {
  11.         new Main().test();
  12.     }
  13.  
  14.     private Session sess;
  15.  
  16.     public void test() throws IOException, InterruptedException {
  17.         Connection c  = new Connection("myserver", 22);
  18.         c.connect();
  19.         boolean OK = c.authenticateWithPassword("user", "pass");
  20.         if (!OK) throw new IOException("Bad password");
  21.         sess = c.openSession();
  22.         sess.requestPTY("vt220");
  23.         new Thread(stdoutLogger).start();
  24.         sess.execCommand("/bin/bash");
  25.         Thread.sleep(2000);
  26.         sess.getStdin().write("echo Hellon".getBytes());
  27.         sess.getStdin().write("ls -l /tmt".getBytes());
  28.         Thread.sleep(4000);
  29.         sess.close();
  30.         c.close();
  31.     }
  32.  
  33.     Runnable stdoutLogger = new Runnable() {
  34.         public void run() {
  35.             InputStream is = sess.getStdout();
  36.             int b;
  37.             try {
  38.                 while ( (b = is.read()) != -1) {
  39.                     System.out.println("Read " + b + " - " + (char)b);
  40.                 }
  41.             } catch (IOException e) {
  42.                 e.printStackTrace();
  43.             }
  44.         }
  45.     };
  46. }
  47.        
  48. compgen -c
  49.        
  50. grep <something>