source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHBase.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.8 KB
Line 
1/*
2 * Copyright 2003-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.ssh;
19
20import com.jcraft.jsch.JSchException;
21import com.jcraft.jsch.Session;
22import com.jcraft.jsch.JSch;
23
24import org.apache.tools.ant.Task;
25import org.apache.tools.ant.BuildException;
26import org.apache.tools.ant.Project;
27
28/**
29 * Base class for Ant tasks using jsch.
30 *
31 * @since Ant 1.6
32 */
33public abstract class SSHBase extends Task implements LogListener {
34
35 /** Default listen port for SSH daemon */
36 private static final int SSH_PORT = 22;
37
38 private String host;
39 private String keyfile;
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 /**
47 * Constructor for SSHBase.
48 */
49 public SSHBase() {
50 super();
51 userInfo = new SSHUserInfo();
52 }
53
54 /**
55 * Remote host, either DNS name or IP.
56 *
57 * @param host The new host value
58 */
59 public void setHost(String host) {
60 this.host = host;
61 }
62
63 public String getHost() {
64 return host;
65 }
66
67 public void setFailonerror(boolean failure) {
68 failOnError = failure;
69 }
70
71 public boolean getFailonerror() {
72 return failOnError;
73 }
74
75 /**
76 * @since Ant 1.6.2
77 */
78 public void setVerbose(boolean failure) {
79 verbose = failure;
80 }
81
82 /**
83 * @since Ant 1.6.2
84 */
85 public boolean getVerbose() {
86 return verbose;
87 }
88
89 /**
90 * Username known to remote host.
91 *
92 * @param username The new username value
93 */
94 public void setUsername(String username) {
95 userInfo.setName(username);
96 }
97
98
99 /**
100 * Sets the password for the user.
101 *
102 * @param password The new password value
103 */
104 public void setPassword(String password) {
105 userInfo.setPassword(password);
106 }
107
108 /**
109 * Sets the keyfile for the user.
110 *
111 * @param keyfile The new keyfile value
112 */
113 public void setKeyfile(String keyfile) {
114 userInfo.setKeyfile(keyfile);
115 }
116
117 /**
118 * Sets the passphrase for the users key.
119 *
120 * @param passphrase The new passphrase value
121 */
122 public void setPassphrase(String passphrase) {
123 userInfo.setPassphrase(passphrase);
124 }
125
126 /**
127 * Sets the path to the file that has the identities of
128 * all known hosts. This is used by SSH protocol to validate
129 * the identity of the host. The default is
130 * <i>${user.home}/.ssh/known_hosts</i>.
131 *
132 * @param knownHosts a path to the known hosts file.
133 */
134 public void setKnownhosts(String knownHosts) {
135 this.knownHosts = knownHosts;
136 }
137
138 /**
139 * Setting this to true trusts hosts whose identity is unknown.
140 *
141 * @param yesOrNo if true trust the identity of unknown hosts.
142 */
143 public void setTrust(boolean yesOrNo) {
144 userInfo.setTrust(yesOrNo);
145 }
146
147 /**
148 * Changes the port used to connect to the remote host.
149 *
150 * @param port port number of remote host.
151 */
152 public void setPort(int port) {
153 this.port = port;
154 }
155
156 public int getPort() {
157 return port;
158 }
159
160 public void init() throws BuildException {
161 super.init();
162 this.knownHosts = System.getProperty("user.home") + "/.ssh/known_hosts";
163 this.port = SSH_PORT;
164 }
165
166 protected Session openSession() throws JSchException {
167 JSch jsch = new JSch();
168 if (null != userInfo.getKeyfile()) {
169 jsch.addIdentity(userInfo.getKeyfile());
170 }
171
172 if (!userInfo.getTrust() && knownHosts != null) {
173 log("Using known hosts: " + knownHosts, Project.MSG_DEBUG);
174 jsch.setKnownHosts(knownHosts);
175 }
176
177 Session session = jsch.getSession(userInfo.getName(), host, port);
178 session.setUserInfo(userInfo);
179 log("Connecting to " + host + ":" + port);
180 session.connect();
181 return session;
182 }
183
184 protected SSHUserInfo getUserInfo() {
185 return userInfo;
186 }
187}
Note: See TracBrowser for help on using the repository browser.