发送Jsch命令

时间:2017-02-04 19:13:12

标签: java linux jsch

在这里把头撞在墙上。 我正在尝试在构造函数中建立一个链接并登录。然后在begin方法中执行远程计算机上的java应用程序。最后在终止时,杀死java进程。

没有异常显示,命令只是不执行。如果我去远程终端并运行最后一个,我可以看到用户已登录。用户的登录时间仅为0。如果我将密码更改为错误的密码,我也会收到错误。

但是远程java应用程序没有运行。当我直接从该终端登录或ssh并从命令行运行它时,远程应用程序运行正常。

想法?提前谢谢。

//Constants for the login information
String HOST="<HOSTNAME>";
String USER="<userid>";
String PASSWORD="<password>";

//Constants for command strings
String START_COMMAND="java -Djava.library.path=/usr/local/share/OpenCV/java -jar <filename>";
String TERMINATE_COMMAND="killall java";

Channel channel;
Session session;
InputStream in;

public VisionProcessing(){
    java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");
    JSch jsch = new JSch();
    try {
        System.out.println("Trying login");
        session = jsch.getSession(USER, HOST, 22);
        session.setPassword(PASSWORD);
        session.setConfig(config);  //XXX No idea what this does
        session.connect();
        channel=session.openChannel("exec");
        ((ChannelExec)channel).setPty(true); 
        channel.connect();//This is important

        if (channel.isConnected()){
            System.out.println("Connected");
        } else {
            System.out.println("Not Connected");
        }
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Exception logging in");
    }
}

public boolean beginTargeting() {
    try {
        StringBuilder outputBuffer = new StringBuilder();

        Channel channel = session.openChannel("exec");
        InputStream commandOutput = channel.getInputStream();
        int readByte = commandOutput.read();

        while(readByte != 0xffffffff)
        {
           outputBuffer.append((char)readByte);
           readByte = commandOutput.read();
        }
        System.out.println("Begin");
    if (channel.isConnected()){
        System.out.println("Connected");            
        ((ChannelExec)channel).setCommand(START_COMMAND);
    } else {
        System.out.println("Not Connected");
    }
    channel.connect();//This is important
    channel.disconnect();
    }catch(IOException ioX)
     {
        System.out.println(ioX);
     }
     catch(JSchException jschX)
     {
            System.out.println(jschX);
     }
    return true;
}



   public boolean terminateTargeting() {
    try {
    System.out.println("Terminate");
    channel=session.openChannel("exec");
    ((ChannelExec)channel).setCommand(TERMINATE_COMMAND);
    channel.connect();//This is important
    System.out.println("Terminating");

    channel.setInputStream(null);
    channel.disconnect();
    session.disconnect();
    System.out.println("DONE");
}catch(Exception ee){
    System.out.println(ee);
}
    return true;
}

0 个答案:

没有答案