Global.helma.Ssh (server, hosts)
constructor

Sourcecode in /home/hannes/helma/modules/helma/Ssh.js:
30:   helma.Ssh = function(server, hosts) {
31:       var SSHPKG = Packages.ch.ethz.ssh2;
32:       var SSHPKGNAME = "ganymed-ssh2.jar";
33:       var SSHPKGURL = "http://www.ganymed.ethz.ch/ssh2";
34:       var className = "helma.Ssh";
35:       var paranoid = false;
36:       var verifier = null;
37:       var knownHosts, connection;
38:   
39:      try {
40:          knownHosts = new SSHPKG.KnownHosts();
41:          connection = new SSHPKG.Connection(server);
42:      } catch (e) {
43:          if (e instanceof TypeError == false)
44:              throw(e);
45:          throw("helma.Ssh needs " + SSHPKGNAME + 
46:                " in lib/ext or application directory " +
47:                "[" + SSHPKGURL + "]");
48:      }
49:      
50:      var SimpleVerifier = {
51:          verifyServerHostKey: function(hostname, port, serverHostKeyAlgorithm, serverHostKey) {
52:              var result = knownHosts.verifyHostkey(hostname, serverHostKeyAlgorithm, serverHostKey);
53:              switch (result) {
54:                  case SSHPKG.KnownHosts.HOSTKEY_IS_OK:
55:                      debug("verifyServerHostKey", "received known host key, proceeding");
56:                      return true;
57:                  case SSHPKG.KnownHosts.HOSTKEY_IS_NEW:
58:                      if (paranoid == true) {
59:                          debug("verifyServerHostKey", "received unknown host key, rejecting");
60:                          return false;
61:                      } else {
62:                          debug("verifyServerHostKey", "received new host key, adding temporarily to known hosts");
63:                          var hn = java.lang.reflect.Array.newInstance(java.lang.String, 1);
64:                          hn[0] = hostname;
65:                          knownHosts.addHostkey(hn, serverHostKeyAlgorithm, serverHostKey);
66:                          return true;
67:                      }
68:                  case SSHPKG.KnownHosts.HOSTKEY_HAS_CHANGED:
69:                      debug("verifyServerHostKey", "WARNING: host key has changed, rejecting");
70:                  default:
71:                      return false;
72:              }
73:              return;
74:          }
75:      };
76:      
77:      /**
78:       * private method for creating an instance of
79:       * java.io.File based on various argument types
80:       * @param a String or an instance of helma.File or java.io.File
81:       */
82:      var getFile = function(arg) {
83:          if (arg instanceof helma.File) {
84:              return new java.io.File(arg.getAbsolutePath());
85:          } else if (arg instanceof java.io.File) {
86:              return arg;
87:          } else if (arg.constructor == String) {
88:              return new java.io.File(arg);
89:          }
90:          return null;
91:      };
92:  
93:      /**
94:       * private method that connects to the remote server
95:       * @return Boolean
96:       */
97:      var connect = function() {
98:          try {
99:              var info = connection.connect(verifier);
100:              debug("connect", "connected to server " + server);
101:              return true;
102:          } catch (e) {
103:              error("connect", "connection to " + server + " failed.");
104:          }
105:          return false;
106:      };
107:      
108:      /**
109:       * debug output method
110:       * @param String name of method
111:       * @param String debug message
112:       */
113:      var debug = function(methodName, msg) {
114:          var msg = msg ? " " + msg : "";
115:          app.debug(className + ":" + methodName + msg);
116:          return;
117:      };
118:  
119:      /**
120:       * error output method
121:       * @param String name of method
122:       * @param String error message
123:       */
124:      var error = function(methodName, msg) {
125:          var tx = java.lang.Thread.currentThread();
126:          tx.dumpStack();
127:          app.log("Error in " + className + ":" + methodName + ": " + msg);
128:          return;
129:     };
130: 
131:     /**
132:      * reads a known hosts file
133:      * @param Object String or instance of helma.File or java.io.File
134:      */
135:     this.addKnownHosts = function(file) {
136:         try {
137:             knownHosts.addHostkeys(getFile(file));
138:             verifier = new SSHPKG.ServerHostKeyVerifier(SimpleVerifier);
139:             return true;
140:         } catch (e) {
141:             error("addKnownHosts", "Missing or invalid known hosts file '" + file + "'");
142:         }
143:         return false;
144:     };
145:     
146:     /**
147:      * connects to a remote host with username/password authentication
148:      * @param String username
149:      * @param String password
150:      * @return Boolean
151:      */
152:     this.connect = function(username, password) {
153:         if (!username || !password) {
154:             error("connect", "Insufficient arguments.");
155:         } else if (connect() && connection.authenticateWithPassword(username, password)) {
156:             debug("connect", "authenticated using password");
157:             return true;
158:         } else {
159:             error("connect", "Authentication failed!");
160:         }
161:         return false;
162:     };
163: 
164:     /**
165:      * connects to a remote host using private key and passphrase
166:      * @param String username
167:      * @param String path to keyfile
168:      * @param String passphrase (if needed by key)
169:      * @return Boolean
170:      */
171:     this.connectWithKey = function(username, key, passphrase) {
172:         var keyFile;
173:         if (!username || !(keyFile = getFile(key))) {
174:             error("connectWithKey", "Insufficient or wrong arguments.");
175:         } else if (connect() && connection.authenticateWithPublicKey(username, keyFile, passphrase)) {
176:             debug("connectWithKey", "authenticated with key");
177:             return true;
178:         } else {
179:             error("connectWithKey", "Authentication failed!");
180:         }
181:         return false;
182:     };
183:     
184:     /**
185:      * disconnect a session
186:      */
187:     this.disconnect = function() {
188:         connection.close();
189:         debug("disconnect", "disconnected from server " + server);
190:         return;
191:     };
192: 
193:     /**
194:      * returns true if the ssh client is connected
195:      * @return Boolean
196:      */
197:     this.isConnected = function() {
198:         return (connection != null && connection.isAuthenticationComplete());
199:     };
200: 
201:     /**
202:      * copies a file to the remote server
203:      * @param String path to local file that should be copied
204:      *                    to remote server. If the argument is a
205:      *                    String Array, all files specified in the
206:      *                    Array are copied to the server
207:      * @param String path to remote destination directory
208:      * @param String (optional) 4-digit permission mode string (eg. "0755");
209:      * @return Boolean
210:      */
211:     this.put = function(localFile, remoteDir, mode) {
212:         if (!localFile || !remoteDir) {
213:             error("put", "Insufficient arguments.");
214:         } else if (!this.isConnected()) {
215:             error("put", "Not connected. Please establish a connection first.");
216:         } else {
217:             try {
218:                 var scp = connection.createSCPClient();
219:                 if (mode != null)
220:                     scp.put(localFile, remoteDir, mode);
221:                 else
222:                     scp.put(localFile, remoteDir);
223:                 debug("put", "copied '" + localFile + "' to '" + remoteDir + "'");
224:                 return true;
225:             } catch (e) {
226:                 error("put", e);
227:             }
228:         }
229:         return false;
230:     };
231: 
232:     /**
233:      * copies a file from the remote server.
234:      * @param String path to remote file that should be copied
235:      *                    to remote server. If the argument is a
236:      *                    String Array, all files specified in the
237:      *                    Array are copied from the remote server
238:      * @param String path to local destination directory
239:      * @return Boolean
240:      */
241:     this.get = function(remoteFile, targetDir) {
242:         if (!remoteFile || !targetDir) {
243:             error("get", "Insufficient arguments.");
244:         } else if (!this.isConnected()) {
245:             error("get", "Not connected. Please establish a connection first.");
246:         } else {
247:             try {
248:                 var scp = connection.createSCPClient();
249:                 scp.get(remoteFile, targetDir);
250:                 debug("get", "copied '" + remoteFile + "' to '" + targetDir + "'");
251:                 return true;
252:             } catch (e) {
253:                 error("get", e);
254:             }
255:         }
256:         return false;
257:     };
258: 
259:     /**
260:      * executes a command on the remote server
261:      * (scp must be in PATH on the remote server)
262:      * @param String the command to execute
263:      * @return String result of the command
264:      */
265:     this.execCommand = function(cmd) {
266:         if (!this.isConnected()) {
267:             error("execCommand", "Not connected. Please establish a connection first.");
268:         } else {
269:             var session = connection.openSession();
270:             try {
271:                 session.execCommand(cmd);
272:                 var stdout = new SSHPKG.StreamGobbler(session.getStdout());
273:                 var br = new java.io.BufferedReader(new java.io.InputStreamReader(stdout));
274:                 res.push();
275:     			while (true) {
276:     				if (!(line = br.readLine()))
277:     					break;
278:     				res.writeln(line);
279:     			}
280:                 debug("execCommand", "executed command '" + cmd + "'");
281:                 return res.pop();
282:             } catch (e) {
283:                 error("execCommand", e);
284:             } finally {
285:                 session.close();
286:             }
287:         }
288:     };
289:     
290:     /**
291:      * toggle paranoia mode
292:      * @param Boolean
293:      */
294:     this.setParanoid = function(p) {
295:         paranoid = (p == true) ? true : false;
296:         return;
297:     };
298: 
299:     /**
300:      * returns true if in paranoid mode
301:      * @return Boolean
302:      */
303:     this.isParanoid = function() {
304:         return paranoid;
305:     };
306:     
307:     /**
308:      * main constructor body
309:      */
310:     if (hosts) {
311:         this.addKnownHosts(hosts);
312:     }
313: 
314:     for (var i in this)
315:         this.dontEnum(i);
316:     return this;
317: }