egonfrenz
Goto Top

Hostname bzw IP-Adresse in Excel eintragen

Hallo Irgendwie gelingt es mir nicht den Hostnamen oder die ip Adresse von nslookup in mein Excel einzufügen ;.(

' IP to Hostname --- or --- Hostname to IP   
'     
' Version 1.0 26.09.2016 Egon H. Frenz   
'   
Dim strHostname, strIP, strPingResult, IntLatency , a , ZielDatei, WshShell
 
intRow = 2 
Set objExcel = CreateObject("Excel.Application")   
 
With objExcel 
     
    .Visible = True 
    .Workbooks.Add 
     
    .Cells(1, 1).Value = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"   
    .Cells(1, 2).Value = "XXXXXXXXXXXXXX"   
    .Cells(1, 3).Value = "XX.XX.XXXX "  
'   .Cells(1, 4).Value = "                "   
     
    .Range("A1:D1").Select   
    .Cells.EntireColumn.AutoFit 
     
    .Cells(1, 1).Value = "Hostname"   
    .Cells(1, 2).Value = "IP"  
    .Cells(1, 3).Value = date()
         
End With  

'--- Input Text File with either Hostames or IP's ---   

Set Fso = CreateObject("Scripting.FileSystemObject")   
a = inputbox("Gebe die IPadresse / Hostnamen an !")  
Set fso = CreateObject("Scripting.FileSystemObject")  
ZielDatei = "c:\snippings\test.txt"  
Set FileOut = fso.OpenTextFile( ZielDatei, 8, true)
fileOut.WriteLine a

Set InputFile = fso.OpenTextFile("c:\snippings\test.Txt")   
 
Do While Not (InputFile.atEndOfStream) 
     
    strHostname = InputFile.ReadLine 
     
    Set WshShell = WScript.CreateObject("WScript.Shell")   
     
    Call PINGlookup( strHostname, strIP, strPingResult, intLatency ) 
     
    With objExcel 
        .Cells(intRow, 1).Value = strHostname 
        .Cells(intRow, 2).Value = strIP 
'       .Cells(intRow, 3).Value = für später   
'       .Cells(intRow, 4).Value = für später   
    End With 
     
    intRow = intRow + 1 
     
Loop 
 
With objExcel 
    .Range("A1:D1").Select   
    .Selection.Interior.ColorIndex = 19 
    .Selection.Font.ColorIndex = 11 
    .Selection.Font.Bold = True 
    .Cells.EntireColumn.AutoFit 
End With 

 
'------------- Subrutines and Functions ----------------   
 
Sub PINGlookup(ByRef strHostname, ByRef strIP, ByRef strPingResult, ByRef intLatency )  
    ' Both IP address and DNS name is allowed    
    ' Function will return the opposite    
     
    ' Check ob der Hostname eine IP ist   
    Set oRE = New RegExp  
    oRE.Pattern = "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"    
     
    ' Sort out if IP or Hostname   
    strMachine = strHostname 
    bIsIP = oRE.Test(strMachine)  
    If bIsIP Then  
        strIP = strMachine 
        strHostname = "-------"   
    Else 
        strIP = "-------"   
        strHostname = strMachine 
    End If  
     
     
    ' Get a temp filename and open it   
    Set osShell = CreateObject("Wscript.Shell")   
    Set oFS = CreateObject("Scripting.FileSystemObject")    
    sTemp = osShell.ExpandEnvironmentStrings("%TEMP%")    
    sTempFile = sTemp & "\" & oFS.GetTempName    
     
    ' PING and check if the IP exists   
    intT1 = Fix( Timer * 1000 )  
    osShell.Run "%ComSpec% /c ping -a " & strMachine & " -n 1 > " & sTempFile, 0, True   
    intT2 = Fix( Timer * 1000 )  
    intLatency = Fix( intT2 - intT1 ) / 1000 
     
     
    ' Open the temp Text File and Read out the Data    
    Set oTF = oFS.OpenTextFile(sTempFile)  
     
    ' Parse the temp text file    
    strPingResult = "-------" 'assume failed unless...   
    Do While Not oTF.AtEndoFStream  
         
        strLine = Trim(oTF.Readline)  
        If strLine = "" Then    
            strFirstWord = ""   
        Else  
            arrStringLine = Split(strLine, " ", -1, 1)   
            strFirstWord = arrStringLine(0) 
        End If  
         
        Select Case strFirstWord 
             
            Case "Pinging"    
                If arrStringLine(2) = "with" Then   
                    strPingResult = "-------"   
                    strHostname = "-------"   
                Else 
                    strHostname = arrStringLine(1) 
                    strIP = arrStringLine(2) 
                    strLen = Len( strIP ) - 2 
                    strIP = Mid( strIP, 2, strLen ) 
                    strPingResult = "Ok"   
                End If  
                Exit Do             
            'End Case   
             
            Case "Ping" ' pinging non existent hostname   
                strPingResult = "------"   
                Exit Do     
            'End Case    
                 
        End Select 
         
    Loop  
     
    'Close it    
    oTF.Close  
    'Delete It    
    oFS.DeleteFile sTempFile  
     
     
End Sub
msgbox a 
Set WshShell = WScript.CreateObject ("WScript.Shell")  
WshShell.Run "C:\windows\system32\nslookup.exe"& a  
WshShell = Nothing

Was mache ich nicht richtig ?
Kommentar vom Moderator Frank am Sep 28, 2016 um 09:46:05 Uhr
Beitrag zum richtigen Thema verschoben und Doppelpost gelöscht

Content-Key: 316407

Url: https://administrator.de/contentid/316407

Printed on: April 20, 2024 at 03:04 o'clock

Member: aqui
aqui Sep 28, 2016 at 07:50:44 (UTC)
Goto Top
Das wäre doch sinnvollerweise ein Beitrag für die Rubrik Batch & Shell als hier !!
So lesen es auch die relevanten Leute !
(Kann man mit dem Bearbeiten Button übrigens nich verschieben face-wink )
Member: Spirit-of-Eli
Spirit-of-Eli Sep 28, 2016 updated at 17:01:19 (UTC)
Goto Top
Fernab von der Tatsache, dass ich aqui zustimme. Nimm dafür einfach nen Network Scanner und exportiere die Liste.

http://www.chip.de/downloads/SoftPerfect-Network-Scanner_13014223.html
Member: aqui
aqui Sep 28, 2016 at 17:20:19 (UTC)
Goto Top
Der hier kann es auch...
https://inetapp.de/de/inetx.html
Member: EgonFrenz
EgonFrenz Sep 28, 2016 at 17:57:49 (UTC)
Goto Top
Sorry aber ich möchte das meinen Scrip überlassen und keine fremd Software einsetzen.
Member: EgonFrenz
EgonFrenz Sep 28, 2016 at 18:02:09 (UTC)
Goto Top
nein ich möchte keine fremde Software sonder jemanden der weiß was er tut ohne im Netz nach ähnlicher Software zu suchen das kann ich auch.
Ich möchte das dieses Script läuft es tut ja schon was es soll nur noch nicht am Ende
Member: EgonFrenz
EgonFrenz Sep 28, 2016 at 18:06:07 (UTC)
Goto Top
probier das mal aus und verstehe das andere Programme nicht das machen was diese Skript macht.
Member: EgonFrenz
EgonFrenz Sep 28, 2016 updated at 18:10:48 (UTC)
Goto Top
kann der networkscanner die ip Adresse die ich in einer Inputbox eingegeben habe in den hostnamen umwandeln und diese 2 dann in ein excelsheet eintragen ?
Member: Pjordorf
Pjordorf Sep 28, 2016 at 18:21:50 (UTC)
Goto Top
Hallo,

Zitat von @EgonFrenz:
Hallo Irgendwie gelingt es mir nicht den Hostnamen oder die ip Adresse von nslookup in mein Excel einzufügen ;.(
Was funktioniert denn nicht oder wonach müssen wir suchen ohne dein Excel VBA zeile für zeile durch zu tickern und uns alle Variablen einzeln anzuschauen? Geht nicht ist ein weiter Begriff.

Ich möchte das dieses Script läuft es tut ja schon was es soll nur noch nicht am Ende
Was denn nun. Was tuts denn nicht?

Gruß,
Peter
Member: EgonFrenz
EgonFrenz Sep 28, 2016 at 18:25:50 (UTC)
Goto Top
die Rückgabe von nslookup des hostnamen den ich in der variable a zugewiesen habe.
Lass das Script mal laufen dann wirst du sehen was ich meine

Gruß Egon
Member: EgonFrenz
EgonFrenz Sep 28, 2016 at 18:42:18 (UTC)
Goto Top
Ich möchte erreichen das in einer Inputbox die ip bzw der hostname eingegeben wird
dann soll entweder der hostname oder die ip von nslookup aufglöst werden.
Beide sollen dann anschließend in ein excelsheet eingetragen werden.

ip Hostname
127.0.0.1 localhost
172.217.17.35 ams16s29-in-f3.1e100.net

dies wird in ein txt file geschrieben was dann um jeden eintrag ergänzt wird
um es anschließend in ein excelshett zu schreiben.
bis jetzt geht das aber nur immer mit einem von beiden.
Es fehlt die Auflösung von nslookup
Member: TsukiSan
TsukiSan Oct 01, 2016 at 05:59:14 (UTC)
Goto Top
Hallo EgonFrenz,

Vielleicht hilft dir mein Hinweis:
Deine Zeilen code stimmen nicht mit dem überein, was Du haben möchtest.
Grob aufgegliedert:
1) du schreibst "X" in entsprechende Zeilen
2) Aufruf der Eingabebox
3) Wert von Eingabebox wird in die Datei Test.txt geschrieben
4) Dann werden alle Daten, die gespeichert sind angepingt und Daten entsprechend in dein Excelsheet eingetragen
5) Mittels Messagebox die Daten von der Eingabebox angezeigt
6) die Daten von "a" mittels nslookup "zerlegt"
7) fertig (hier passiert nichts mehr).

Gruß
Tsuki
Mitglied: 77559
77559 Oct 03, 2016 at 22:24:54 (UTC)
Goto Top
Hallo Egon,

du bestehst darauf das jemand, der Ahnng hat, seine Zeit opfert und deinen von mehreren Stellen zusammengeklopften Code überprüft.

Mein nicht so überheblicher Ansatz wäre ja mich reinzuknien und die (eigenen) Fehler zu suchen und zu beheben.


Wenn du Ping mit -a aufrufst brauchst du eigentlich nslookup nicht mehr. In der Ausgabe strht so denn erreichbar der hostname gefolgt von der IPv4 in eckigen Klammern.

> Ping -n 3 -w 500 -4 -a  8.8.8.8

Ping wird ausgeführt für google-public-dns-a.google.com [8.8.8.8] mit 32 Bytes Daten:
Zeitüberschreitung der Anforderung.
Antwort von 8.8.8.8: Bytes=32 Zeit=24ms TTL=48
Antwort von 8.8.8.8: Bytes=32 Zeit=24ms TTL=48

Ping-Statistik für 8.8.8.8:
    Pakete: Gesendet = 3, Empfangen = 2, Verloren = 1
    (33% Verlust),
Ca. Zeitangaben in Millisek.:
    Minimum = 24ms, Maximum = 24ms, Mittelwert = 24ms

Du brauchst also nur eine RegExp mit Submatches um die Daten filern.

Die WShShell.run Methode ist da weniger geeignet als die .Exec
welche dir ohne die Fummelei mit externen Dateien einen Stream liefert.

Dein script hat mich nach längerer MS-Office Abstinenz veranlasst eine stark vreänderte (und funktionierende) Version zu schreiben.

Da Subs nicht im linearen Ablauf benutzt werden verwirrt es nur wenn du davor und dahinter normalen Code plazierst.

Hier meine Version

Gruß LotPings

'
' IP to Hostname --- or --- Hostname to IP   
' Version 1.0 26.09.2016 Egon H. Frenz   
'         1.xx LotPings 2016-10-03 22:40  
Option Explicit
Dim sHostname, sMachine, sIP, sPingResult, iLatency, InputHost, HostList
Dim iRow, oExcel, oFS, FileOut, InputFile

Dim oRE1, cRE1matches, RE1match : Set oRE1 = New RegExp  
oRE1.Pattern = " ([^ ]+) \[([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\]"  
Dim oRE2, cRE2matches, RE2match  : Set oRE2 = New RegExp  
oRE2.Pattern = "Minimum = (\d+)ms, Maximum = (\d+)ms, Mittelwert = (\d+)ms"  
Dim oSH     : Set oSH   = WScript.CreateObject("WScript.Shell")  
Dim sHostDom, sHost, sPing, sPingOut : sPingOut = ""  

HostList = "c:\test\hostlist.txt"  

'--- Input Text File with either Hostames or IP's ---   
InputHost = inputbox("Gebe die IPadresse / Hostnamen an !")  

Set oFS = CreateObject("Scripting.FileSystemObject")  
Set FileOut = oFS.OpenTextFile( HostList, 8, true)
FileOut.WriteLine InputHost
FileOut.Close

iRow = 2 
Set oExcel = CreateObject("Excel.Application")   
With oExcel 
    .Visible = True 
    .Workbooks.Add 
    .Cells(1, 1).Value = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"   
    .Cells(1, 2).Value = "XXXXXXXXXXXXXX"   
    .Cells(1, 3).Value = "XX.XX.XXXX XX:XX"  
'   .Cells(1, 4).Value = "                "   
     
    .Range("A1:D1").Select   
    .Cells.EntireColumn.AutoFit 
     
    .Cells(1, 1).Value = "Hostname"   
    .Cells(1, 2).Value = "IP"  
    .Cells(1, 3).Value = Now
    .Cells(1, 4).Value = "Avg ms"  
    
End With  

Set InputFile = oFS.OpenTextFile(HostList) 
Do While Not (InputFile.atEndOfStream) 
    sMachine = InputFile.ReadLine 

    Call PINGlookup(sMachine, sHostname, sIP, sPingResult, iLatency ) 

    With oExcel 
        .Cells(iRow, 1).Value = sHostname 
        .Cells(iRow, 2).Value = sIP 
        .Cells(iRow, 3).Value = sPingResult
        .Cells(iRow, 4).Value = iLatency
    End With 
    iRow = iRow + 1 
Loop 
 
With oExcel 
    .Range("A1:D1").Select   
    .Selection.Interior.ColorIndex = 19 
    .Selection.Font.ColorIndex = 11 
    .Selection.Font.Bold = True 
    .Cells.EntireColumn.AutoFit 
End With 

'------------- Subroutines and Functions ----------------   
Sub PINGlookup(ByRef sMachine ,ByRef sHostname, ByRef sIP, ByRef sPingResult, ByRef iLatency )  
    ' Use Shell.Exec instead of .run to avoid writing to file  
    sPingOut = "":sHostname="":sIP=""  
    sPing = "cmd /c Ping -n 3 -w 500 -4 -a " & sMachine  
    Dim oExec   : Set oExec = oSH.Exec(sPing)
    Do While Not oExec.StdOut.AtEndOfStream
        sPingOut = sPingOut & oExec.StdOut.ReadLine() & vbCrLf
    Loop
    ' Use RegExp 1 to match "hostname [123.123.123.123]" in sPingOUt  
    set cRE1matches = oRE1.Execute(sPingOut)
    if cRE1matches.Count > 0 Then
        Set RE1match= cRE1matches(0)
        sHostDom = RE1match.Submatches(0)
        sHostName = Split(sHostDom, ".")  
        sIP = RE1match.Submatches(1)
        sPingResult = "ok"  
    Else
        sPingResult = "err " & sMachine  
    End if
    ' Use RE2 to match min max avg in sPingOut  
    set cRE2matches = oRE2.Execute(sPingOut)
    if cRE2matches.Count > 0 Then
        Set RE2match = cRE2matches(0)
        iLatency = RE2match.Submatches(2)
    Else
        iLatency = "n/a"  
    End If
End Sub '  

Die Excel Tabelle sieht etwa so aus:

Hostname IP 2016-10-04 00:07 Avg ms
google-public-dns-a 8.8.8.8 ok 24
HPn54L 192.168.3.91 ok 0
Zyxel-24G-Switch 192.168.3.1 ok 0
HPojPro8500plus 192.168.3.144 ok 0
TMDat1 192.168.3.167 ok 0
Fuji-Win10-64 192.168.3.190 ok 0
iMac24HD 192.168.3.124 ok 0
err McMini n/a
HP-G1610 192.168.3.176 ok 0
err 192.168.3.230 0
fritz 192.168.3.250 ok 0
HomeMatic-CCU 192.168.3.254 ok 0
err 192.168.3.142 n/a