自动化测试中用到的一些功能类

发表于:2013-9-09 11:00

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Kiven_Test    来源:51Testing软件测试网采编

分享:

  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+"\r\n",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:&nbsp;&nbsp;&nbsp;&nbsp;";
  
  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);
// }
}

  写分享这么多吧~各位,晚安!

55/5<12345
价值398元的测试课程免费赠送,填问卷领取吧!

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海漕溪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2022
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号