问题描述
我已经使用基于Java的JSch库为SSH shell实现了这个简单程序。
程序的工作方式是,首先创建SSHShellBasic实例,然后为该实例运行passwordConnectSSH。成功建立连接后,我将以字符串形式将和命令发送到SSHExec,如下所示:
shell1.SSHExec(pwd); //where shell1 is the current instance of my SSH shell
现在,这对于pwd
和ls
之类的命令非常有用。但是,我无法cd
进入任何文件夹或mkdir
。我不明白为什么。另外,我也没有从shell中得到错误。这是因为我不接受STDERR
吗?如果是这样,我该如何实现?
这是我的代码供参考:
import com.jcraft.jsch.*;
import java.io.InputStream;
public class SSHShellBasic extends Thread
{
String hostname,username,password;
int portNumber;
TextArea textArea_terminalOut;
protected String terminalOut;
Session session;
public SSHShellBasic(String hostname,String username,String password,String portNumber,TextArea textArea_terminalOut)
{
this.hostname = hostname;
this.username = username;
this.password = password;
this.portNumber = Integer.parseInt(portNumber);
this.textArea_terminalOut = textArea_terminalOut;
this.terminalOut = "";
}
public boolean passwordConnectSSH()
{
try
{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking","no");
JSch jsch = new JSch();
session = jsch.getSession(username,hostname,portNumber);
session.setPassword(password);
session.setConfig(config);
session.connect();
//System.out.println("Connected");
} catch (Exception e)
{
return false;
}
return true;
}
public void endSession ()
{
session.disconnect();
System.out.println("\n--------------------\nSession terminated.");
}
public void SSHExec(String command)
{
if (command.charat(0) == '\n')
{
command = command.substring(1);
}
try
{
if (command.equals("terminate"))
{
endSession();
return;
}
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true)
{
while (in.available() > 0)
{
int i = in.read(tmp,1024);
if (i < 0)
break;
String output = new String(tmp,i);
System.out.println(output);
}
if (channel.isClosed())
{
break;
}
}
channel.disconnect();
} catch (Exception e)
{
e.printstacktrace();
}
}
}
编辑:
问题是JSch的exec在独立实例中运行命令。因此,它不会记住上一个命令,因此总是在连接的服务器的默认主目录中运行每个命令/脚本。
由于我的程序主要用于执行基本的Shell操作,因此我需要的关键功能是在所选目录中,所以我先在不同的命令中依次输入“ cd”,“ ls”或“ mkdir”。
为此,我这样做是为了使在通道中发送的第一个命令像这样附加一个pwd:
compositeCommand = command.concat("; pwd");
现在我有了工作目录,可以cd
进入我的工作目录以用于后续命令:
if (!pwd.equals(""))
{
compositeCommand = "cd ".concat(pwd).concat("; ").concat(oldComposite);
}
无论何时执行cd
操作,pwd
变量也会被更新:
if (command.indexOf("cd ") != -1)
{
compositeCommand = command.concat("; pwd");
getPwd = true;
}
else
{
compositeCommand = command;
}
这是我的解决方法,可以满足我的要求。
如果您知道一个更好的方法(我不使用JSch shell,因为很难将它与JavaFX GUI集成)或任何问题,那么我们将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)