1
多個渠道,現在有一個問題,當我嘗試在同一會話射擊第二個命令使用Jsch APIJsch - 每個會話
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配置文件中設置一些選項來支持這一個會話和多個通道?
這可能是因爲服務器的SSHD實現只是不支持多通道。這看起來更像是一個服務器問題,而不是客戶端(即JSch)。 – 2012-04-15 14:32:53
首先感謝回覆..是的,我明白它一定是服務器問題。但我如何讓服務器支持這一個會話的多個通道。我已經嘗試在服務器端的sshd_config文件上設置MaxSessions選項,因爲我已經讀過它映射到JSch API上的通道數量的地方。但是在上述情況下,這並沒有幫助。 – bhagyashree 2012-04-17 05:02:59