Jump to content

Port Checker, checking IP and port


cdtoews
 Share

Recommended Posts

## Updated 6-19-08 ##

I made a small program to monitor an IP on a certain port.

I have to monitor IP's, but do not have ICMP available. So I have to monitor if I can get there on a certain port.

I tried to use the built-in tcpconnect, but found odd results. I finally went with scripting against Microsoft's portqry.exe (never re-invent the wheel)

It's downloadable from:

http://www.microsoft.com/downloads/details...;displaylang=en

Here is my code:

#cs
v2.0 got the status gui working,
v2.1 going to try to get an exit button working
v2.3 fixing tooltraytip
v2.4 ip in window name
v2.5 stripping white space
2.7 adding ID text, and reconfigure
v2.8 trying to change infrastructure so exit and setup work immediately
        removed exit button, as this mode of gui doesn't need it
#ce

Opt("GUIOnEventMode", 1)
#include <Constants.au3>
#include <array.au3>
#Include <Date.au3>
#include <GUIConstants.au3>

FileInstall("PortQry.exe",@ScriptDir & "\PortQry.exe")
dim $badtext =  '"' &  " ,`~!@#$%^&*()+=[]\{}|<>?/'"
dim $debug = 0
 dim $status_array[30]
 dim $ipaddress, $port , $looptime , $id_text , $log, $status_gui , $id , $exit_box , $reconfig_box , $status_label
 dim $was_up , $is_up , $test , $column_gui , $ipbox , $portbox , $secbox , $textbox , $go_button , $input_verified
 dim $monitor , $running , $gui_title 
 dim $gui_location[2]
$gui_location[0] = @DesktopWidth / 2 - 125
$gui_location[1] = @DesktopHeight / 2 - 155
;variable for the initial gui
dim $small_spacer = -5
dim $big_spacer = 10
dim $width = 150
dim $status_rows = 20

;run config gui, and setup status gui 
_setup()


;##########################################################################
;#                                                                        #
;#                        Main Program Loop                               #
;#                                                                        #
;##########################################################################
Do
    sleep($looptime * 1000)
    if $monitor = 1 Then
        $running = 1
        $result = ""
        $test = _port_query($ipaddress,$port)
        if $monitor = 1 Then
            if $test = 0 then
                $result = _now() & " , Port Listening"
                $is_up = 1
            ElseIf $test = 1 then
                $result = _now() & " , *** Not Listening ***"
                $is_up = 0
            ElseIf $test = 2 Then
                $result = _now() & " , *** Port Filtered ***"
                $is_up = 0
            Else
                $result = _now() & " , *** Problem ***"
                $is_up = 0
            EndIf
        
        
            _update_status($result)
            _writetolog($log,$result & @crlf)
            if $is_up = 1 Then
                if $was_up = 0 Then
                    _blink()
                EndIf
                $was_up = 1
                GUISetBkColor(0x00FF00)
            Else
                if $was_up = 1 Then
                    _blink()
                EndIf
                $was_up = 0
                GUISetBkColor(0xFF0000)
            EndIf
        EndIf
    EndIf
        
until 1=2
        
        

;##########################################################################
;# ###################################################################### #
;# # ################################################################## # #
;# # #                                                                # # #
;# # #                          Functions                             # # #
;# # #                                                                # # #
;# # ################################################################## # #
;# ###################################################################### #

func _monitor_loop()
    do
    if GUICtrlRead($exit_box)=$GUI_CHECKED then exit
    if GUICtrlRead($reconfig_box) = $GUI_CHECKED Then
        ;delete the gui, then run setup, 
        GUIDelete()
        _setup($ipaddress, $port , $looptime , $id_text)
    EndIf
    
    
    ;red FF0000
    ;green 00FF00
    sleep($looptime * 1000)
until 1=2

EndFunc;##########################################################################


func _blink()
    local $cycles = 7
    local $blink_delay = 100
    for $blink_loop = 1 to $cycles
        GUISetBkColor(0x00FF00)
        Sleep($blink_delay)
        GUISetBkColor(0xFF0000)
        Sleep($blink_delay)
    Next
    
EndFunc


func _setup($setup_host = "localhost", $setup_port = 502 , $setup_sec = 1 , $setup_id = "")
    
    if $running = 1 Then
        $gui_location = WinGetPos($gui_title)
        $setup_host = $ipaddress
        $setup_port = $port
        $setup_sec = $looptime
        $setup_id = $id_text
    EndIf
    
    $monitor = 0 ;stop monitoring
    $gui_title = "Port-Checker Setup"
    $column_gui = GUICreate($gui_title,210,230 , $gui_location[0] + 20 , $gui_location[1] + 40)

    GUICtrlCreateLabel("Hostname to Check",30,20,$width)
    $ipbox = GUICtrlCreateInput ( $setup_host, 30, 35, $width )

    GUICtrlCreateLabel("Port to Monitor",30,65,$width)
    $portbox = GUICtrlCreateInput ($setup_port, 30, 80, $width)

    GUICtrlCreateLabel("How often",30,105,$width)
    $secbox = GUICtrlCreateInput ( $setup_sec, 30, 120, $width )

    GUICtrlCreateLabel("Text Identifier",30,145,$width)
    $textbox = GUICtrlCreateInput($setup_id,30,160,$width)

    $go_button = GUICtrlCreateButton("GO!",80 ,190,45)
    
    
    
    GUICtrlSetOnEvent($go_button, "gobutton")
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
    GuiSetState()
    



EndFunc

func gobutton()
    ;stuff
    $errors = 0
    ;verify IP address
    $ipaddress = StringStripWS(GUICtrlRead($ipbox),8)
    $ipcheck = _checkip($ipaddress)
    if $ipcheck = 1 then 
        $bad_msg &= 'not a valid hostname' & @CRLF
        $errors += 1
    EndIf


    $port = StringStripWS(GUICtrlRead($portbox),8)
        $portcheck = _port_checker($port)
        if $portcheck = 0 Then
            $bad_msg &= 'not a valid port' & @CRLF
            $errors += 1
        EndIf
        
    $looptime = StringStripWS(GUICtrlRead($secbox),8)
        if $looptime < 1 or $looptime > 6000 then
            $bad_msg &= "Improper time" & @crlf 
            $errors += 1
        endif
    
    $id_text = GUICtrlRead($textbox)
        
    if $errors > 0 Then
        MsgBox(0,'oops',$errors & " ERRORS" & @CRLF & $bad_msg)
        $bad_msg = ""
    Else
        $gui_location = WinGetPos($gui_title)
        GUIDelete($column_gui)
        $input_verified = 1
            TraySetToolTip("monitoring " & $ipaddress & ":" & $port)
    dim $log = $ipaddress & " - " & $port &".txt"
    
    ;###########  create the status gui  ###################
    GUIDelete($status_gui)
    $gui_title = $ipaddress & " : " & $port
    $status_gui = GUICreate($gui_title , 250 , 310 , $gui_location[0] -20 , $gui_location[1] - 40 ) 
    $id = GUICtrlCreateLabel($id_text,3,3,190,40)

    ;dim $exit_text = GUICtrlCreateLabel("Exit",230,3,40)
    ;$exit_box = GUICtrlCreateButton("Exit",200,3,50,20);removed because in this gui mode, X works

    ;dim $reconfig_text = GUICtrlCreateLabel("setup",170,3,30)
    $reconfig_box = GUICtrlCreateButton("Setup",190,13,50,20)

     $status_label = GUICtrlCreateLabel("",10,45,280,280)
    GUICtrlSetOnEvent($exit_box, "CLOSEClicked")
    GUICtrlSetOnEvent($reconfig_box, "_setup")
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

     GUISetState(@SW_SHOW,$status_gui)
     sleep(300)
    _update_status($ipaddress & " : " & $port)
    $was_up = 3

    $monitor = 1 ;start monitoring again
    EndIf
            
            
EndFunc



Func CLOSEClicked()
    Exit
EndFunc


func _update_status($new_text)
    local $temp_stat = ""
    for $z = 1 to $status_rows - 1 ;move each value up the list
        $status_array[$z] = $status_array[$z + 1]
        $temp_stat &= $status_array[$z] & @crlf
    Next
    $status_array[$status_rows] = $new_text ;add latest status update to the bottom of the list
    $temp_stat &= $status_array[$status_rows]
    GUICtrlSetData ($status_label, $temp_stat) ;write the list to the gui
    ConsoleWrite($new_text & @crlf)
EndFunc 


Func _port_query($ipaddress,$port)
    local $ret
    $ret = RunWait( "portqry.exe -n " & $ipaddress & " -e " & $port & " -q",@TempDir,@SW_HIDE)
    return $ret
        
EndFunc  

Func _writetolog($logfile,$logdata)
    
    $templog = fileopen($logfile, 1)
    FileWrite($templog, $logdata)
    FileClose($templog)
    
EndFunc


Func _checkip($ip2check)
    local $isbadhost
    ;only checking if hostname is good, not checking for connectivity
    for $cleanloop = 1 to stringlen($badtext)
        $temp = StringInStr($ip2check,stringMid($badtext,$cleanloop,1))
        if $temp > 0 Then $isbadhost=1
    Next

    if $isbadhost = 1 Then
        Return(1)
    EndIf
    
EndFunc


Func _port_checker($_port)
    if $_port > 0 and $_port < 65536 then 
        return 1
    else 
        return 0
    EndIf
    
EndFunc

func close_script()
    Exit
EndFunc
Edited by cdtoews
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...