AutoHotkey Community

It is currently May 27th, 2012, 10:05 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: June 1st, 2006, 7:25 pm 
Offline

Joined: September 20th, 2005, 9:23 pm
Posts: 16
This is a simple script I wrote up because I think I saw someone mention they would like a way to convert an IP Address into a Hostname. I also threw in a Hostname to IP Address just because. :D

Code:
#NoEnv

TempDir = %A_Temp%
TrcR = 20  ; German language = 23
If A_OSVersion = WIN_2000  ; Win 95/98/2000 do not have tracert switches
   TrcV =
Else If A_OSVersion = WIN_98
   TrcV =
Else If A_OSVersion = WIN_95
   TrcV =
Else
   TrcV = -h 1

GuiMenu:
Gui, 2:Destroy
Array1 =
Array2 =
Array3 =
Array4 =
IPAdd = 0
Gui, +ToolWindow
Gui, Add, Text,, Enter the IP/Web Address or Hostname below:
Gui, Add, Edit, w150 h21 vIPHost -VScroll -Wrap, %IPHost%
Gui, Add, Button, default, Resolve
Gui, Add, Edit,, %IP1%
Gui, Add, Edit,, %Host1%
Gui, Add, Edit,, %Host2%
Gui, Show,, Resolve IP or Host
Return

ButtonResolve:
Gui, Submit
Gui, 2:+ToolWindow -SysMenu
Gui, 2:Add, Progress, vProgressV w300 h20 cBlue
Gui, Destroy
Gui, 2:Show,, Progress
IP1 =
Host1 =
Host2 =
IPHost2 = %IPHost%
StringSplit, Array, IPHost, .
If Array1 is digit
   IPAdd += 1
If Array2 is digit
   IPAdd += 1
If Array3 is digit
   IPAdd += 1
If Array4 is digit
   IPAdd += 1
   
GuiControl, 2:, ProgressV, +10

If IPAdd = 4
{
   RunWait, cmd /C tracert %IPHost2% %TrcV% >"%TempDir%\IPHost.txt",,Hide
   GuiControl, 2:, ProgressV, +30
   FileRead, FRead, %TempDir%\IPHost.txt
   GuiControl, 2:, ProgressV, +10
   StringTrimLeft, FRead, FRead, %TrcR%
   GuiControl, 2:, ProgressV, +10
   StringGetPos, FReadL, FRead, %A_Space%[%IPHost2%]
   GuiControl, 2:, ProgressV, +10
   StringMid, FRead, FRead, %FReadL%,, L
   GuiControl, 2:, ProgressV, +10
   Host1 = %FRead%
   IP1 = %IPHost2%
   GuiControl, 2:, ProgressV, +20
   GoSub, GuiMenu
   Return
}
else If IPAdd <> 4
{
   RunWait, cmd /C ping %IPHost2% -n 1 >"%TempDir%\IPHost.txt",,Hide
   GuiControl, 2:, ProgressV, +10
   FileReadLine, FRead, %TempDir%\IPHost.txt, 2
   GuiControl, 2:, ProgressV, +10
   StringGetPos, FReadL, FRead, [
   StringGetPos, FReadR, FRead, ]
   GuiControl, 2:, ProgressV, +10
   FReadR := FReadR - FReadL - 1
   FReadL := FReadL + 2
   StringMid, FRead, FRead, %FReadL%, %FReadR%
   GuiControl, 2:, ProgressV, +10
   IPHost2 = %FRead%
   IP1 = %FRead%
}
GuiControl, 2:, ProgressV, 50
RunWait, cmd /C tracert %IPHost2% %TrcV% >"%TempDir%\IPHost.txt",,Hide
GuiControl, 2:, ProgressV, +10
FileRead, FRead, %TempDir%\IPHost.txt
StringTrimLeft, FRead, FRead, %TrcR%
StringGetPos, FReadL, FRead, %A_Space%[%IPHost2%]
StringMid, FRead, FRead, %FReadL%,, L
GuiControl, 2:, ProgressV, +10
Host1 = %FRead%
RunWait, cmd /C tracert %IPHost% %TrcV% >"%TempDir%\IPHost.txt",,Hide
GuiControl, 2:, ProgressV, +10
FileRead, FRead, %TempDir%\IPHost.txt
StringTrimLeft, FRead, FRead, %TrcR%
StringGetPos, FReadL, FRead, %A_Space%[%IPHost2%]
StringMid, FRead, FRead, %FReadL%,, L
GuiControl, 2:, ProgressV, +10
Host2 = %FRead%

GuiControl, 2:, ProgressV, +10
GoSub, GuiMenu
Return

GuiClose:
ExitApp


This should also allow you to resolve websites to their hostname when using the IP/Web to Host option. The script should be fairly easy to customize and place in your own scripts if anyone feels the need.

There shouldn't be many things that won't resolve. I can see where it would not resolve a website that started with something that looks similar to an IP address like 123.18.28.29.hotdog.com, but this should be pretty rare.

Let me know if anyone finds this useful! :)

Edit: Updated code with OS checking, Progress bars, choosing correct mode of operation automatically, and a variable for the tracert switches for other languages.

_________________
-Lufia


Last edited by Lufia on June 2nd, 2006, 12:59 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2006, 7:54 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Nice! Thanks, Lufia, for sharing it.

Do you plan to enhance it? If yes, it would be nice to have a progress bar, or something to ensure us that the script is working, not died, while we wait for the result. Also, it could automatically choose the right mode of operation: if an all numeric value is given with 3 points it can be assumed an IP address, and convert it to host name.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2006, 8:36 pm 
Offline

Joined: September 20th, 2005, 9:23 pm
Posts: 16
That's a good idea, I didn't think about doing that! I'll add those things and post back! Thanks for the advice. :)

_________________
-Lufia


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2006, 5:00 am 
Thanks for the script.
Win2000:
you have to replace
Code:
RunWait, cmd /C tracert %IPHost% -h 1>"%TempDir%\IPHost.txt",,Hide
by
Code:
RunWait, cmd /C tracert %IPHost% >"%TempDir%\IPHost.txt",,Hide
I have installed the German Language.
The corresponding line for tracert begins with Routenverfolgung zu
Therefore in this case you have to replace
Code:
StringTrimLeft, FRead, FRead, 20
by
Code:
StringTrimLeft, FRead, FRead, 23


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2006, 1:04 pm 
Offline

Joined: September 20th, 2005, 9:23 pm
Posts: 16
I updated the code with more changes. It also now will display three results. This is mostly for websites that may sometimes not resolve into a hostname. Like www.google.com will give you something like www.l.google.com instead of resolving with a hostname, so I included the option to show that in the third box. If you use www.autohotkey.com you should have a different result in each box. :)

I placed an Edit: in the original post with the changes.
Let me know if you like them!

_________________
-Lufia


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 2nd, 2006, 2:19 pm 
Many thanks, Win2000, German Language (adjustment in the second line by the user) works fine!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2007, 6:39 pm 
This is kind of a dead topic bump.. but thanks!


Report this post
Top
  
Reply with quote  
 Post subject: Resolve Domain to IP
PostPosted: April 10th, 2009, 11:10 pm 
Offline

Joined: May 15th, 2007, 8:59 pm
Posts: 169
Thx for your script.
I required a function to resolve a domain to an IP without nessicarily checking if it was active.

Here is my small modification

Resolve Domain to IP Address
Code:
NoEnv

TempDir = %A_Temp%
TrcR = 20  ; German language = 23
If A_OSVersion = WIN_2000  ; Win 95/98/2000 do not have tracert switches
   TrcV =
Else If A_OSVersion = WIN_98
   TrcV =
Else If A_OSVersion = WIN_95
   TrcV =
Else
   TrcV = -h 1
GuiMenu:
Array1 =
Array2 =
Array3 =
Array4 =
IPAdd = 0
Gui, Add, Text,x10 y10, Enter the Hostname below:
Gui, Add, Edit, w150 x10 y30 vIPHost -VScroll -Wrap, %IPHost%
Gui, Add, Button, default x170 y30, Resolve
Gui, Add, Edit,vtip x230 y30 w200, %IP1%
Gui, Show,, Resolve Host
Return

ButtonResolve:
guicontrol,,tip,
Gui, Submit, nohide
IP1 =
StringReplace, IPHost, IPHost, http://
StringReplace, IPHost, IPHost, /
RunWait, cmd /C ping %IPHost% -n 1 -w 0 >"%TempDir%\IPHost.txt",,Hide
FileReadLine, FRead, %TempDir%\IPHost.txt, 2
StringGetPos, FReadL, FRead, [
StringGetPos, FReadR, FRead, ]
   
FReadR := FReadR - FReadL - 1
FReadL := FReadL + 2
StringMid, FRead, FRead, %FReadL%, %FReadR%
   
IP1 = %FRead%
   
guicontrol,,tip,%IP1%
Return

GuiClose:
ExitApp


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group