1 import java.io.BufferedReader ; 2 import java.io.IOException ; 3 import java.io.InputStream ; 4 import java.io.InputStreamReader ; 5 6 import ch.ethz.ssh2.Connection; 7 import ch.ethz.ssh2.Session; 8 import ch.ethz.ssh2.StreamGobbler; 9 10 public class Basic 11 { 12 public static void main(String [] args) 13 { 14 String hostname = "127.0.0.1"; 15 String username = "joe"; 16 String password = "joespass"; 17 18 try 19 { 20 21 22 Connection conn = new Connection(hostname); 23 24 25 26 conn.connect(); 27 28 33 34 boolean isAuthenticated = conn.authenticateWithPassword(username, password); 35 36 if (isAuthenticated == false) 37 throw new IOException ("Authentication failed."); 38 39 40 41 Session sess = conn.openSession(); 42 43 sess.execCommand("uname -a && date && uptime && who"); 44 45 System.out.println("Here is some information about the remote host:"); 46 47 51 52 InputStream stdout = new StreamGobbler(sess.getStdout()); 53 54 BufferedReader br = new BufferedReader (new InputStreamReader (stdout)); 55 56 while (true) 57 { 58 String line = br.readLine(); 59 if (line == null) 60 break; 61 System.out.println(line); 62 } 63 64 65 66 System.out.println("ExitCode: " + sess.getExitStatus()); 67 68 69 70 sess.close(); 71 72 73 74 conn.close(); 75 76 } 77 catch (IOException e) 78 { 79 e.printStackTrace(System.err); 80 System.exit(2); 81 } 82 } 83 } 84 | Popular Tags |