Hello,
I am currently using Jsch to run a simple xclock through a SSH connection
(localhost or remote machine).
(I need to run more complex application, but this is a simple example).
I wrote the following program but I got a "Can't open display: localhost:10.0"
error.
Typing the same command within an xterm allows me to show the xclock displaying.
Can you tell me what is wrong ? I expect to see an xclock running.
I am using public key authentification.
Here is the code :
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SSHDebugJsch {
public static void main(String[] args) {
String scriptCommand = "xclock";
String host = "machineA";
int port = 22;
String username = "myusername";
JSch jsch = null;
try {
jsch = new JSch();
String home = System.getenv("HOME");
File dsaKeyFile = new File(home + "/.ssh/id_dsa");
File rsaKeyFile = new File(home + "/.ssh/id_rsa");
File known_hosts = new File(home + "/.ssh/known_hosts");
if (dsaKeyFile.exists()) {
jsch.addIdentity(dsaKeyFile.getAbsolutePath());
}
else if (rsaKeyFile.exists()) {
jsch.addIdentity(rsaKeyFile.getAbsolutePath());
}
else {
JOptionPane.showMessageDialog(null, "Please check your SSH
configuration.");
}
if (known_hosts.exists()) {
jsch.setKnownHosts(known_hosts.getAbsolutePath());
}
String[] lines = null;
Session session = jsch.getSession(username, host, port);
session.setX11Host("localhost");
session.setX11Port(6010);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("exec");
ChannelExec channelExec = (ChannelExec) channel;
channelExec.setCommand(scriptCommand);
channel.setXForwarding(true);
InputStream in = channel.getInputStream();
InputStream err = channel.getExtInputStream();
StringBuffer sbIn = new StringBuffer();
StringBuffer sbErr = new StringBuffer();
MyReadStreamInTextThread readThreadIn = new
MyReadStreamInTextThread(in, sbIn);
readThreadIn.setName("Input Thread");
MyReadStreamInTextThread readThreadErr = new
MyReadStreamInTextThread(err, sbErr);
readThreadErr.setName("Error Thread");
readThreadIn.start();
readThreadErr.start();
channel.connect();
while (readThreadIn.isFinished() == false && readThreadErr.isFinished()
== false) {
Thread.sleep(100);
}
channel.disconnect();
session.disconnect();
sbIn.append(sbErr);
lines = sbIn.toString().split("[\n\r]+");
}
catch (UnknownHostException e) {
String[] lines = new String[] { "Error : The host " + host + " can not
be found." };
}
catch (ConnectException e) {
String[] lines = new String[] { "Error : Connection refused while
trying to connect to the host " + host + " on the port " + port };
}
catch (IOException ioe) {
String[] lines = new String[] { "Error : Unable to find file
$HOME/.ssh/id_dsa", "Please check your configuration for SSH access." };
}
catch (Exception e) {
e.printStackTrace();
}
}
static class MyReadStreamInTextThread extends Thread {
boolean finished = false;
InputStream stream;
StringBuffer textToBuild;
public MyReadStreamInTextThread(InputStream stream, StringBuffer
textToBuild) {
this.stream = stream;
this.textToBuild = textToBuild;
}
@Override
public void run() {
byte buffer[] = new byte[255];
int read;
try {
while ((read = stream.read(buffer)) > 0) {
textToBuild.append(new String(buffer, 0, read));
}
finished = true;
}
catch (IOException ignoreMe) {}
}
public boolean isFinished() {
return finished;
}
}
}
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users