Playing Around with IP

Started by spl, September 29, 2024, 12:41:34 PM

Previous topic - Next topic

spl

First; I know there are several ways to extract IP addresses and location info. I noticed when looking up my IV4 addresses from Ipconfig I either received 'address in private range' or an ISP in Toronto, Canada [which when resolved to IV6 gave correct info.... Found this 2-liner in PS both resolved and gave correct location info. Placed in my StdOut function to work in WB. Probably easy to build in straight WB. Be curious for anyone trying it out to report any failures/anomalies
;Winbatch 2024B Display IP Location
;save as ..\StdOut_IPLocation.wbt
;Stan Littlefield 9/29/2024
;==========================================================
Gosub udfs
cmd="Powershell"
msg='IP Location'
args = $"$IPAddress = (Invoke-WebRequest -uri "http:://ifconfig.me/ip").Content
$LocationInfo = Invoke-RestMethod -Method Get -Uri "http:://ip-api.com/json/$IPAddress"
$LocationInfo
$"
BoxOpen("Running...",cmd:" ":args:@LF:"PLEASE WAIT...MAY TAKE SOME TIME")
TimeDelay(2)
vals = Get_stdout()
Message(msg,vals)
Exit
;==========================================================
:udfs
#DefineSubroutine Get_stdout()
ObjectClrOption("useany","System")
objInfo = ObjectClrNew("System.Diagnostics.ProcessStartInfo")
Output=""
timeOut = ObjectType("I2",5000)
objInfo.FileName = cmd
objInfo.RedirectStandardError = ObjectType("BOOL",@TRUE)
objInfo.RedirectStandardOutput = ObjectType("BOOL",@TRUE)
objInfo.UseShellExecute = ObjectType("BOOL",@FALSE)
objInfo.CreateNoWindow = ObjectType("BOOL",@TRUE)
objInfo.Arguments = args
oProcess = ObjectClrNew("System.Diagnostics.Process")
oProcess.StartInfo = objInfo
Boxtext("Starting Process")
TimeDelay(2)
BoxShut()
oProcess.Start()
oProcess.WaitForExit(timeout)
STDOUT = oProcess.StandardOutput.ReadToEnd()
STDERR = oProcess.StandardError.ReadToEnd()
Output = Output:STDOUT:@CRLF
If STDERR<>""
   Output = Output:"STDERR:":STDERR:@CRLF
Endif
oProcess = 0
objInfo = 0

Return (Output)
#EndSubroutine
Return
;==========================================================
Stan - formerly stanl [ex-Pundit]

JTaylor

Seems to work.   Even ran it through a VPN.

Jim