Problems with nslookup writing to file Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
vahju
Posts: 27
Joined: 30 Sep 2013, 16:09

Problems with nslookup writing to file

15 Mar 2018, 15:36

I am trying to loop through a list of IP addresses and get the output of nslookup results into a text for each IP.
Once I have all the files I can do some Excel/Power Query foo to get the results.

The problem I am running into is the output only provides last two lines of the first IP address in the file.
It loops through the list and creates separate files but the file only contains last two lines of the very first IP in the list.

Code: Select all

Loop, read, C:\temp\IPList.txt
{
	FileName = %A_LoopReadLine%.txt
	RunWait, %comspec% /c nslookup %A_LoopFieldReadLine% >C:\temp\%FileName%,,hide
}
Tried using StdOutToVar function that was referenced in the old AHK forum but I could get this to work.

Thanks ahead of time.
BoBo
Posts: 3873
Joined: 13 May 2014, 17:15

Re: Problems with nslookup writing to file

15 Mar 2018, 17:28

Code: Select all

Loop, Read, C:\Temp\IPList.txt
{
	FileName := A_LoopReadLine . ".txt"
	RunWait, % "Comspec /c nslookup " . A_LoopReadLine . " > C:\Temp\" . FileName. ".txt",, Hide
}
Edited but not tested.
vahju
Posts: 27
Joined: 30 Sep 2013, 16:09

Re: Problems with nslookup writing to file  Topic is solved

15 Mar 2018, 21:03

Thanks for the quick reply BoBo.
I still had some problems with your code but I combined yours with what I found in the help file and that seemed to work.
It is fast but the command window is not fully hidden.
Hope this helps others.

Code: Select all

Loop, Read, C:\temp\IPList.txt
{
	FileName := "C:\temp\" . A_LoopReadLine . ".txt"
	Command := "nslookup " . A_LoopReadLine . " >" . FileName
	RunWaitOne(Command)
}

;Function below can be found inside the AHK help documentation
RunWaitOne(command)
{
    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObjCreate("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(ComSpec " /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()
}
BoBo
Posts: 3873
Joined: 13 May 2014, 17:15

Re: Problems with nslookup writing to file

16 Mar 2018, 01:26

Found [here], slightly edited ...

Code: Select all

MsgBox % output := func("ping www.google.com")

func(cmdline) {
	DetectHiddenWindows On
	Run %ComSpec%,, Hide, pid
	WinWait ahk_pid %pid%
	DllCall("AttachConsole", "UInt", pid)
	WshShell := ComObjCreate("Wscript.Shell")
	exec := WshShell.Exec(cmdline)
	output := exec.StdOut.ReadAll()
	DllCall("FreeConsole")
	Process Close, %pid%
	Return output
	}

Return to “Ask For Help”

Who is online

Users browsing this forum: bautai, punchin, sobuj53, TAC109, Xeo786 and 36 guests