Java 未执行使用ChannelExec的命令-Jsch
我正在使用Jsch在服务器中创建一个文件并执行一些命令。对于文件创建,它可以正常工作,但是对于命令执行,它不能正常工作。它保持状态-1(仍在工作),并永远保持状态。这种情况发生在shell执行或我试图成为root时。请遵循下面使用的方法:Java 未执行使用ChannelExec的命令-Jsch,java,ssh,jsch,Java,Ssh,Jsch,我正在使用Jsch在服务器中创建一个文件并执行一些命令。对于文件创建,它可以正常工作,但是对于命令执行,它不能正常工作。它保持状态-1(仍在工作),并永远保持状态。这种情况发生在shell执行或我试图成为root时。请遵循下面使用的方法: public void upload(String localPath) throws IOException { Session session = connectToServer(); System.out.println("In
public void upload(String localPath) throws IOException {
Session session = connectToServer();
System.out.println("In upload");
ChannelSftp channelSftp = getChannelToSftpServer(session);
//Creating file in temporary location
File f = new File(localPath);
FileInputStream fi = new FileInputStream(f);
// Creating file on server and setting the permissions to the user (chmod 777)
if (channelSftp != null) {
try {
System.out.println("Change working in temp directory");
changeWorkingDirectory(channelSftp, TEMP_PATH);
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
//THE PROBLEM ALSO HAPPENS WHEN EXECUTING A SHELL WITH THIS COMMAND INSIDE
channelExec.setCommand(
"root command (using pbrun) <command is here, confidential> ");
InputStream commandOutput = channelExec.getInputStream();
channelExec.connect();
StringBuilder outputBuffer = new StringBuilder();
int readByte = commandOutput.read();
while(readByte != 0xffffffff)
{
outputBuffer.append((char)readByte);
readByte = commandOutput.read();
System.out.println(outputBuffer);
}
System.out.println("Root connected.");
channelExec.disconnect();
channelSftp.put(fi, f.getName());
channelSftp.chmod(0777, localPath);
channelSftp.chown(123, localPath);
channelSftp.chgrp(123, localPath);
System.out.println("File configurations changed.");
//Copying to the official path
channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("mv /tmp/"+f.getName()+" "+path);
channelExec.connect();
System.out.println("File is completed and ready!");
while (channelExec.getExitStatus() == -1) {
Thread.sleep(1000);
}
channelExec.disconnect();
} catch (SftpException e) {
e.printStackTrace();
throw new IOException(e.getStackTrace() + "");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
disconnectChanneltoSftpServer(channelSftp);
session.disconnect();
fi.close();
// Deletes the local File.
f.delete();
}
}
}
public void upload(字符串localPath)引发IOException{
会话=connectToServer();
System.out.println(“上传中”);
ChannelSftp ChannelSftp=getChannelToSftpServer(会话);
//在临时位置创建文件
文件f=新文件(localPath);
FileInputStream fi=新的FileInputStream(f);
//在服务器上创建文件并设置用户权限(chmod 777)
if(channelSftp!=null){
试一试{
System.out.println(“在临时目录中更改工作”);
变更工作目录(channelSftp,临时路径);
ChannelExec ChannelExec=(ChannelExec)session.openChannel(“exec”);
//在内部使用此命令执行SHELL时也会出现此问题
channelExec.setCommand(
“根命令(使用pbrun)”;
InputStream commandOutput=channelExec.getInputStream();
channelExec.connect();
StringBuilder outputBuffer=新的StringBuilder();
int readByte=commandOutput.read();
while(readByte!=0xffffffff)
{
append((char)readByte);
readByte=commandOutput.read();
System.out.println(输出缓冲区);
}
System.out.println(“根连接”);
channelExec.disconnect();
channelSftp.put(fi,f.getName());
channelSftp.chmod(0777,localPath);
channelSftp.chown(123,localPath);
channelSftp.chgrp(123,localPath);
System.out.println(“文件配置已更改”);
//复制到官方路径
channelExec=(channelExec)session.openChannel(“exec”);
channelExec.setCommand(“mv/tmp/”+f.getName()+“”+path);
channelExec.connect();
System.out.println(“文件已完成并准备就绪!”);
while(channelExec.getExitStatus()==-1){
睡眠(1000);
}
channelExec.disconnect();
}捕获(SFTPE例外){
e、 printStackTrace();
抛出新的IOException(例如getStackTrace()+);
}捕获(JSCHEException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}最后{
断开ChannelToSftpServer(channelSftp)的连接;
session.disconnect();
fi.close();
//删除本地文件。
f、 删除();
}
}
}
我做错了什么?提前谢谢。在调用
connect()
之前,您必须先调用getInputStream()
实际上,您最好同时阅读stderr和stdout以获取错误。关于这一点,请参见我的答案