当我尝试使用Jsch API
在同一会话中触发第二个命令时出现问题package examples.com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class TempFile {
private static Session connectSession(String userName, String password, String host) throws Exception {
JSch.setLogger(new MyLogger());
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(userName, host, 22);
session.setPassword(password);
session.setTimeout(10000);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
}
catch (JSchException ex) {
System.out.println(ex);
}
return session;
}
private static void connectChannel(Session session, String command) throws JSchException, IOException {
System.out.println(command);
Channel channel = null;
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
InputStream in = channel.getInputStream();
channel.connect();
System.out.println("channel.isConnected() "+channel.isConnected());
setInAndOutStream(channel, in);
channel.disconnect();
System.out.println("channel.isClosed() "+channel.isClosed());
}
private static void setInAndOutStream(Channel channel, InputStream in) throws IOException, JSchException {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder outPutResult = new StringBuilder("");
int exitStatus = -100;
String output;
while (true) {
while (true) {
try {
String result = br.readLine();
if (result == null)
break;
outPutResult.append(result);
} catch (Exception ex) {
ex.printStackTrace();
break;
}
}
output = outPutResult.toString();
if (channel.isClosed()) {
exitStatus = channel.getExitStatus();
break;
}
}
System.out.println(exitStatus);
System.out.println(output);
}
private static void executeCommand(Session session, String command) throws JSchException, IOException{
connectChannel(session,command);
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("starting");
// TODO Auto-generated method stub
try {
Session session = connectSession("username", "password", "host");
if(session!=null) {
executeCommand(session, "ls");
executeCommand(session, "cp out.txt copy.txt");
session.disconnect();
}
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done..");
}
}
如果我使用Linux机器作为主机,它可以正常工作。 但是,如果我尝试连接到具有openssh的Windows Server 2008计算机,则无法执行第二个命令,即“executeCommand(session,”cp out.txt copy.txt“);”它返回状态255。
一定出了什么问题?我们是否需要在ssh配置文件中设置一些选项以支持这一个会话和多个通道??
答案 0 :(得分:0)
尝试更改
channel = session.openChannel("exec");
带
channel = session.openChannel("shell");