如何在鳄梨酱中将主机名作为 ssh 的参数传递
How can I pass hostname as a parameter for ssh in guacamole
我尝试通过以下教程为 ssh 构建一个演示鳄梨酱应用程序。
http://guac-dev.org/doc/gug/writing-you-own-guacamole-app.html
只要值是硬编码的,该应用程序就可以正常运行。但是我需要从用户那里得到 hostname/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 那样使用它时,它不起作用。而如果我硬编码相同的值,它就可以正常工作。
请帮帮我。
您需要使用 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"));
我尝试通过以下教程为 ssh 构建一个演示鳄梨酱应用程序。
http://guac-dev.org/doc/gug/writing-you-own-guacamole-app.html
只要值是硬编码的,该应用程序就可以正常运行。但是我需要从用户那里得到 hostname/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 那样使用它时,它不起作用。而如果我硬编码相同的值,它就可以正常工作。 请帮帮我。
您需要使用 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"));