때로는 코드를 사용하여 Linux 명령 실행을 제어하여 특정 기능을 구현해야 할 수도 있습니다.
이러한 유형의 문제의 경우 JSCH를 사용하여 구현할 수 있습니다. 특정 코드는 다음과 같습니다.
공개 클래스 cogradientimgfileManager {private static final logger log = loggerfactory.getLogger (CogradientImgFileManager.class); private static channelexec channelexec; private static session = null; private static int timeout = 60000; // test code public static void main (string [] args) {try {versousshutil ( "10.8.12.189", "jmuser", "root1234", 22); runcmd ( "Java -version", "utf-8");} catch (예외 e) {//*}}}}}}}}} {retated retrated blocke.printated. 원격 서버* @param 호스트 IP 주소* @param username 로그인 이름* @param 비밀번호 암호* @param port port* @throws Exception*/public static void vershutil (문자열 호스트, 문자열 사용자 이름, 문자열 암호, int port) 예외 {log.info ( " + host +", username + ", porter +", porter + ", ports +" port); jsch jsch = new jsch (); // jsch 객체 세션 생성 = jsch.getSession (사용자 이름, 호스트, 포트); // 세션 객체 세션을 가져옵니다. // 비밀번호 속성 설정 config = new Properties (); config.put ( "StricThostKeyChecking", "no"); session.setConfig (config); // propertiessession을 설정합니다 .settimeout (timeout); // set timeout session.connect (); // 링크 생성 세션}/*** 원격 서버에서 명령을 실행하십시오* @param cmd 명령 문자열* @param charset encocoding* @throws exception*/public static void runcmd (문자열 cmd, String charset) 예외 {channelexec = (channelexec). session.openchannel ( "exec"); ChannelExec.setCommand (cmd); channelExec.setInputStream (null); channelExec.seterRstream (system.err); channelExec.connect (); inputStream in = channelExec.getInexec.getInputStream (bufferedReader = new bucberedReader charset.forname (charset))); String buf = null; while ((buf = reader.readline ())! = null) {system.out.println (buf);} reader.close (); channelexec.disconnect ();}}