自动化测试框架-连接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;
}
}
- 自动化测试框架-连接Unix
- 自动化测试框架: 协同
- 浅谈自动化测试框架
- 自动化测试框架: 协同
- 自动化测试框架
- 自动化测试框架指南
- 自动化测试框架剖析
- 自动化测试框架
- 自动化测试框架
- 自动化测试框架指南
- CUBRID自动化测试框架
- 自动化测试框架思路
- 自动化开发测试框架
- 自动化测试框架设计
- 自动化测试框架简介
- 自动化测试框架图解
- 浅谈自动化测试框架
- 自动化测试框架Concordion
- Base64 加密
- 条件编译
- 几道CMM相关考试题
- ORA-01502:索引'name'或这类索引的分区处于不可用状态
- #define new DEBUG_NEW 作用
- 自动化测试框架-连接Unix
- 使用ajax异步回调函数实现服务端图片上传验证
- configuring gradle test task
- 怎样在mac系统的电脑上注册一个免信用卡id的App store账号
- shell计时
- Ubuntu 10.10搭建git服务器
- Change Log of Joomsport: Adding Order to Teams within one Group
- hello,csdn!
- Python获取rpm包基本信息[import rpm]