Détecter @ip,@mac des pcs connectés au lan avec java?

Résolu
cs_hamines Messages postés 11 Date d'inscription lundi 21 janvier 2008 Statut Membre Dernière intervention 25 mai 2009 - 21 mars 2009 à 14:20
RaSdab Messages postés 27 Date d'inscription mercredi 6 avril 2011 Statut Membre Dernière intervention 30 septembre 2011 - 13 avril 2011 à 01:04
Bonjour,
si vous pouvez m'aider à détecter les @ip et les @mac des pcs connectés à un lan à un moment donné: j'ai besoin d'un programme en java qui fait ça...
j'ai une idée de lancer un processus de ping sur une plage d'adresse puis utiliser la commande ARP -a qui retourne la table arp... mais comment le faire en java
merci d'avance pour votre aide
A+

7 réponses

kohntark Messages postés 3705 Date d'inscription lundi 5 juillet 2004 Statut Membre Dernière intervention 27 avril 2012 30
21 mars 2009 à 14:30
Salut,

mais comment le faire en java
=> en postant dans le forum java
Le thème actuel porte à confusion, mais sauf erreur de ma part, il est destiné à donner des idées de nouvelles rubriques pour le forum.

Cordialement,

Kohntark -
3
cs_samiad Messages postés 20 Date d'inscription lundi 14 juillet 2008 Statut Membre Dernière intervention 16 décembre 2009
7 mai 2009 à 17:06
Bonjour,
j'ai le méme probléme si vous avez une solution répond moi c'est urgent merci d'avance
0
sadikizaydane Messages postés 3 Date d'inscription mercredi 12 janvier 2011 Statut Membre Dernière intervention 12 avril 2011
4 avril 2011 à 04:08
bon pour vous répondre je me demande bien si vous connaissez jpcap ou pas ?
en tout cas la solution est facile et passe par 3 étapes:
1 - installer winpcap (telecharger winpcap à google)
2 - installer jpcap.
3 - chercher le tutorial de jpcac à google ou bien au site: www.siteduzero.com.

et c fini .
0
RaSdab Messages postés 27 Date d'inscription mercredi 6 avril 2011 Statut Membre Dernière intervention 30 septembre 2011
12 avril 2011 à 19:51
bonjour,je vous en pris de m'envoyer la solution si vous l'avez trouvé je suis bloqué.
je serai reconnaissant.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
sadikizaydane Messages postés 3 Date d'inscription mercredi 12 janvier 2011 Statut Membre Dernière intervention 12 avril 2011
12 avril 2011 à 21:10
passe moi ton adresse E-mail
0
sadikizaydane Messages postés 3 Date d'inscription mercredi 12 janvier 2011 Statut Membre Dernière intervention 12 avril 2011
12 avril 2011 à 21:25
il faut utiliser ces 4 classes:
*GestionRecuperationinformation dont le programme est le suivant:



import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;


import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.ARPPacket;
import jpcap.packet.EthernetPacket;

public class GestionRecuperationInformation {

public ArrayList listeDesPcsEnReseaux = new ArrayList() ;
public jpcap.NetworkInterface bestNetworkInterface ;
public byte[] myAddressIp ;
public byte[] addressBroadcast ;
public byte[] subnet ;


public GestionRecuperationInformation() {

bestNetworkInterface = getBestNetworkInterface() ;
NetworkInterfaceAddress bestNetworkInterfaceAddress = getBestNetworkInterfaceAddresses(bestNetworkInterface.addresses) ;
myAddressIp = bestNetworkInterfaceAddress.address.getAddress() ;
addressBroadcast = bestNetworkInterfaceAddress.broadcast.getAddress() ;
subnet = bestNetworkInterfaceAddress.subnet.getAddress() ;
}

public ArrayList getListeDesPcsEnReseaux() {
return listeDesPcsEnReseaux;
}

public String getMyAdressIp() {
String valeurRetourner = null ;
try {
Enumeration<NetworkInterface> listeDesInterfaceNetwork = NetworkInterface.getNetworkInterfaces() ;
while(listeDesInterfaceNetwork.hasMoreElements()){
NetworkInterface interfaceN = listeDesInterfaceNetwork.nextElement() ;
Enumeration listeDesAdressesIps = interfaceN.getInetAddresses() ;
while(listeDesAdressesIps.hasMoreElements()){
InetAddress adresseIpATester = listeDesAdressesIps.nextElement() ;
if(adresseIpATester instanceof Inet4Address && (!adresseIpATester.getHostAddress().equals("127.0.0.1")))
valeurRetourner = adresseIpATester.getHostAddress() ;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return valeurRetourner ;
}



public String getMyPcName(){
String valeurRetourner = null ;
try {
valeurRetourner = InetAddress.getLocalHost().getHostName() ;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return valeurRetourner;
}

public String getPcName(String address){
String valeurRetourner = null ;
try {
valeurRetourner = InetAddress.getByName(address).getHostName() ;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return valeurRetourner;
}

public ArrayList getListingReseaux(){
ArrayList valeurRetourner = listeDesPcsEnReseaux ;
try {
Thread threadEcouteurARP = null ;
JpcapCaptor captor = JpcapCaptor.openDevice(bestNetworkInterface, 65000, false, 100) ;
JpcapSender sender = captor.getJpcapSenderInstance() ;

threadEcouteurARP = new ThreadEcouteurARP(this, captor) ;
threadEcouteurARP.start() ;

ARPPacket arp = new ARPPacket() ;
byte[] adressBroadcast = {(byte)255,(byte)255,(byte)255,(byte)255,(byte)255,(byte)255} ;
arp.hardtype = ARPPacket.HARDTYPE_ETHER ;
System.out.println("Type:" +ARPPacket.HARDTYPE_ETHER );
arp.operation = ARPPacket.ARP_REQUEST ;
System.out.println("Operation" +ARPPacket.ARP_REQUEST);
arp.prototype = ARPPacket.PROTOTYPE_IP ;
System.out.println("Prototype:" + ARPPacket.PROTOTYPE_IP);
arp.sender_hardaddr = bestNetworkInterface.mac_address ;
System.out.println("Adresse MAC:"+ bestNetworkInterface.mac_address);
arp.sender_protoaddr = myAddressIp ;
System.out.println("Mon IP:" +myAddressIp);
arp.target_hardaddr = adressBroadcast ;
arp.hlen = 6 ;
System.out.println("hlen:" +arp.hlen);
arp.plen = 4 ;
System.out.println("plen:" +arp.plen);


EthernetPacket ethernet = new EthernetPacket() ;
ethernet.dst_mac = adressBroadcast ;
ethernet.frametype = EthernetPacket.ETHERTYPE_ARP ;
ethernet.src_mac = bestNetworkInterface.mac_address ;

arp.datalink = ethernet ;
byte[] addressReseaux = getAdresseReseaux() ;
int IPMax = addressBroadcast[3]&0xff ;
for(int j = 0; j < IPMax; j++){
addressReseaux[3]++;

arp.target_protoaddr = addressReseaux ;
sender.sendPacket(arp) ;
}
while(threadEcouteurARP.isAlive()){}
sender.close() ;
}catch (Exception e) {
e.printStackTrace() ;
}

return valeurRetourner ;

}

public jpcap.NetworkInterface getBestNetworkInterface(){
jpcap.NetworkInterface valeurRetourner = null ;
jpcap.NetworkInterface[] listeDesInterfaces = JpcapCaptor.getDeviceList() ;
loop: for(jpcap.NetworkInterface interfaceN : listeDesInterfaces){
for(NetworkInterfaceAddress addressIPParInterface : interfaceN.addresses){
if(!(addressIPParInterface.address instanceof Inet4Address)) continue ;
if(addressIPParInterface.address.getHostAddress().equals(getMyAdressIp())){
valeurRetourner = interfaceN ;
break loop ;
}
}
}
return valeurRetourner ;
}

public NetworkInterfaceAddress getBestNetworkInterfaceAddresses(jpcap.NetworkInterfaceAddress[] listeDesAddresses){
NetworkInterfaceAddress valeurRetourner = null ;
loop: for(NetworkInterfaceAddress addressIPParInterface : listeDesAddresses){
if(!(addressIPParInterface.address instanceof Inet4Address)) continue ;
if(addressIPParInterface.address.getHostAddress().equals(getMyAdressIp())){
valeurRetourner = addressIPParInterface ;
break loop ;
}

}
return valeurRetourner ;
}

public byte[] getAdresseReseaux(){
byte[] valeurRetourner = new byte[4];
for(int i =0 ; i < 4; i++){
valeurRetourner[i] += myAddressIp[i] & subnet[i];
}
return valeurRetourner ;
}

public boolean posteDejaPresent(String addressMac){
for(InformationPoste posteTrouver : listeDesPcsEnReseaux){
if(posteTrouver.getAdresseMac().equals(addressMac))
return true ;
}
return false ;
}




*InformationPoste dont le programme est:


public class InformationPoste {
private String ip ;
private String adresseMac ;
private String interfaceReseaux ;
private String hostName ;
public InformationPoste(String ip, String adresseMac,
String interfaceReseaux, String hostName) {
this.ip = ip;
this.adresseMac = adresseMac;
this.interfaceReseaux = interfaceReseaux;
this.hostName = hostName;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getAdresseMac() {
return adresseMac;
}
public void setAdresseMac(String adresseMac) {
this.adresseMac = adresseMac;
}
public String getInterfaceReseaux() {
return interfaceReseaux;
}
public void setInterfaceReseaux(String interfaceReseaux) {
this.interfaceReseaux = interfaceReseaux;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}

@Override
public String toString() {

return "Hostname : "+ getHostName() +"\n Ip : " + getIp() + "\n Adresse Mac : " + getAdresseMac() + "\n Interface reseaux : " + getInterfaceReseaux();
}
}
*ThreadEcouteurARP:



import java.io.IOException;
import java.net.InetAddress;

import javax.swing.Timer;

import jpcap.JpcapCaptor;
import jpcap.packet.ARPPacket;
import jpcap.packet.Packet;

public class ThreadEcouteurARP extends Thread {

private GestionRecuperationInformation gestionReseaux ;
private JpcapCaptor captor ;

public ThreadEcouteurARP(GestionRecuperationInformation gestionReseaux, JpcapCaptor captor) {
this.gestionReseaux = gestionReseaux ;
this.captor = captor ;
}

@Override
public void run() {
Timer timer = new Timer(10000, null) ;
timer.setRepeats(false) ;
timer.start() ;
try {
while(timer.isRunning()){
Packet possibleARP = captor.getPacket() ;
if(possibleARP instanceof ARPPacket){
ARPPacket reponceARP = (ARPPacket) possibleARP ;
if(reponceARP.operation == ARPPacket.ARP_REPLY){
InetAddress addressTrouver = InetAddress.getByAddress(reponceARP.sender_protoaddr);
String addressMac = reponceARP.getSenderHardwareAddress().toString() ;
if(!gestionReseaux.posteDejaPresent(addressMac))
gestionReseaux.getListeDesPcsEnReseaux().add(new InformationPoste(addressTrouver.getHostAddress(),addressMac,gestionReseaux.bestNetworkInterface.name,addressTrouver.getHostName())) ;
}
}
}
captor.close() ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


*Main:

import java.io.IOException;

import jpcap.NetworkInterfaceAddress;


public class Main {

public static void main(String[] args) throws IOException {

GestionRecuperationInformation gestionRecuperationInformation=new GestionRecuperationInformation();
System.out.println("Chargement...");
@SuppressWarnings("unused")
NetworkInterfaceAddress[] listeDesAddresses = null;
System.out.println(gestionRecuperationInformation.getListingReseaux());

}

}
0
RaSdab Messages postés 27 Date d'inscription mercredi 6 avril 2011 Statut Membre Dernière intervention 30 septembre 2011
13 avril 2011 à 01:04
The import jpcap cannot be resolved.
j'ai télécharger "jpcapSetup-0.6.exe"et je l'ai installé mais je ne sait pas comment l'utiliser.
s'il vous plait je suis débutant en java guider moi je serai reconnaissant.
0
Rejoignez-nous