2016-04-04 72 views
0

我試圖從以下教程建立一個演示鱷梨醬應用程序的SSH。我怎樣才能通過主機名作爲SSH參數在鱷梨醬

http://guac-dev.org/doc/gug/writing-you-own-guacamole-app.html

的應用程序,只要值是硬編碼的工作就好了。但我需要從用戶那裏獲得主機名/ IP。爲了實現這個目標我嘗試使用的request.getParameter()在下面的代碼:

package org.glyptodon.guacamole.net.example; 

import javax.servlet.http.HttpServletRequest; 
import org.glyptodon.guacamole.GuacamoleException; 
import org.glyptodon.guacamole.net.GuacamoleSocket; 
import org.glyptodon.guacamole.net.GuacamoleTunnel; 
import org.glyptodon.guacamole.net.InetGuacamoleSocket; 
import org.glyptodon.guacamole.net.SimpleGuacamoleTunnel; 
import org.glyptodon.guacamole.protocol.ConfiguredGuacamoleSocket; 
import org.glyptodon.guacamole.protocol.GuacamoleConfiguration; 
import org.glyptodon.guacamole.servlet.GuacamoleHTTPTunnelServlet; 

public class TutorialGuacamoleTunnelServlet 
    extends GuacamoleHTTPTunnelServlet { 

    @Override 
    protected GuacamoleTunnel doConnect(HttpServletRequest request) 
     throws GuacamoleException { 

     // Create our configuration 
     String hostname = request.getParameter("hostname"); 
     GuacamoleConfiguration config = new GuacamoleConfiguration(); 
     config.setProtocol("ssh"); 
     config.setParameter("hostname", hostname); 
     config.setParameter("port", "22"); 

     // Connect to guacd - everything is hard-coded here. 
     GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
      new InetGuacamoleSocket("localhost", 4822), 
      config 
     ); 


     // Return a new tunnel which uses the connected socket 
     return new SimpleGuacamoleTunnel(socket); 

    } 

} 

但是,當我嘗試使用它像本地主機:8080 /鱷梨教程-0.9.9主機名本地主機=,它不起作用。而如果我硬編碼相同的值,它工作得很好。 請幫我一把。

回答

2

您需要使用JavaScript將這些參數傳遞給Guacamole.Client的connect()函數。 got from here

<script type="text/javascript"> 

    // Get display div from document 
    var display = document.getElementById("display"); 

    // Instantiate client, using an HTTP tunnel for communications. 
    var guac = new Guacamole.Client(
     new Guacamole.HTTPTunnel("tunnel") 
    ); 

    // Add client to display div 
    display.appendChild(guac.getDisplay().getElement()); 

    // Error handler 
    guac.onerror = function(error) { 
     alert(error); 
     console.log(error); 
    }; 
    // Connect 
    guac.connect('ip=192.168.99.100&user=root');** set parameters here** 

    // Disconnect on close 
    window.onunload = function() { 
     guac.disconnect(); 
    } 

    </script> 

,並在您的TutorialGuacamoleTunnelServlet訪問這些作爲

config.setProtocol("ssh"); 
config.setParameter("hostname", request.getParameter("ip")); 
config.setParameter("username", request.getParameter("user")); 
+0

感謝。但它似乎並不符合我的用例。 我需要從url中獲取這些內容。我怎樣才能做到這一點。 –

+1

以及你可以使用JavaScript從url獲取數據,然後通過它們http://stackoverflow.com/a/901144/1315392 – vinay