1 Star 0 Fork 0

cnrobin / myjava

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
KaliBatchScan.java 5.23 KB
一键复制 编辑 原始数据 按行查看 历史
cnrobin 提交于 2020-07-07 14:54 . kaliCentos
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.text.*;
import java.util.*;
import java.io.*;
public class KaliBatchScan {
public static Set getIPlist(String filepath){
Set myset = new HashSet();
//String filepath="C:\\myjava\\sec\\ok\\";
File file=new File(filepath);
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
System.out.println(filelist[i]);
String cols[]=filelist[i].split("_");
myset.add(cols[0]);
}
return myset;
}
///home/robin/myjava/xingneng
public static void main(String args[]) {
String main_ip_list="/home/robin/myjava/sec/hz_all_ip0323.txt";
String filepath="/home/robin/myjava/ok/";
String subfile="/home/robin/myjava/sec/iplist";
Set hs = new HashSet();
int Max_sub=50;
try{
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(main_ip_list)));
BufferedWriter bw[]=new BufferedWriter[Max_sub];
for(int i=0;i<Max_sub;i++){
bw[i]=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(subfile+(i+1)+".txt")));
}
String line;
while((line=br.readLine())!=null){
hs.add(line);
}
br.close();
Set myset=getIPlist(filepath);
Iterator it = myset.iterator();
while (it.hasNext()) {
hs.remove(it.next());
}
Iterator it2 = hs.iterator();
int index=0;
while (it2.hasNext()) {
bw[index].write(it2.next()+"\r\n");
index++;
if(index==Max_sub) index=0;
}
for(int i=0;i<Max_sub;i++){
bw[i].close();
}
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
//******************************up code is create file,down code is create thread
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String s=df.format(new Date());
System.out.println(s);
ThreadScan T[]=new ThreadScan[Max_sub];
for(int i=0;i<Max_sub;i++){
T[i]=new ThreadScan("iplist"+(i+1),s);
T[i].start();
}
s=df.format(new Date());
System.out.println("main:"+s);
}
}
class ThreadScan extends Thread {
private Thread t;
private String threadName;
private String timeStr;
ThreadScan( String name,String timeString) {
threadName = name;
timeStr=timeString;
System.out.println("Creating " + threadName );
}
private void moveTotherFolders(String fileName){
String startPath = "/home/robin/myjava/sec/temp" + File.separator + fileName;
String endPath = "/home/robin/myjava/sec/ok"+ File.separator;
try {
File startFile = new File(startPath);
File tmpFile = new File(endPath);//获取文件夹路径
if(!tmpFile.exists()){//判断文件夹是否创建,没有创建则创建新文件夹
tmpFile.mkdirs();
}
System.out.println(endPath + startFile.getName());
if (startFile.renameTo(new File(endPath + startFile.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
public void run() {
String mydir="/home/robin/myjava/sec/";
System.out.println("Running " + threadName );
try {
int index=1;
while(index>0){
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(mydir+threadName+".txt")));
String line,line2;
while(((line = br.readLine()) != null)){
System.out.println(line);
Thread.sleep(10);
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String s=df.format(new Date());
String osName = System.getProperty("os.name");//获取操作系统类型
String pingCommand = "";
pingCommand="nmap -A -T4 -Pn -p0-65535 "+line;
Process pro = Runtime.getRuntime().exec(pingCommand);
BufferedReader buf = new BufferedReader(new InputStreamReader(
pro.getInputStream()));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(mydir+"temp"+File.separator +line+"_time"+s+".txt")));
bw.write("time:"+s+"\n");
while ((line2 = buf.readLine()) != null){
bw.write(line2+"\r\n");
}
bw.flush();
bw.close();
moveTotherFolders(line+"_time"+s+".txt");
}
br.close();
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String s=df.format(new Date());
System.out.println("sub:"+s);
index=index-1;
}
}catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start () {
System.out.println("Starting " + threadName );
System.out.println(System.currentTimeMillis());
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}
1
https://gitee.com/cnrobin/myjava.git
git@gitee.com:cnrobin/myjava.git
cnrobin
myjava
myjava
master

搜索帮助