Java에서 SSH를 통해 원격 GNU/Linux 시스템에서 명령을 실행하는 데 문제가 있습니다. 다음 명령은 로컬 Bash에서 실행될 때 잘 동작합니다 (물론 사용자와 호스트는 다르지만 동작은 변경되지 않습니다).Java에서 SSH를 통해 Bash 명령을 실행할 때의 문제
$ ssh [email protected] 'hostname'
host
$ ssh [email protected] 'hostname -f'
host.example.com
$ ssh [email protected] "hostname -f"
host.example.com
것은 자바가 인수없이 hostname
보다 더 복잡 아무것도 실패에서 내가 생각하는 것은 동일 하.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;
public class SOPlayground {
public static void main(String[] args) throws Exception {
for (String argument : new String[]{"hostname", "'hostname'", "\"hostname\"",
"'hostname -f'", "\"hostname -f\""}) {
CommandLine commandLine = new CommandLine("ssh");
commandLine.addArgument("[email protected]");
commandLine.addArgument(argument);
System.out.println(commandLine);
final Executor executor = new DefaultExecutor();
try (ByteArrayOutputStream os = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream()) {
executor.setStreamHandler(new PumpStreamHandler(os, err));
int exitcode = executor.execute(commandLine);
System.out.println("exitcode=" + exitcode);
System.out.println(new String(os.toByteArray(), "UTF-8"));
System.err.println(new String(err.toByteArray(), "UTF-8"));
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
}
}
출력은 다음과 같습니다
ssh [email protected] hostname
exitcode=0
host
ssh [email protected] 'hostname'
exitcode=0
host
ssh [email protected] "hostname"
exitcode=0
host
ssh [email protected] 'hostname -f'
Process exited with an error: 127 (Exit value: 127)
ssh [email protected] "hostname -f"
Process exited with an error: 127 (Exit value: 127)
당신이 볼 수 있듯이, 자바에서 SSH를 통해 hostname -f
을 실행 내가 무엇 배쉬 (로컬 또는 원격)이 없습니다 궁금해 127 종료 코드로 실패 무엇을 찾으십니까? 명령.
나는 변형
addArgument(String argument, boolean handleQuoting)
을 사용하려했지만 결과의 차이는 없었다.
SSH를 통해 작동하는 Java에서 CommandLine
을 어떻게 작성해야합니까?
는 jsch (com.jcraft.jsch.JSch)을 사용해보십시오 - 그것은 내가 다른 접근 방식에 JSch을 시도했지만 공개 키 인증을 구성하는 데 실패했습니다 ssh를 –
위한 목적으로 제작되었습니다. 그것에 관한 어떤 암시라도? –
미안 해요 'JSch shell = new JSch();와 같이 username/passwd와 함께 사용했습니다. \t \t session = shell.getSession (userName, serverIP, 22); \t \t session.setPassword (password); ' –