我们这里的大多数人都是用自己业余的时间来解答别人的问题的,所以你可能不能在第一时间内得到答案,给别人一点时间,当然我们也希望看到有意义回答的帖子,这么我们也会得到提高的,不要期待会有人给你一个100%的答案,有一个提示,你就可以自己研究下去。不要给我们任何压力,呵呵,毕竟我可不想在下了班以后还要面对另一个Team leader,编程带给我们的是快乐,而不是压力。我不是高手,永远也不是,嘿嘿嘿...。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.util.Date;
/**
* A small program to hack others MAC address.
* Note that you need to turn on the nbstat in your OS.
* TO aspirer , you need a hosts.txt in your current working directory.
* @author jiafanz
*
* Also feel free to send me an email if you have any further questions
* on this source code: zhoujiafan@hotmail.com
* Don't blame me or others if reply late.
*
*/
// TODO: This program is not finished yet, it is only a prototype.
// TODO: System.out.println() looks horriable in the code.
// we might change them to log4j if necessary.
public class GetMac
{
private static final String INPUT_FILE = "hosts.txt";
private static final String OUTPUT_FILE = "results.txt";
private static final String NBTSTAT_A = "nbtstat -a ";
private static final String MESSAGE = "-------------------------------------------------------------\n"
+ "This is a program to resolve MAC Address of remote computers.\n"
+ "** Don't use it for hacking other computers. ***\n"
+ "-------------------------------------------------------------\n"
+ "Choose the option in the following menu:\n"
+ "1. Read hostname from the console:\n"
+ "2. Read hostnames from a CVS file in the CWD:\n"
+ "Enter '0' to exit this program: ";
private static final String INVALID_INPUT = "Invalid input, choose 1,2 or 0 to exit.";
/**
* Use the console to get the remote machine's name.
* @return <tt>true</tt> if succeed to get one.
* <tt>false</tt> otherwise
*/
public boolean useConsole()
{
try
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Please enter the remote machine's name (ctrl+c to exit): ");
String hostName = br.readLine();
return processHost(hostName, false, null) ? true : false;
}
catch (java.io.IOException e)
{
System.err.println("IOException " + e.getMessage());
return false;
}
}
/**
* Use the CVS to get the remote machine's name.
* @param inputFileName the file name in the current working directory.
* @return <tt>true</tt> if succeed to get one.
* <tt>false</tt> otherwise
*/
public boolean useCvs(String inputFileName)
{
try
{
File inputFile = new File(inputFileName);
File outputFile = new File (OUTPUT_FILE);
if (!inputFile.exists())
{
System.out.println("Error: file does not exist");
return false;
}
BufferedReader br = new BufferedReader(
new FileReader(inputFile));
PrintWriter pw = new PrintWriter(
new FileWriter(outputFile));
String input = null;
while ((input = br.readLine()) != null)
{
processHost(input, true, pw);
}
br.close();
pw.close();
}
catch (java.io.IOException e)
{
System.err.println("IOException " + e.getMessage());
return false;
}
return true;
}
/**
* Process one computer's MAC address based on the given hostName.
* @param hostName
* @param isWriteFile whether write to a file or not
* @param pw PrintWriter
* @return
* @throws IOException
*/
private boolean processHost(String hostName,
boolean isWriteFile,
PrintWriter pw) throws IOException
{
long startTime = System.currentTimeMillis();
Process process = Runtime.getRuntime().exec(NBTSTAT_A + hostName);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null)
{
if (line.indexOf("MAC Address") > 0)
{
String MACAddr =line.substring(line.indexOf("-") -2);
System.out.println("MAC address = [" + MACAddr+ "]");
System.out.println(MACAddr);
if (isWriteFile)
{
pw.println(new Date());
pw.println("MAC address = [" + MACAddr+ "]");
pw.println(MACAddr);
}
}
}
long stopTime = System.currentTimeMillis();
if (stopTime - startTime > 1000)
{
System.err.println("This could be a potential DNS issue, or hostname does not exist!!");
return false;
}
return true;
}
// Get MAC Address function
public static void main(String[] args)
{
System.out.print(MESSAGE);
GetMac instance = new GetMac();
try
{
BufferedReader br = null;
String input = null;
boolean setSystemIn = true;
while (true)
{
try
{
if (setSystemIn)
{
System.out.println(MESSAGE);
br = new BufferedReader(
new InputStreamReader(System.in));
input = br.readLine().toLowerCase().trim();
}
int option = Integer.parseInt(input);
switch (option)
{
case 0:
System.exit(0);
break;
case 1:
setSystemIn = false;
instance.useConsole();
break;
case 2:
setSystemIn = true;
if (!instance.useCvs(INPUT_FILE))
{
return;
}
break;
default:
setSystemIn = true;
System.out.println(INVALID_INPUT);
break;
}
}
catch (NumberFormatException nfex)
{
setSystemIn = true;
System.out.println(INVALID_INPUT);
}
}
}
catch (java.io.IOException e)
{
System.err.println("IOException " + e.getMessage());
}
}
}
If you compare the preceding two source code, you will find I refactored the code a little bit, hope that it will not cause any confusion to you.
Cheers,
Jiafan