Topic: 帮修改一个程序,现在缺少输入函数

  Print this page

1.帮修改一个程序,现在缺少输入函数 Copy to clipboard
Posted by: aspirer
Posted on: 2007-04-03 14:57

已经有了一程序,可以查询IP 地址的,现在稍微改了一下DOS命令那条语句,可以用于查询远端计算机的MAC地址了,“nbtstat -a 计算机名”
现在有一想法就是改造这个程序,计算机名用于手工输入,然后回车就得到结果。
再深入一点的改造就是计算机名,我把它存成txt文档,然后用程序一行一行读取,然后输出MAC地址。(也可以存为txt文档)
高手帮帮忙吧,附上原代码,请帮忙改造一下。偶是新手。希望得到你的帮助。

import java.io.*;

public class GetMac { // Get MAC Address function
public static void main(String[] args) {
try {


Process process = Runtime.getRuntime().exec("nbtstat -a computername");
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);
}
} catch (java.io.IOException e) {
System.err.println("IOException " + e.getMessage());
}
}
}

2.Re:帮修改一个程序,现在缺少输入函数 [Re: aspirer] Copy to clipboard
Posted by: aspirer
Posted on: 2007-04-03 22:35

nobody here or nobody can help?

{ Please be patient! }

3.Re:帮修改一个程序,现在缺少输入函数 [Re: aspirer] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2007-04-04 00:03

okay..check out my code.

import java.io.*;

public class GetMac
{
// Get MAC Address function
public static void main(String[] args)
{
try
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));

System.out.print("Please enter the remote machine's name:");
String hostName = br.readLine();

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);
}
}

long stopTime = System.currentTimeMillis();

if (stopTime - startTime > 1000)
{
throw new RuntimeException("This could be a potential DNS issue, or hostname does not exist");
}
}
catch (java.io.IOException e)
{
System.err.println("IOException " + e.getMessage());
}
}
}

4.Re:帮修改一个程序,现在缺少输入函数 [Re: aspirer] Copy to clipboard
Posted by: aspirer
Posted on: 2007-04-04 10:25

Thanks for HenryShanley.Thanks for your kind heart.
我借此学习了 简单的字符串输入输出,因为有实例,比看书吸收得快。谢谢了。

下面继续进一步改造如何?
如何用它读取txt文档,逐行读取,然后逐个赋给hostname,查出来的结果,逐个写到指定的文件里面?

即我想进一步了解的是Java 中文件的输入与输出。一方面也可以将这程序用于工作,不亦乐乎!
Thanks again!

5.Re:帮修改一个程序,现在缺少输入函数 [Re: aspirer] Copy to clipboard
Posted by: aspirer
Posted on: 2007-04-06 09:47

存在问题如下:输入完一个得到MAC地址后不能继续去输入第二个主机名。

6.Re:帮修改一个程序,现在缺少输入函数 [Re: aspirer] Copy to clipboard
Posted by: aspirer
Posted on: 2007-04-09 10:00

靠山山会倒,
问别人不如问自己!多钻研少发问。

此区 1.人气不旺,2,高手不多?3.热心者不多?
4.我自己问题太简单?

7.Re:帮修改一个程序,现在缺少输入函数 [Re: aspirer] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2007-04-10 19:31

我们这里的大多数人都是用自己业余的时间来解答别人的问题的,所以你可能不能在第一时间内得到答案,给别人一点时间,当然我们也希望看到有意义回答的帖子,这么我们也会得到提高的,不要期待会有人给你一个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


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923