1 18 19 package org.apache.tools.ant.taskdefs.optional.ssh; 20 21 import com.jcraft.jsch.JSchException; 22 import com.jcraft.jsch.Session; 23 import com.jcraft.jsch.JSch; 24 25 import org.apache.tools.ant.Task; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.Project; 28 29 34 public abstract class SSHBase extends Task implements LogListener { 35 36 37 private static final int SSH_PORT = 22; 38 39 private String host; 40 private String knownHosts; 41 private int port = SSH_PORT; 42 private boolean failOnError = true; 43 private boolean verbose; 44 private SSHUserInfo userInfo; 45 46 49 public SSHBase() { 50 super(); 51 userInfo = new SSHUserInfo(); 52 } 53 54 59 public void setHost(String host) { 60 this.host = host; 61 } 62 63 67 public String getHost() { 68 return host; 69 } 70 71 77 public void setFailonerror(boolean failure) { 78 failOnError = failure; 79 } 80 81 85 public boolean getFailonerror() { 86 return failOnError; 87 } 88 89 94 public void setVerbose(boolean verbose) { 95 this.verbose = verbose; 96 } 97 98 103 public boolean getVerbose() { 104 return verbose; 105 } 106 107 112 public void setUsername(String username) { 113 userInfo.setName(username); 114 } 115 116 117 122 public void setPassword(String password) { 123 userInfo.setPassword(password); 124 } 125 126 131 public void setKeyfile(String keyfile) { 132 userInfo.setKeyfile(keyfile); 133 } 134 135 140 public void setPassphrase(String passphrase) { 141 userInfo.setPassphrase(passphrase); 142 } 143 144 152 public void setKnownhosts(String knownHosts) { 153 this.knownHosts = knownHosts; 154 } 155 156 161 public void setTrust(boolean yesOrNo) { 162 userInfo.setTrust(yesOrNo); 163 } 164 165 170 public void setPort(int port) { 171 this.port = port; 172 } 173 174 178 public int getPort() { 179 return port; 180 } 181 182 187 public void init() throws BuildException { 188 super.init(); 189 this.knownHosts = System.getProperty("user.home") + "/.ssh/known_hosts"; 190 this.port = SSH_PORT; 191 } 192 193 198 protected Session openSession() throws JSchException { 199 JSch jsch = new JSch(); 200 if (null != userInfo.getKeyfile()) { 201 jsch.addIdentity(userInfo.getKeyfile()); 202 } 203 204 if (!userInfo.getTrust() && knownHosts != null) { 205 log("Using known hosts: " + knownHosts, Project.MSG_DEBUG); 206 jsch.setKnownHosts(knownHosts); 207 } 208 209 Session session = jsch.getSession(userInfo.getName(), host, port); 210 session.setUserInfo(userInfo); 211 log("Connecting to " + host + ":" + port); 212 session.connect(); 213 return session; 214 } 215 216 220 protected SSHUserInfo getUserInfo() { 221 return userInfo; 222 } 223 } 224 | Popular Tags |