62,612
社区成员
发帖
与我相关
我的任务
分享
package com.util;
import java.net.InetAddress;
public class IpTest implements Runnable{
private static StringBuffer targetIp;
public void searchIp(){
try {
InetAddress is = InetAddress.getLocalHost();
byte[] ip = is.getAddress();
targetIp = new StringBuffer();
for(int i=0; i<(ip.length-1); i++) {
if(i > 0) {
targetIp.append(".");
}
targetIp.append(ip[i]&0xff);
}
//int a = ip[3]&0xff;
new Thread(this).start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
String host = null;
try {
for(int s=1; s<254; s++) {
host = targetIp.toString() + "." + s;
InetAddress it = InetAddress.getByName(host);
System.out.println(host);
String str = it.getHostName();
if(!str.equals(host)) {
System.out.println(host + "/" + str);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new IpTest().searchIp();
}
}
import java.net.*;
import java.io.*;
public class ScanIntranet
{
static int ip1 = 192;
static int ip2 = 168;
static int ip3 = 0;
static int ip4 = 0;
static int ipCount = 64;
public static void main(String [] args)
{
while (ip3 < 256 && ip4 < 256)
{
Thread scanner = new ScanThread(ip3,ip4);
scanner.start();
ip4 += ipCount;
if (ip4 > 255)
{
ip3++;
ip4 = 0;
}
}
}
}
class ScanThread extends Thread
{
int ip3;
int ip4;
public ScanThread(int ip3,int ip4)
{
this.ip3 = ip3;
this.ip4 = ip4;
System.out.println(Thread.currentThread() + " scanning from " + ip3 + "." + ip4 + " to " + ip3 + "." + (ip4 + ScanIntranet.ipCount));
}
private boolean checkIP(String ip) throws Exception
{
Runtime runtime = Runtime.getRuntime();
String cmd = "ping " + ip;
Process proc = runtime.exec(cmd);
BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String infor = theReader.readLine();
boolean alive = false;
while (infor != null)
{
//System.out.println(infor);
if (infor.startsWith("Reply from"))
{
alive = true;
break;
}
else if (infor.startsWith("Request timed out"))
{
break;
}
infor = theReader.readLine();
}
theReader.close();
proc.destroy();
return alive;
}
public void run()
{
FileWriter writer = null;
try
{
writer = new FileWriter("ip_alive.txt");
}
catch (Exception e) {}
for (int i = 0; i < ScanIntranet.ipCount; i++)
{
try
{
if (ip3 > 255 || ip3 < 0)
{
ip3 = 255;
}
if (ip4 > 255 || ip4 < 0)
{
ip4 = 255;
}
String ip = ScanIntranet.ip1 + "." + ScanIntranet.ip2 + "." + ip3 + "." + ip4;
boolean alive = checkIP(ip);
if (alive)
{
System.out.println(ip + " is alive ");
writer.write(ip);
writer.flush();
}
else
{
System.out.println(ip + " is dead ");
}
}
catch (Exception e)
{
e.printStackTrace();
}
ip4++;
}
try
{
writer.close();
}
catch (Exception e) {}
}
}
import java.net.*;
import java.io.*;
public class ScanIntranet
{
static int ip1 = 192;
static int ip2 = 168;
static int ip3 = 0;
static int ip4 = 0;
static int ipCount = 64;
public static void main(String [] args)
{
while (ip3 < 256 && ip4 < 256)
{
Thread scanner = new ScanThread(ip3,ip4);
scanner.start();
ip4 += ipCount;
if (ip4 > 255)
{
ip3++;
ip4 = 0;
}
}
}
}
class ScanThread extends Thread
{
int ip3;
int ip4;
public ScanThread(int ip3,int ip4)
{
this.ip3 = ip3;
this.ip4 = ip4;
System.out.println(Thread.currentThread() + " scanning from " + ip3 + "." + ip4 + " to " + ip3 + "." + (ip4 + ScanIntranet.ipCount));
}
private boolean checkIP(String ip) throws Exception
{
Runtime runtime = Runtime.getRuntime();
String cmd = "ping " + ip;
Process proc = runtime.exec(cmd);
BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String infor = theReader.readLine();
boolean alive = false;
while (infor != null)
{
//System.out.println(infor);
if (infor.startsWith("Reply from"))
{
alive = true;
break;
}
else if (infor.startsWith("Request timed out"))
{
break;
}
infor = theReader.readLine();
}
theReader.close();
proc.destroy();
return alive;
}
public void run()
{
FileWriter writer = null;
try
{
writer = new FileWriter("ip_alive.txt");
}
catch (Exception e) {}
for (int i = 0; i < ScanIntranet.ipCount; i++)
{
try
{
if (ip3 > 255 || ip3 < 0)
{
ip3 = 255;
}
if (ip4 > 255 || ip4 < 0)
{
ip4 = 255;
}
String ip = ScanIntranet.ip1 + "." + ScanIntranet.ip2 + "." + ip3 + "." + ip4;
boolean alive = checkIP(ip);
if (alive)
{
System.out.println(ip + " is alive ");
writer.write(ip);
writer.flush();
}
else
{
System.out.println(ip + " is dead ");
}
}
catch (Exception e)
{
e.printStackTrace();
}
ip4++;
}
try
{
writer.close();
}
catch (Exception e) {}
}
}
package com.util;
import java.net.InetAddress;
public class IpTest implements Runnable{
private static StringBuffer targetIp;
public void searchIp(){
try {
InetAddress is = InetAddress.getLocalHost();
byte[] ip = is.getAddress();
targetIp = new StringBuffer();
for(int i=0; i<(ip.length-1); i++) {
if(i > 0) {
targetIp.append(".");
}
targetIp.append(ip[i]&0xff);
}
//int a = ip[3]&0xff;
new Thread(this).start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
String host = null;
try {
for(int s=1; s<254; s++) {
host = targetIp.toString() + "." + s;
InetAddress it = InetAddress.getByName(host);
System.out.println(host);
String str = it.getHostName();
if(!str.equals(host)) {
System.out.println(host + "/" + str);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new IpTest().searchIp();
}
}
import java.util.*;
import java.io.*;
import java.net.*;
public class ScanIntranet
{
static int ip1 = 192;
static int ip2 = 168;
static int ip3 = 0;
static int ip4 = 0;
static int ipCount = 4;
static FileWriter writer = null;
static SortedSet<String> ips = new TreeSet<String>();
public static void main(String [] args)
{
try
{
writer = new FileWriter("ip_alive.txt");
}catch (Exception e) {}
int ip3Max = 256;
int ip4Max = 256;
Vector<ScanThread> allThreads = new Vector<ScanThread>();
while (ip3 < ip3Max && ip4 < ip4Max)
{
Thread scanner = new ScanThread(ip3,ip4);
scanner.start();
allThreads.add((ScanThread)scanner);
ip4 += ipCount;
if (ip4 > ip4Max)
{
ip3++;
ip4 = 0;
}
}
while (true)
{
boolean allRun = false;
for (ScanThread t : allThreads)
{
if (t.threadRun != false)
{
allRun = true;
break;
}
}
try
{
Thread.sleep(1000);
} catch (Exception e) {}
if (!allRun)
{
System.out.println("all thread scan over... ");
try
{
Iterator<String> it = ips.iterator();
System.out.println(ips.size() + " ips are alive");
while (it.hasNext())
{
String ip = it.next();
//System.out.println(ip);
ip = ip.replaceAll("#","");
writer.write(ip + "\r\n");
}
writer.close();
} catch (Exception e) {}
break;
}
}
}
}
class ScanThread extends Thread
{
int ip3;
int ip4;
public boolean threadRun;
public ScanThread(int ip3,int ip4)
{
this.ip3 = ip3;
this.ip4 = ip4;
System.out.println(this + " scanning from " + ip3 + "." + ip4 + " to " + ip3 + "." + (ip4 + ScanIntranet.ipCount - 1));
}
private boolean checkIP(String ip) throws Exception
{
Runtime runtime = Runtime.getRuntime();
String cmd = "ping " + ip;
Process proc = runtime.exec(cmd);
BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String infor = theReader.readLine();
boolean alive = false;
while (infor != null)
{
//System.out.println(infor);
if (infor.startsWith("Reply from"))
{
alive = true;
break;
}
else if (infor.startsWith("Request timed out"))
{
break;
}
infor = theReader.readLine();
}
theReader.close();
proc.destroy();
return alive;
}
public void run()
{
threadRun = true;
for (int i = 0; i < ScanIntranet.ipCount; i++)
{
try
{
if (ip3 > 255 || ip3 < 0)
{
ip3 = 255;
}
if (ip4 > 255 || ip4 < 0)
{
ip4 = 255;
}
String ip = ScanIntranet.ip1 + "." + ScanIntranet.ip2 + "." + ip3 + "." + ip4;
boolean alive = checkIP(ip);
if (alive)
{
System.out.println(ip + " is alive ");
String ip3str = "";
String ip4str = "";
if (ip3 < 100)
{
ip3str = "#";
if (ip3 < 10)
{
ip3str = "##";
}
}
ip3str += ip3;
if (ip4 < 100)
{
ip4str = "#";
if (ip4 < 10)
{
ip4str = "##";
}
}
ip4str += ip4;
String hostName = InetAddress.getByName(ip).getCanonicalHostName();
ip = ScanIntranet.ip1 + "." + ScanIntranet.ip2 + "." + ip3str + "." + ip4str + "\t\t" + hostName;
ScanIntranet.ips.add(ip);
}
else
{
//System.out.println(ip + " is dead ");
}
}
catch (Exception e)
{
e.printStackTrace();
}
ip4++;
}
threadRun = false;
System.out.println(this + " scann over... ");
}
}