自动化测试框架-连接Unix

来源:互联网 发布:富士通打印软件安装 编辑:程序博客网 时间:2024/05/16 17:43


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class Unix {

    private String hostname;
    private String username;
    private String password;
    private Connection conn;
    private Session session;

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

    public void login() throws Exception {
        System.out.print("Login Unix host " + this.hostname + " ... ");

        conn = null;

        try {
            conn = new Connection(hostname);
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(username, password);
            if (isAuthenticated == false)
                throw new Exception("Authentication failed.");
        } catch (Exception e) {
            throw e;
        }

        System.out.println("OK");
    }

    public void logout() throws Exception {
        System.out.print("Logout Unix host" + this.hostname + " ... ");

        if (conn == null)
            System.err.println("Unix connection is not open");
        else {
            conn.close();
        }
        System.out.println("OK");
    }

    public void scpFileToLocal(String remoteFile, String localTargetDirectory) throws Exception {
        System.out.print("Copy file " + remoteFile + " ... ");
        SCPClient client = new SCPClient(conn);
        client.get(remoteFile, localTargetDirectory);
        System.out.println("OK");
    }

    public int execCommand(String command) throws Exception {
        System.out.println("Execute command " + command + " ... ");

        command = ". ./.profile && " + command;

        session = conn.openSession();
        session.execCommand(command);

        int timeout = 60;
        while (session.getExitStatus() == null) {
            timeout--;
            Thread.sleep(1000);
            if (timeout < 0)
                break;
        }
        int retVal = session.getExitStatus().intValue();

        InputStream stdout = new StreamGobbler(session.getStdout());
        InputStream stderr = new StreamGobbler(session.getStderr());

        BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
        BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));

        // System.out.println("Here is the output from stdout:");

        while (true) {
            String line = stdoutReader.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }

        // System.out.println("Here is the output from stderr:");

        // while (true) {
        // String line = stderrReader.readLine();
        // if (line == null)
        // break;
        // System.out.println(line);
        // }

        session.close();
        System.out.println("OK");
        return retVal;
    }
}