ssh设备通过Java,执行命令并从命令获得响应

时间:2014-03-07 13:54:44

标签: java ssh

我正在尝试通过java代码 ssh 设备,我遇到了错误。我在用 (http://www.jcraft.com/)的罐子。现在这是我的问题

  • 如何使用java在chell中执行多个命令? (jcreft lib或其他)
  • 如何从以下代码获得输出(输出流的转换)

    OutputStream out = System.out;
    PrintStream ps = new PrintStream(out);
    

    这是代码的快照

    public static String LoginAppliance(String host, String ID )throws JSchException, InterruptedException, IOException
    {
           String result="";    
           String command = "_shell\n";
           JSch jsch = new JSch();
           Session session = jsch.getSession(user, host, 22);
           session.setPassword(password);
           session.setConfig("StrictHostKeyChecking", "no");
           session.connect(10*1000);
           Channel channel = session.openChannel("shell");
           InputStream is = new ByteArrayInputStream(command.getBytes());
           channel.setInputStream(is);
           channel.setOutputStream(System.out);
           OutputStream out = System.out;
           PrintStream ps = new PrintStream(out);        
           channel.connect(15 * 1000);
           Thread.sleep(3*1000);
           channel.disconnect();
           session.disconnect();
           return (result);
    }
    

这对我很有帮助。

2 个答案:

答案 0 :(得分:0)

  1. 多个命令可以用分号(;)分隔,如:

    String command = "echo 'hello';echo 'hello2'\n";
    
  2. 将结果作为字符串获取,而不是将其打印到控制台,如:

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    channel.setOutputStream(out);
    ...
    return out.toString();
    

答案 1 :(得分:0)

我误解了这个问题。我看了一下Jsch的文档,这对我来说是最简单的方法:

import java.io.IOException;
import java.io.InputStream;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class Main {

    public static void main(String[] args) {
        String host = "theAddress";
        String user = "root";
        String password = "thePassword";

        JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(10000);
            System.out.println("-->" + runCommand("echo 'hello'", session)); // -->hello
            System.out.println("-->" + runCommand("whoami", session)); // -->root
            System.out.println("-->" + runCommand("date", session)); // -->Thu Mar 13 23:45:39 CET 2014
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }

    }

    private static String runCommand(String string, Session session) {
        ChannelExec channel = null;
        try {
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(string);
            channel.connect();
            return readOutput(channel);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
        }
        return null;
    }

    private static String readOutput(ChannelExec channel) throws IOException {
        // code from: http://www.jcraft.com/jsch/examples/Exec.java.html
        StringBuilder sb = new StringBuilder();
        InputStream in = channel.getInputStream();
        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0)
                    break;
                sb.append(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception e) {}
        }
        return sb.toString();
    }

}