如何将主机名作为guakamole中ssh的参数传递

时间:2016-04-04 12:02:30

标签: ssh guacamole

我尝试从下面的教程为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);

    }

}

但是当我尝试使用它像 localhost:8080 / guacamole-tutorial-0.9.9?hostname = localhost 时,它不起作用。然而,如果我对相同的值进行硬编码,它就可以正常工作。 请帮帮我。

1 个答案:

答案 0 :(得分:3)

您需要使用JavaScript将这些参数传递给Guacamole.Client的connect()函数。 got from here

<script>
  function addRow() {
      var table = document.getElementById('in_tbl_name');
      var row = document.createElement("TR");
      var td1 = document.createElement("TD")
      var strHtml1 = "<Rule Id>";
      td1.innerHTML = strHtml1.replace(/!count!/g, "count");
      var td2 = document.createElement("TD")
      var strHtml2 = "<Rule Name>";
        td2.innerHTML = strHtml2.replace(/!count!/g,count);
        row.appendChild(td1);
        row.appendChild(td2);
        table.appendChild(row);

    }
</script>

<div>Selected Plans
  <br>
  <table ID="in_tbl_name" border="1">
    <tr>
      <td>Rule Id</td>
      <td>Rule Name</td>
    </tr>
  </table>


  <form action="/SubmitRule" method="post" model="command">
    <table>
      <tr>
        <td>
          <select name="Select">
            <c:forEach items="${listRules}" var="rule">
              <option value="${rule.id}">
                <c:out value="${rule.id}" />
              </option>
            </c:forEach>
          </select>
        </td>
        <td>
          <input type="submit" onClick="addRow()" VALUE="AddRow" />
        </td>
      </tr>
    </table>
  </form>
</div>

并在您的TutorialGuacamoleTunnelServlet中访问这些

  <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>