免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 8058 | 回复: 11

一个轻量型完整性检测工具-Triproot [复制链接]

论坛徽章:
0
发表于 2008-05-30 12:35 |显示全部楼层
前些天为了测试个东西,写的监控某个目录下文件变化的小程序,稍微修改下就成了类似tripwire的工具。
编译: javac triproot.java
运行:java triproot -init [Dir you want init] [Output file name]
         java triproot -check [Dir you want check] [Trip file,that must be init early]

在windows2k、window2003、AS5.1上测试过,linux下编译运行要改个字符,注释里有。



   import java.io.*;
   import java.util.*;
   import java.lang.*;
   import java.text.*;

   public class triproot
   {


  static  ArrayList dirlist = new ArrayList();
  static  HashSet filewriter=new HashSet();
  static long filenum=0;
  static long dirnum=0;

  

public   String   getDateString(long unixtime)    //convert unix time to human time

  {  
Date   date   =   new   Date(unixtime);   
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
return dateString;
  }

   void detectwhatisit(String receivefilename)  //detect receivefilename is a dir or a file,push dir to dirlist,push file to filewriter;

   {
            File dir2 = new File(receivefilename);

            boolean isDir = dir2.isDirectory();
            if (isDir)
            {   
            triproot.dirnum++;
            triproot.dirlist.add(receivefilename);
            }
            else
            {
            triproot.filenum++;
            String lastmodified=getDateString(dir2.lastModified());
            triproot.filewriter.add("Size : "+dir2.length() +"\t Last modify : "+lastmodified + " \t File name :"+dir2.getPath() );

            }//else end


    }//end of detectwhatisit






boolean listfile(String getDir)          //scan getDir

{
   File dir = new File(getDir);
   String[] children = dir.list();
   boolean list_if=false;


    if (children == null)
    {
        // Either dir does not exist or is not a directory

        System.out.println("Directory is null");
    }
    else
    {
        for (int i=0; i<children.length; i++)
        {
            // Get filename of file or directory

            String filename = children[i];
            detectwhatisit(getDir+"\\"+filename);   //if os is linux,replace "\\" to "/"


            if((i+1)==children.length)
            {
                list_if=true;
                }
            else{list_if=false;}                 //if scan getDir is done,set list_if=true


        }//for end

    }//else end

    return list_if;
}//end of listfile



void init_dir(String original,String outDir)
{
ArrayList templist=new ArrayList();
if(new triproot().listfile(original))
{
while(dirlist.size()!=0)
{
for(int j=0;j<dirlist.size();j++)
{
templist.add(dirlist.get(j).toString());
            if((j+1)==dirlist.size())
            dirlist.clear();                   //copy dirlist to templist,and clear dirlist

}

for(int k=0;k<templist.size();k++)                      //scan the second round dir

{
new triproot().listfile(templist.get(k).toString());
        if((k+1)==templist.size()){
             templist.clear(); }
            else{}
}

if(dirlist.size()==0)
{
try
{
  PrintWriter out2 = new PrintWriter(new FileWriter(outDir));
  Iterator ir=filewriter.iterator();
  while(ir.hasNext())
  {
   out2.println(ir.next());
  }

System.out.println("Total dirs is: "+triproot.dirnum);
System.out.println("Total files is: "+triproot.filenum);
out2.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
  }//end of while

  }
}//end of  function init_dir




void check_dir(String newList,String oldList)
{
try
{
BufferedReader new_List =new BufferedReader(new FileReader(newList));
BufferedReader old_List =new BufferedReader(new FileReader(oldList));
HashSet new_hash=new HashSet();
HashSet old_hash=new HashSet();
String s=new String();
   
    while((s = new_List.readLine())!= null)
            {
                new_hash.add(s);   
            }
   
            
            new_List.close();
    while((s = old_List.readLine())!= null)
            {
                old_hash.add(s);   
            }
        
            old_List.close();

  File delete_temp = new File(newList);
  delete_temp.delete();


  Iterator irq=new_hash.iterator();
  int countChange=0;
  System.out.println("-----------Start Check-----------------------------------------------------");
  while(irq.hasNext())
  {
   s =(String)irq.next();
   if(old_hash.contains(s))
   {
   
   }
   else
   {
   System.out.println(s);
   countChange++;
   }
  }
  System.out.println("-----------Check Done!-----------------------------------------------------");
  System.out.println("Total "+countChange+ " Files have been Modified!!!");

}

catch(Exception e)
{
System.out.println(e);
}
}//end of function check_dir



public static void main(String args[])   
{
try{
if(args.length!=3)
{

if(args[0].equals("-help"))
{
System.out.println("User Guide :");
System.out.println("java triproot -init [Dir you want init] [Output file name]");
System.out.println("java triproot -check [Dir you want check] [Trip file,that must be init early]");
System.out.println("IF Dir include space,don't forget \"\" !");
System.exit(1);

}

if(args[0].equals("-check")||args[0].equals("-init"))
{
System.out.println("Incompleted :");
System.out.println("Please type \"java triproot -help\" for more infomation!");
System.exit(1);

}


System.out.println("Unkonw command "+ args[0]);
System.out.println("Please type \"java triproot -help\" for more infomation!");
System.exit(1);
}
}
catch(Exception e)
{
System.out.println(e);
System.out.println("catched input error");
}




if(args[0].equals("-init"))
{
new triproot().init_dir(args[1],args[2]);
System.exit(0);
}



if(args[0].equals("-check"))
{
String temp_out="temp.out";
new triproot().init_dir(args[1],temp_out);
new triproot().check_dir(temp_out,args[2]);
System.exit(0);
}

else
{
System.out.println("Unkonw command "+ args[0]);
System.out.println("Please type \"java triproot -help\" for more infomation!");
System.exit(1);

}

}//end of main

    }//end of class



[ 本帖最后由 t920 于 2008-5-30 12:40 编辑 ]

论坛徽章:
0
发表于 2008-06-03 09:14 |显示全部楼层
嗯,写得不错!

论坛徽章:
0
发表于 2008-06-04 13:10 |显示全部楼层
C:\Program Files\Java\jdk1.5.0_12\bin>javac triproot.java
注意:triproot.java 使用了未经检查或不安全的操作。
注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。

平台p-sp3

编译不成功:(

论坛徽章:
0
发表于 2008-06-05 15:37 |显示全部楼层
ddddddddddddddddd

论坛徽章:
0
发表于 2008-06-19 18:06 |显示全部楼层
有点问题

论坛徽章:
0
发表于 2008-06-23 01:18 |显示全部楼层
支持了 强

论坛徽章:
0
发表于 2008-08-22 12:08 |显示全部楼层
怎么使用的啊?

论坛徽章:
20
CU大牛徽章
日期:2013-04-17 11:48:26羊年新春福章
日期:2015-03-10 22:39:202015年中国系统架构师大会
日期:2015-06-29 16:11:282015亚冠之平阳省
日期:2015-07-31 09:19:042015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-09-30 06:20:002015亚冠之柏太阳神
日期:2015-10-19 20:29:5915-16赛季CBA联赛之天津
日期:2016-11-29 14:03:4315-16赛季CBA联赛之北控
日期:2016-12-24 20:51:492015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-12 20:58:532014年中国系统架构师大会
日期:2014-10-14 15:59:00
发表于 2008-12-02 03:16 |显示全部楼层
嗯,写得不错!

论坛徽章:
0
发表于 2010-05-16 00:53 |显示全部楼层
顶!!

论坛徽章:
0
发表于 2010-05-16 12:11 |显示全部楼层
路过.支持一下.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP