Guest User

Hostname to IP

a guest
Nov 21st, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 2.94 KB | Source Code | 0 0
  1. ; Function to get highlighted text from the clipboard
  2. GetHighlightedText() {
  3.     ClipSaved := ClipboardAll  ; Save the current clipboard content
  4.     Clipboard := ""            ; Clear the clipboard
  5.     Send, ^c                   ; Copy the selected text
  6.     ClipWait, 1                ; Wait for the clipboard to contain text
  7.  
  8.     text := Clipboard           ; Store the copied text
  9.  
  10.     ; Check if text is empty
  11.     if (text = "") {
  12.         MsgBox, No text selected! Please copy again.
  13.         Clipboard := ClipSaved   ; Restore the original clipboard
  14.         return                   ; Exit the function
  15.     }
  16.  
  17.     text := Trim(text)          ; Trim whitespace from the text
  18.     Clipboard := ClipSaved      ; Restore the original clipboard
  19.     return text                 ; Return the highlighted text
  20. }
  21.  
  22. ; Function to resolve hostname to IP address
  23. ResolveHostname(hostname) {
  24.     hWS2_32 := DllCall("LoadLibrary", "str", "ws2_32.dll", "ptr")
  25.     VarSetCapacity(WSADATA, 394 + (A_PtrSize - 2) + A_PtrSize, 0)
  26.    
  27.     if (DllCall("ws2_32\WSAStartup", "ushort", 0x0202, "ptr", &WSADATA) != 0) {
  28.         DllCall("ws2_32\WSACleanup")
  29.         return "Failure in WSAStartup"
  30.     }
  31.    
  32.     VarSetCapacity(hints, 16 + 4 * A_PtrSize, 0)
  33.     NumPut(2, hints, 4, "int")  ; AF_INET
  34.     NumPut(1, hints, 8, "int")  ; SOCK_STREAM
  35.     NumPut(6, hints, 12, "int") ; IPPROTO_TCP
  36.    
  37.     result := 0
  38.     if (DllCall("ws2_32\getaddrinfo", "astr", hostname, "ptr", 0, "ptr", &hints, "ptr*", result)) {
  39.         DllCall("ws2_32\WSACleanup")
  40.         return "getaddrinfo: " DllCall("ws2_32\WSAGetLastError")
  41.     }
  42.    
  43.     addr := result
  44.     IPList := []
  45.    
  46.     while (addr) {
  47.         ipaddr := DllCall("ws2_32\inet_ntoa", "uint", NumGet(NumGet(addr + 0, 16 + 2 * A_PtrSize) + 4, 0, "uint"), "astr")
  48.         IPList.Push(ipaddr)
  49.         addr := NumGet(addr + 0, 16 + 3 * A_PtrSize)
  50.     }
  51.    
  52.     DllCall("ws2_32\WSACleanup")
  53.     DllCall("FreeLibrary", "ptr", hWS2_32)
  54.    
  55.     return IPList
  56. }
  57.  
  58. ; Function to show IP addresses in a GUI
  59. ShowIPAddresses(IPAddresses) {
  60.     Gui, New, +AlwaysOnTop +ToolWindow +Resize
  61.     Gui, Add, Text, vIPText, Resolved IP Addresses:`n
  62.     for index, ip in IPAddresses {
  63.         GuiControl,, IPText, % ip "`n"  ; Append each IP address to the GUI text
  64.     }
  65.     Gui, Show, AutoSize, IP Addresses
  66.     ; Position the GUI near the mouse cursor
  67.     MouseGetPos, mouseX, mouseY
  68.     Gui, Show, x%mouseX% y%mouseY%  ; Show GUI near mouse position
  69. }
  70.  
  71. ; Hotkey to trigger the action when F8 is pressed
  72. F8::
  73.     highlightedText := GetHighlightedText()  ; Get the highlighted text
  74.     if (highlightedText) {
  75.         IPAddresses := ResolveHostname(highlightedText)  ; Resolve the hostname to IP addresses
  76.         if (IsObject(IPAddresses)) {
  77.             ShowIPAddresses(IPAddresses)  ; Show IP addresses in a GUI
  78.         } else {
  79.             MsgBox % "Error: " IPAddresses  ; Display error message if any
  80.         }
  81.     }
  82. return
  83.  
Advertisement
Add Comment
Please, Sign In to add comment