Verbindungsüberprüfung zu anderem Computer

Hab jetzt eine Überprüfung mit dem "tracert"-Befehl gemacht... das ist aber seeehr langsam (insgesamt ca. 1min).
@FerdiP: Kannst du bitte mal ein Beispiel posten?
 
Java:
InetAddress address = InetAddress.getByName("127.0.0.1");
if (!address.isReachable(1000)) {
    System.out.println("IP ist nicht erreichbar.");
}

Das Programm würde jetzt maximal 1 Sekunde warten, bis dein eigener Rechner antwortet.. ;)
 
Schade, es wird von meiner Firewall geblockt...

Kennt jemand einen Befehl (cmd/Java/C++), der schnell und nur den Hostnamen durch eine IP-Adresse herausfindet?
 
Ich denke die Java-Variante macht anschließend auch nur native den Lookup:

Java:
            String hostName = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }).getCanonicalHostName();
            System.out.println(hostName);
 
Zuletzt bearbeitet von einem Moderator:
Ja, aber sie funktioniert mit meiner Firewall nicht... Das einzige, was da durchkommt, ist die Hostnamen-Auflösung der Befehle tracert und ping.
Weiß vielleicht jemand den Code von dem tracert oder dem ping?
Wichtig: Bitte keine nachprogrammierten "Ersatzcodes"
 
Hab jetzt endlich eine Lösung gefunden, alle Computer mit Zustand und trotz Firewall zu finden...
Hier mein Code:

Code:
// RAHN.cpp : Resolve All HostNames
//

#include "stdafx.h"
#include <iostream>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <conio.h>
using namespace std;
#pragma comment(lib, "IPHLPAPI.lib")
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "ws2_32.lib")

#define BUFFER_SIZE 1024

bool testConnection(char *ip)
{
    DWORD dwRetVal;
    ULONG MacAddr[2];       /* for 6-byte hardware addresses */
    ULONG PhysAddrLen = 6;  /* default to length of six bytes */
    IPAddr DestIp = inet_addr(ip);
    memset(&MacAddr, 0xff, sizeof (MacAddr));
    dwRetVal = SendARP(DestIp, 0, &MacAddr, &PhysAddrLen);
    if (dwRetVal == NO_ERROR) {
        return true;
    } else {
        return false;
    }
    return false;
}

char *getHostName(char *ip){
	WSADATA wsaData;
    int iResult;
    DWORD dwRetval;
    struct sockaddr_in saGNI;
    char hostname[NI_MAXHOST];
    char servInfo[NI_MAXSERV];
	u_short port = 80;
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        return ip;
    }
	saGNI.sin_family = AF_INET;
    saGNI.sin_addr.s_addr = inet_addr(ip);
    saGNI.sin_port = htons(port);
	dwRetval = getnameinfo((struct sockaddr *) &saGNI,
                           sizeof (struct sockaddr),
                           hostname,
                           NI_MAXHOST, servInfo, 
                           NI_MAXSERV, NI_NUMERICSERV);
	if (dwRetval == 0) {
		return hostname;
	}
	return ip;
}

int main(int argc, char **argv)
{
	bool on = false;
	if(argc==2){
		if(strcmp(argv[1], "onlyNames")==0){
			on = true; //"onlyNames" option
		}
	}
	int t = clock()/(CLOCKS_PER_SEC/1000);
	char* half_ip = "192.168.178";
	char ip[16];
	char partD[4];
	char* ret;
	char out[1000000];
	strcpy(out, " ");
	if(!on){
		system("title Resolve All HostNames");
		strcat(out, "\n\n Gefundene Computer im Netzwerk:\n\n");
		strcat(out, " IP-Adresse:\t| Zustand:\t| Hostname:\n");
		strcat(out, " --------------------------------------------------\n");
	}
	printf("\n");
	for(int i = 0; i<256; i++){
		if(!on){
			printf(" %i%%\r", (i*100)/256+1);
		}
		strcpy(ip, half_ip);
		strcat(ip, ".");
		sprintf(partD, "%i", i);
		strcat(ip, partD);
		ret = getHostName(ip);
		bool active;
		const char* activity;
		if(ret!=NULL&&strcmp(ret, ip)!=0){
			if(!on){
				strcat(out, " ");
				strcat(out, ip);
				strcat(out, "\t| ");
				if(strcmp(partD, "1")==0){
					strcat(out, "aktiv  "); //Router muss aktiv sein
				}else{
					active = testConnection(ip);
					activity = (active?"aktiv  ":"inaktiv");
					strcat(out, activity);
				}
				strcat(out, "\t| ");
				strcat(out, ret);
				strcat(out, "\n");
			}else{
				printf("%s:\t%s\n\r", ip, ret);
			}
		}
	}
	if(!on){
		strcat(out, "\n Verbrauchte Zeit: ");
		sprintf(out, "%s%i Millisekunden", out, (clock()/(CLOCKS_PER_SEC/1000))-t);
		strcat(out, "\n\n Bitte druecken Sie eine Taste, um das Programm zu beenden...");
		cout<<out;
		getch();
		printf("\n");
	}
	WSACleanup();
	return 0;
}

Zum Starten entweder "RAHN" für alle Ausgaben, oder "RAHN onlyNames" für IP und Name. Viel Spaß damit! :)
 
Ich hab noch eine zweite Version meines Programms (C und C++ gemischt :D) geschrieben, hier der Code.
Wichtig: Das funktioniert nur bei den IP-Adressen, die im String half_ip festgelegt werden, also bei "192.168.178" nur zb. 192.168.178.1 bis 192.168.178.255. Andernfalls einfach ändern...
Viel Spaß! :)
Im Anhang sind die kompilierten Programme (1 und 2)...

Code:
// RAHN2.cpp

#include "stdafx.h"
#include <iostream>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <conio.h>
using namespace std;
#pragma comment(lib, "IPHLPAPI.lib")
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "ws2_32.lib")

#define BUFFER_SIZE 1024

char *myIp(){
    WSADATA wsaData;
    WORD wVer = MAKEWORD(2, 0);
    WSAStartup( wVer, &wsaData );
    char hostname[255];
    gethostname(hostname, sizeof(hostname));
    return inet_ntoa(*reinterpret_cast<in_addr*>(*gethostbyname(hostname)->h_addr_list));
}

char *macAddr(char *Ip)
{
    DWORD dwRetVal;
    IPAddr DestIp = 0;
    IPAddr SrcIp = 0;
    ULONG MacAddr[2];
    ULONG PhysAddrLen = 6;

    BYTE *bPhysAddr;
    DestIp = inet_addr(Ip);
    SrcIp = inet_addr(NULL);

    //memset(&MacAddr, 0xff, sizeof (MacAddr));

    dwRetVal = SendARP(DestIp, SrcIp, &MacAddr, &PhysAddrLen);

    char add[3];
    static char Ret[18];
    sprintf(Ret, "");

    if (dwRetVal == NO_ERROR) {
        bPhysAddr = (BYTE *) & MacAddr;
        if (PhysAddrLen) {
            for (int i = 0; i < (int) PhysAddrLen; i++) {
                sprintf(add, "%.2X", (int) bPhysAddr[i]);
                strcat(Ret, add);
                if(i<((int)PhysAddrLen-1)){
                    strcat(Ret, "-");
                }
            }
        } else {
            return "ERROR";
        }
    } else {
        return "ERROR";
    }
    return Ret;
}

bool testConnection(char *ip)
{
    DWORD dwRetVal;
    ULONG MacAddr[2];        /* for 6-byte hardware addresses */
    ULONG PhysAddrLen = 6;  /* default to length of six bytes */
    IPAddr DestIp = inet_addr(ip);
    memset(&MacAddr, 0xff, sizeof (MacAddr));
    dwRetVal = SendARP(DestIp, 0, &MacAddr, &PhysAddrLen);
    if (dwRetVal == NO_ERROR) {
        return true;
    } else {
        return false;
    }
    return false;
}

char *getHostName(char *ip){
    WSADATA wsaData;
    int iResult;
    DWORD dwRetval;
    struct sockaddr_in saGNI;
    char hostname[NI_MAXHOST];
    char servInfo[NI_MAXSERV];
    u_short port = 80;
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        return ip;
    }
    saGNI.sin_family = AF_INET;
    saGNI.sin_addr.s_addr = inet_addr(ip);
    saGNI.sin_port = htons(port);
    dwRetval = getnameinfo((struct sockaddr *) &saGNI,
                            sizeof (struct sockaddr),
                            hostname,
                            NI_MAXHOST, servInfo, 
                            NI_MAXSERV, NI_NUMERICSERV);
    if (dwRetval == 0) {
        return hostname;
    }
    return ip;
}

int main(int argc, char **argv)
{
    int t = clock()/(CLOCKS_PER_SEC/1000);
    char* half_ip = "192.168.178";
    char ip[16];
    char partD[4];
    char* ret;
    char out[1000000];
    system("title Resolve All HostNames 2.0");
    strcpy(out, " ");
    printf("\n");
    for(int i = 0; i<256; i++){
        if(i<255){
            printf(" %i%%\r", (int)((i*100)/256+1));
        }else{
            printf(" 100%%");
        }
        strcpy(ip, half_ip);
        strcat(ip, ".");
        sprintf(partD, "%i", i);
        strcat(ip, partD);
        ret = getHostName(ip);
        bool active;
        const char* activity;
        if(ret!=NULL&&strcmp(ret, ip)!=0){
            strcat(out, " \n\n");
            strcat(out, " IP:\t\t");
            strcat(out, ip);
            strcat(out, "\n Hostname:\t");
            strcat(out, ret);
            strcat(out, "\n Aktivitaet:\t");
            if(strcmp(partD, "1")==0){
                active = true;
                strcat(out, "aktiv  "); //Router muss aktiv sein
            }else{
                active = testConnection(ip);
                activity = (active?"aktiv  ":"inaktiv");
                strcat(out, activity);
            }
            strcat(out, "\n MAC-Adresse:\t");
            if(active){
                ret = macAddr(ip);
                //strcat(out, ip);
                //cout<<endl<<ip<<endl;
                strcat(out, ret);
            }else{
                //strcat(out, "(unbekannt)");
                strcat(out, "(unbekannt)        ");
            }
            strcat(out, "\n");
        }
    }
    strcat(out, "\n\n Verbrauchte Zeit: ");
    sprintf(out, "%s%i Millisekunden", out, (clock()/(CLOCKS_PER_SEC/1000))-t);
    strcat(out, "\n\n\n Bitte druecken Sie eine Taste, um das Programm zu beenden...");
    cout<<out;
    getch();
    printf("\n");
    WSACleanup();
    exit(0);
    return 0;
}
 

Anhänge

  • RAHN.zip
    119,2 KB · Aufrufe: 15
Zuletzt bearbeitet:
Zurück