自动化测试中用到的一些功能类
作者:网络转载 发布时间:[ 2013/9/9 14:47:28 ] 推荐标签:
9、java远程登录linux并执行命令
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
/*
* 远程调用linux下的vmstat命令,并将结果完整写入文件中
*/
public class SSHTest {
/**
* @param args
* @throws IOException
*/
/*
* 主机地址、端口、用户名、密码
*/
static String hostName = "172.16.3.9";
static int port = 2222;
static String userName = "root";
static String pwd = "kedats";
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("开始连接主机");
Connection conn = new Connection(hostName, port);
conn.connect();
boolean isdenglu = conn.authenticateWithPassword(userName, pwd);
if (isdenglu) {
System.out.println("ssh2登陆成功");
} else {
System.out.println("登陆失败");
}
//System.out.println("当前目录:");
Session ses = conn.openSession();
ses.execCommand("vmstat 2");
InputStream stdout = new StreamGobbler(ses.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
FileWriter fw = new FileWriter("F:\vmstat.txt");
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
fw.write(line+"
",0,line.length()+2);
fw.flush();
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("data2.txt"));
// osw.write(line,0,line.length());
// osw.flush();
// PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("hello3.txt")),true);
// pw.println(line);
}
System.out.println("运行结果:"+ses.getExitStatus());
//关闭文件
fw.close();
ses.close();
conn.close();
}
}
10、java将控制台打印写入日志文件
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class ToLog {
static GregorianCalendar time = new GregorianCalendar();
// int year = time.get(Calendar.YEAR); //得到日期的年份
// int day = time.get(Calendar.DAY_OF_MONTH); //得到日期的天
// int month = time.get(Calendar.MONTH)+1; //得到日期的月份
// int weekDay = time.get(Calendar.DAY_OF_WEEK); //得到日期为星期几
// int weekOfYear = time.get(Calendar.WEEK_OF_YEAR); //得到日期为年的第几周
// int weekOfMonth = time.get(Calendar.WEEK_OF_MONTH); //得到日期为月的第几周
private static final String getToday = time.get(Calendar.YEAR)+"-"+(time.get(Calendar.MONTH)+1)+"-"+time.get(Calendar.DAY_OF_MONTH)+"-";
private static final String filePath = "C:\Documents and Settings\Administrator\workspace\Movision_script\logs\"+getToday+"log.html";
//写入文件
public void toLog(String message){
StackTraceElement stack[] = (new Throwable()).getStackTrace();
StackTraceElement s = stack[1];
String headerMessage = s.getClassName()+"."+s.getMethodName()+"()"+"★LineNum:"+s.getLineNumber()+"<br />★Message: ";
headerMessage = addDateTimeHeader(headerMessage);
message = headerMessage + message + "<br /><br /><br />";
FileWriter fw = null;
File file = null;
try{
file = new File(filePath);
fw = new FileWriter(file,true);
fw.write(message);
}catch(IOException ie){
ie.printStackTrace();
}finally{
try{
fw.close();
}catch(IOException ie){
ie.printStackTrace();
}
}
}
@SuppressWarnings("deprecation")
public String addDateTimeHeader(String headerMessage) {
String dateTimeHeader = new Date().toLocaleString()+"★";
return dateTimeHeader += headerMessage;
}
// public static void main(String args[]){
// ToLog log = new ToLog();
// String message = "这只是测试";
// log.toLog(message);
// }
}
写分享这么多吧!
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11