Leitura do HD

5 respostas
T

Amigos, preciso saber a Classe/método que faça a leitura do número serial e volume do disco rigido.

5 Respostas

R

Número Serial, é complicado, pro windows fica num lugar, pro linux fica n’outro…

o volume, tu diz é o tamanho?

se for, usa File.

[]'s

J

Cara, isso é realmente complicado. Talvez você tenha que usar métodos nativos com JNI. Tente implementar/encontrar alguma solução em baixo nível. Já andei procurando por isso e não encontrei nada (em Java somente). Mas se o teu objetivo for identificar uma máquina, que tal o MAC Adress? Tenho uma solução para isso. Se tiver interesse…

T

Na verdade eu faria um método para ler o número do HD, esse seria mais uma maneira de impedir a pirataria.
Já há procedimentos diferentes, mas queria adotar este também. Entendem?
Alguém tem alguma dica de criação de proteção contra pirataria… ?

T

Amigão, obrigado, eu gostaria sim de conhecer sua solução.

SObre o MAc adress tenho um pouco de medo, pois aqui na minha rede (100 micros), eu tenho micros com MAC iguais, :roll: fizemos propositalmente.
Há como fazer.! É só mudar.

hmm… até achei estranho…

Mas gostaria sim de contacta-lo.

Valeu. :razz:

J

Putz, na verdade o objetivo do MAC Address realmente seria ser UNICO, mas com a incidencia de placas ‘la garantia soy yo’, fica dificil mesmo. Mas se for ver, acho que HD tbm tem esse risco, não sei ao certo. Bom, segue a solução para MAC:

import java.io.*;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;
import java.lang.management.MemoryUsage;
 
import static java.lang.management.ManagementFactory.*;
import java.lang.management.*;
import javax.management.*;
 
import java.lang.*;
 
public final class InfoRede {
	
	private final static String getMacAddress() throws IOException {
		String os = System.getProperty("os.name");
		
		try {
			if(os.startsWith("Windows")) {
				return windowsParseMacAddress(windowsRunIpConfigCommand());
			} else if(os.startsWith("Linux")) {
				return linuxParseMacAddress(linuxRunIfConfigCommand());
			} else {
				throw new IOException("Sistema operacional desconhecido: " + os);
			}
		} catch(ParseException ex) {
			ex.printStackTrace();
			throw new IOException(ex.getMessage());
		}
	}
	
	
	/*
	 * Conversão MAC Address Linux
	 */
	private final static String linuxParseMacAddress(String ipConfigResponse) throws ParseException {
		String localHost = null;
		try {
			localHost = InetAddress.getLocalHost().getHostAddress();
		} catch(java.net.UnknownHostException ex) {
			ex.printStackTrace();
			throw new ParseException(ex.getMessage(), 0);
		}
		
		StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
		String lastMacAddress = null;
		
		while(tokenizer.hasMoreTokens()) {
			String line = tokenizer.nextToken().trim();
			boolean containsLocalHost = line.indexOf(localHost) >= 0;
			
			// verifica se a linha contém endereço IP
			if(containsLocalHost && lastMacAddress != null) {
				return lastMacAddress;
			}
			
			// verifica se linha contem MAC address
			int macAddressPosition = line.indexOf("HWaddr");
			if(macAddressPosition <= 0) continue;
			
			String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
			if(linuxIsMacAddress(macAddressCandidate)) {
				lastMacAddress = macAddressCandidate;
				continue;
			}
		}
		
		ParseException ex = new ParseException
			("não foi possivel ler o MAC Address de " + localHost + " a partir de [" + ipConfigResponse + "]", 0);
		ex.printStackTrace();
		throw ex;
	}
	
	
	private final static boolean linuxIsMacAddress(String macAddressCandidate) {

		if(macAddressCandidate.length() != 17) return false;
		return true;
	}
	
	
	private final static String linuxRunIfConfigCommand() throws IOException {
		Process p = Runtime.getRuntime().exec("ifconfig");
		InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
		
		StringBuffer buffer= new StringBuffer();
		for (;;) {
			int c = stdoutStream.read();
			if (c == -1) break;
			buffer.append((char)c);
		}
		String outputText = buffer.toString();
		
		stdoutStream.close();
		
		return outputText;
	}
	
	
	
	/*
	 * Solução Windows
	 */
	private final static String windowsParseMacAddress(String ipConfigResponse) throws ParseException {
		String localHost = null;
		try {
			localHost = InetAddress.getLocalHost().getHostAddress();
		} catch(java.net.UnknownHostException ex) {
			ex.printStackTrace();
			throw new ParseException(ex.getMessage(), 0);
		}
		
		StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
		String lastMacAddress = null;
		
		while(tokenizer.hasMoreTokens()) {
			String line = tokenizer.nextToken().trim();
			
			// verifica se a linha contém endereço IP
			if(line.endsWith(localHost) && lastMacAddress != null) {
				return lastMacAddress;
			}
			
			// verifica se linha contem MAC address
			int macAddressPosition = line.indexOf(":");
			if(macAddressPosition <= 0) continue;
			
			String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
			if(windowsIsMacAddress(macAddressCandidate)) {
				lastMacAddress = macAddressCandidate;
				continue;
			}
		}
		
		ParseException ex = new ParseException("não foi possivel ler o MAC Address de [" + ipConfigResponse + "]", 0);
		ex.printStackTrace();
		throw ex;
	}
	
	
	private final static boolean windowsIsMacAddress(String macAddressCandidate) {
		
		if(macAddressCandidate.length() != 17) return false;
		
		return true;
	}
	
	
	private final static String windowsRunIpConfigCommand() throws IOException {
		Process p = Runtime.getRuntime().exec("ipconfig /all");
		InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
		
		StringBuffer buffer= new StringBuffer();
		for (;;) {
			int c = stdoutStream.read();
			if (c == -1) break;
			buffer.append((char)c);
		}
		String outputText = buffer.toString();
		
		stdoutStream.close();
		
		return outputText;
	}
		
	private final static void terminalPrint(String informacao, String SO, String arquitetura, String versaoSO, String IP, String MAC)
	{
		System.out.println();
		System.out.println(informacao);
		System.out.println(SO);
		System.out.println(arquitetura);
		System.out.println(versaoSO);
		System.out.println(IP);
		System.out.println(MAC);		
	
	}
	
	/*
	 * Main
	 */
	public final static void main(String[] args) {
		
		try {
			String informacao = "Informação da Rede";
			String SO = "  Sistema operacional: " + System.getProperty("os.name");
			String arquitetura = "  Arquitetura do sistema operacional: " + System.getProperty("os.arch");
			String versaoSO = "  Versão do sistema operacional: " + System.getProperty("os.version");
			String IP = "  IP/Localhost: " + InetAddress.getLocalHost().getHostAddress();
			String MAC = "  MAC Address: " + getMacAddress();		
			
			terminalPrint(informacao, SO, arquitetura, versaoSO, IP, MAC);
			
		} catch(Throwable t) {
			t.printStackTrace();
		}
		
	}
}

Avise caso de algum resultado.
[]'s

Criado 15 de março de 2007
Ultima resposta 19 de mar. de 2007
Respostas 5
Participantes 3