private static String DEFAULTCHART = "UTF-8";public static void main(String[] args) throwsException {
addAlarmFile_new("");
}/*** shell指令复制文件到linux机器
*
**/
public static void addAlarmFile_new(String collectionServiceIP) throwsException{
String recordtime= "";
Date dt= newDate();
SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");
recordtime=sdf.format(dt);
System.out.println("recordtime = " +recordtime);int port = 22;
String username= "root";
String password= "Changeme_123";//创建连接
Connection conn = newConnection(collectionServiceIP,port);
conn.connect();//验证用户名密码
conn.authenticateWithPassword(username,password);//进入到文件保存的目录alarm
Session session =conn.openSession();
session.execCommand("cd opt/osss/share/NCE/FMServices/export/fileint/alarm/;ls");//消费所有输入流
String inStr =consumeInputStream(session.getStdout());
consumeInputStream1(session.getStderr());
session.close();//将文件从alarm复制到fm目录下
Session session1 =conn.openSession();
session1.execCommand("cp /cd opt/osss/share/NCE/FMServices/export/fileint/alarm/"+inStr+" /opt/osss/share/NCE/FMServices/export/fileint/fm/;");
consumeInputStream1(session1.getStdout());
consumeInputStream1(session1.getStderr());
session1.close();//修改文件权限和群组
Session session2 =conn.openSession();
session2.execCommand("chown -R ossuser:ossgroup /opt/osss/share/NCE/FMServices/export/fileint/fm/");
session2.close();//查看复制过去文件的信息//# stat testfile #输入命令//File: `testfile'//Size: 102 Blocks: 8 IO Block: 4096 regular file//Size和Block之前的字符串就是文件大小//Device: 807h/2055d Inode: 1265161 Links: 1//Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)//Access: 2014-08-13 14:07:20.000000000 +0800//Modify: 2014-08-13 14:07:07.000000000 +0800//Change: 2014-08-13 14:07:07.000000000 +0800
String res= doShellCode(collectionServiceIP,"stat /opt/osss/share/NCE/FMServices/export/fileint/fm/"+inStr);
System.out.println("res====="+res);//记录复制过去的文件
String zipSize =getZipSize(res);
System.out.println("复制后文件"+inStr + "\n 大小:"+zipSize);
tools.writeText("复制文件成功,文件大小为:" + zipSize, false);
conn.close();
}private staticjava.lang.String getZipSize(java.lang.String shellResult) {if(shellResult==null||shellResult.equals("")) {
System.out.println("传入查看文件大小的命令有问题");return null;
}int startIndex = shellResult.indexOf("Size:");int endIndex = shellResult.indexOf("Blocks:");
String ress= shellResult.substring(startIndex+5,endIndex); //此处如果不+5,返回值就是“Size: 102”;+5之后,返回值就是“102”
returnress;
}public static String doShellCode(String collectionServiceIP,String ShellCode) throwsException {int port = 22;
String username= "root";
String password= "Changeme_123";//创建连接
Connection conn = newConnection(collectionServiceIP,port);
conn.connect();//验证用户名密码
conn.authenticateWithPassword(username,password);
Session session=conn.openSession();
session.execCommand(ShellCode);//消费所有输入流//String inStr = consumeInputStream(session.getStdout());
String errStr =consumeInputStream(session.getStderr());
String result=processStdout(session.getStdout(),DEFAULTCHART);
session.close();returnresult;
}private staticString processStdout(InputStream in, String charset){
InputStream stdout= newStreamGobbler(in);
StringBuffer buffer= newStringBuffer();try{
BufferedReader br= new BufferedReader(newInputStreamReader(stdout,charset));
String line=null;while((line=br.readLine()) != null){
buffer.append(line+"\n");
}
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}returnbuffer.toString();
}/*** 消费inputstream,并返回
*@throwsException*/
public static void consumeInputStream1(InputStream is) throwsException{
BufferedReader br= new BufferedReader(newInputStreamReader(is));
String s ;while((s=br.readLine())!=null){
System.out.println(s);
}
}public static String consumeInputStream(InputStream is) throwsException{
BufferedReader br= new BufferedReader(newInputStreamReader(is));
String s ;
StringBuilder sb= newStringBuilder();
ArrayList files = new ArrayList<>();while((s=br.readLine())!=null){
System.out.println(s);
sb.append(s);
}return files.get(newRandom().nextInt(files.size()));
}