从Jsch插入对命令的响应

时间:2017-10-28 21:27:14

标签: java ssh jsch

我需要使用Jsch从java运行Unix命令。当我在服务器的本地控制台中执行此命令时,控制台会显示一条消息以插入一些参数:

$ cd /mycinema
$ films
$ Insert the title:

但是我不知道如何从java中捕获它,所以我可以检测到它需要一个参数并且能够插入标题并返回电影的结果而不关闭频道或会话。有任何想法吗?我现在的代码就是这个(从本论坛的另一个帖子中复制):

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;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;

public class SSHConnectionManager {

    private Session session;

    private String username = "user";
    private String password = "pass";
    private String hostname = "server";

    public SSHConnectionManager() { }

    public SSHConnectionManager(String hostname, String  , String password) {
        this.hostname = hostname;
        this.username = username;
        this.password = password;
    }

    public void open() throws JSchException {
        open(this.hostname, this.username, this.password);
    }

    public void open(String hostname, String username, String password) throws JSchException{

        JSch jSch = new JSch();

        session = jSch.getSession(username, hostname, 22);
        Properties config = new Properties(); 
        config.put("StrictHostKeyChecking", "no");  // not recommended
        session.setConfig(config);
        session.setPassword(password);

        System.out.println("Connecting SSH to " + hostname + " - Please wait for few seconds... ");
        session.connect();
        System.out.println("Connected!");
    }

    public String runCommand(String command) throws JSchException, IOException {

        String ret = "";

        if (!session.isConnected())
            throw new RuntimeException("Not connected to an open session.  Call open() first!");

        ChannelExec channel = null;
        channel = (ChannelExec) session.openChannel("exec");

        channel.setCommand(command);
        channel.setInputStream(null);

        InputStream in = channel.getInputStream(); // channel.getInputStream();

        channel.connect();

        ret = getChannelOutput(channel, in);

        channel.disconnect();

        System.out.println("Finished sending commands!");

        return ret;
    }


    private String getChannelOutput(Channel channel, InputStream in) throws IOException{

        byte[] buffer = new byte[1024];
        StringBuilder strBuilder = new StringBuilder();

        String line = "";
        while (true){
            while (in.available() > 0) {
                int i = in.read(buffer, 0, 1024);
                if (i < 0) {
                    break;
                }
                strBuilder.append(new String(buffer, 0, i));
                System.out.println(line);
            }

            if(line.contains("logout")){
                break;
            }

            if (channel.isClosed()){
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee){}
        }

        return strBuilder.toString();   
    }

    public void close(){        
        session.disconnect();
        System.out.println("Disconnected channel and session");
    }


    public static void main(String[] args){

        SSHConnectionManager ssh = new SSHConnectionManager();
        try {
            ssh.open();
            String ret = ssh.runCommand("cd /mycinema; films;");              

            System.out.println(ret);
            ssh.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案