001package ssh; 002 003import grails.plugin.remotessh.SshConfig; 004 005import java.io.BufferedReader; 006import java.io.File; 007import java.io.IOException; 008import java.io.InputStream; 009import java.io.InputStreamReader; 010 011import ch.ethz.ssh2.Connection; 012import ch.ethz.ssh2.Session; 013import ch.ethz.ssh2.StreamGobbler; 014 015public class RemoteSSH { 016 String host = ""; 017 String user = ""; 018 String sudo = ""; 019 Integer port=0; 020 String userpass=""; 021 String usercommand = ""; 022 String filter = ""; 023 024 StringBuilder output = new StringBuilder(); 025 026 public RemoteSSH(String host, String usercommand) { 027 this.host = host; 028 this.usercommand = usercommand; 029 } 030 public RemoteSSH(String host, String sudo, String usercommand) { 031 this.host = host; 032 this.sudo = sudo; 033 this.usercommand = usercommand; 034 } 035 036 public RemoteSSH(String host, String user, String sudo, String usercommand) { 037 this.host = host; 038 this.user = user; 039 this.sudo = sudo; 040 this.usercommand = usercommand; 041 } 042 public RemoteSSH(String host, String user, String sudo, String usercommand,String filter) { 043 this.host = host; 044 this.user = user; 045 this.sudo = sudo; 046 this.usercommand = usercommand; 047 this.filter = filter; 048 } 049 public RemoteSSH(String host, String user, String sudo, String usercommand, Integer port) { 050 this.host = host; 051 this.user = user; 052 this.sudo = sudo; 053 this.usercommand = usercommand; 054 this.port=port; 055 } 056 public RemoteSSH(String host, String user, String sudo, String usercommand,String filter, Integer port) { 057 this.host = host; 058 this.user = user; 059 this.sudo = sudo; 060 this.usercommand = usercommand; 061 this.filter = filter; 062 this.port=port; 063 } 064 public RemoteSSH(String host, String user, String userpass, String sudo, String usercommand,String filter,Integer port) { 065 this.host = host; 066 this.user = user; 067 this.userpass=userpass; 068 this.sudo = sudo; 069 this.usercommand = usercommand; 070 this.filter = filter; 071 this.port=port; 072 } 073 074 public String Result(SshConfig ac) throws InterruptedException, IOException { 075 Object sshuser=ac.getConfig("USER"); 076 Object sshpass=ac.getConfig("PASS"); 077 Object sshkey=ac.getConfig("KEY"); 078 Object sshkeypass=ac.getConfig("KEYPASS"); 079 Object sshport=ac.getConfig("PORT"); 080 //System.out.println("----"+sshuser.toString()); 081 if (user.equals("")) { 082 user = sshuser.toString(); 083 } 084 if (userpass.equals("")) { 085 userpass = sshpass.toString(); 086 } 087 if (port==0) { 088 String sps=sshport.toString(); 089 if (sps.matches("[0-9]+")) { 090 port=Integer.parseInt(sps); 091 } 092 } 093 String hostname = host; 094 String username = user; 095 File keyfile = new File(sshkey.toString()); 096 String keyfilePass = sshkeypass.toString(); 097 if (port==0){port=22; } 098 Connection conn = new Connection(hostname,port); 099 /* Now connect */ 100 conn.connect(); 101 /* Authenticate */ 102 boolean isAuthenticated=false; 103 if (userpass.equals("")) { 104 isAuthenticated = conn.authenticateWithPublicKey(username, 105 keyfile, keyfilePass); 106 }else{ 107 isAuthenticated = conn.authenticateWithPassword(username,userpass); 108 } 109 110 if (isAuthenticated == false) 111 throw new IOException("Authentication failed."); 112 /* Create a session */ 113 Session sess = conn.openSession(); 114 sess.requestPTY("vt220"); 115 if (sudo.equals("sudo")) { 116 sess.execCommand("sudo bash"); 117 // sess.execCommand("sudo bash"); 118 } else { 119 sess.execCommand("/bin/bash"); 120 } 121 Thread.sleep(10); 122 usercommand = usercommand + "\n"; 123 sess.getStdin().write(usercommand.getBytes()); 124 sess.getStdin().write("exit\n".getBytes()); 125 sess.getStdin().write("exit\n".getBytes()); 126 Thread.sleep(10); 127 InputStream stdout = new StreamGobbler(sess.getStdout()); 128 BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); 129 // output.append("Remote execution of "+usercommand+" returned:<br>"); 130 while (true) { 131 String line = br.readLine(); 132 if (line == null) 133 break; 134 if (filter.equals("")) { 135 output.append(line).append("<br>"); 136 } else { 137 if (line.startsWith(filter)) { 138 output.append(line).append("<br>"); 139 } 140 } 141 } 142 /* Close this session */ 143 sess.close(); 144 /* Close the connection */ 145 conn.close(); 146 return output.toString(); 147 } 148}