How to check the Ports Status on a remote server using VB Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mdfidahussain
    New Member
    • Oct 2007
    • 40

    How to check the Ports Status on a remote server using VB Script

    Hello Friends,

    Can any body help me out in "how to check the Ports Status (opened & enabled) on a remote server using VB Script

    Any sort of help is highly appreciable.... .

    Thnx

    -FH
  • AHMEDYO
    New Member
    • Nov 2007
    • 112

    #2
    hi

    i think this code work well but it have loop to check for state that i cant accept but im not good in vbs and in same time i think we cant do other thing because we cant receve events in vbs

    ' VBScript source code

    Function CheckPort(Byval Server,Byval Port)
    Dim SockObject
    set SockObject=Crea teObject("MSWin sock.Winsock.1" )
    SockObject.Prot ocol=0 ' TCP
    Call SockObject.Clos e
    Call SockObject.Conn ect (Server,Port)
    while ((SockObject.St ate=6) or (SockObject.Sta te=3)) 'socket state <> connecting or connection pending
    'do nothing
    wend
    if(SockObject.S tate=7) then ' if socket connected
    msgbox "Port OPen"
    elseif(SockObje ct.State=9)then ' If Error
    msgbox "error"
    elseif(SockObje ct.State=0)then 'Closed
    msgbox "connection refused"
    end if
    call SockObject.Clos e
    set SockObject=noth ing
    End Function
    Call CheckPort("Serv er",445)

    and plz take in ur mind Winsock COM Class is only installed with vb6 component and u will not find it in PC that not have vb6 components

    Best Regards

    Comment

    • mdfidahussain
      New Member
      • Oct 2007
      • 40

      #3
      Thanks Ahmed,

      The logic is exactly what Iwant but I think this will not completely solve my problem, I need to install VB6 on all my target or remote servers on which I need to run this code (and this I cannot do), anyways I am getting this errror

      Microsoft VBScript runtime error: ActiveX component can't create object: 'MSWinsock.Wins ock.1'

      is there any other way to get this info

      Thnx a million

      FH

      Comment

      • mdfidahussain
        New Member
        • Oct 2007
        • 40

        #4
        Ahmed,

        More over, I have this code, but this is limited to check the ports' status on local machine, cannot be executed on a remote server

        ############### # CODE ############### ###

        Set objFirewall = CreateObject("H NetCfg.FwMgr")
        Set objPolicy = objFirewall.Loc alPolicy.Curren tProfile

        Set colPorts = objPolicy.Globa llyOpenPorts

        For Each objPort in colPorts
        Wscript.Echo "Port name: " & objPort.Name
        Wscript.Echo "Port number: " & objPort.Port
        Wscript.Echo "Port protocol: " & objPort.Protoco l
        Wscript.Echo "Port enabled: " & objPort.Enabled
        Next

        Comment

        • AHMEDYO
          New Member
          • Nov 2007
          • 112

          #5
          Originally posted by mdfidahussain
          Ahmed,

          More over, I have this code, but this is limited to check the ports' status on local machine, cannot be executed on a remote server

          ############### # CODE ############### ###

          Set objFirewall = CreateObject("H NetCfg.FwMgr")
          Set objPolicy = objFirewall.Loc alPolicy.Curren tProfile

          Set colPorts = objPolicy.Globa llyOpenPorts

          For Each objPort in colPorts
          Wscript.Echo "Port name: " & objPort.Name
          Wscript.Echo "Port number: " & objPort.Port
          Wscript.Echo "Port protocol: " & objPort.Protoco l
          Wscript.Echo "Port enabled: " & objPort.Enabled
          Next
          yas man u wanna enumerate Open Ports but i was give u port scanner , sorry :D , because u dont wanna external component and u wanna to get ports from remote server , i think and not sure may be using WMI winmgmts Class , but it have heavy number of classes and i must take a look, and i will response

          Comment

          • AHMEDYO
            New Member
            • Nov 2007
            • 112

            #6
            Originally posted by mdfidahussain
            Thanks Ahmed,

            The logic is exactly what Iwant but I think this will not completely solve my problem, I need to install VB6 on all my target or remote servers on which I need to run this code (and this I cannot do), anyways I am getting this errror

            Microsoft VBScript runtime error: ActiveX component can't create object: 'MSWinsock.Wins ock.1'

            is there any other way to get this info

            Thnx a million

            FH
            :D as i said u dont have MSWinsockLib in ur machine , u must install it

            Comment

            • mdfidahussain
              New Member
              • Oct 2007
              • 40

              #7
              I installed it, but still getting the same error, any help using Win32_ classes or any other you know from which I can get the ports info using VBScript

              Comment

              • AHMEDYO
                New Member
                • Nov 2007
                • 112

                #8
                Originally posted by mdfidahussain
                I installed it, but still getting the same error, any help using Win32_ classes or any other you know from which I can get the ports info using VBScript
                which file you installed?, and how u install it????
                the file name is MSWINSCK.OCX
                after you copy it you must registry it

                by the way, try to change the socket number or remove it in the current line

                Code:
                set SockObject=CreateObject("MSWinsock.Winsock.[B]1[/B]")
                or use "Regedit" search the registry about ".Winsock" to see what version you install

                and i will search about Winmgmts Win32_Classes and i will reply

                GOOD LUCK

                Comment

                • mdfidahussain
                  New Member
                  • Oct 2007
                  • 40

                  #9
                  Thnks Ahmed I have searched almost all of the Win32_ classes, couldn't find any helpful, s stated earlier the below is only gathering the ports info on the local pc, not sure where to give the remote server name as input in it

                  Any idea how to run this code on a remote server?

                  ############### # CODE ############### ###

                  Set objFirewall = CreateObject("H NetCfg.FwMgr")
                  Set objPolicy = objFirewall.Loc alPolicy.Curren tProfile

                  Set colPorts = objPolicy.Globa llyOpenPorts

                  For Each objPort in colPorts
                  Wscript.Echo "Port name: " & objPort.Name
                  Wscript.Echo "Port number: " & objPort.Port
                  Wscript.Echo "Port protocol: " & objPort.Protoco l
                  Wscript.Echo "Port enabled: " & objPort.Enabled
                  Next
                  ############### ############### ############

                  I am just searching for an option that could be available for "LocalPolic y" in the "Set objPolicy = objFirewall.Loc alPolicy.Curren tProfile", tried alot but of no luck........... .....any sort of help will be highly appreciable.... .

                  Regards

                  Comment

                  • mdfidahussain
                    New Member
                    • Oct 2007
                    • 40

                    #10
                    Thanks a million for ur patience......A waiting for ur valuable reply...:)

                    Comment

                    • AHMEDYO
                      New Member
                      • Nov 2007
                      • 112

                      #11
                      Originally posted by mdfidahussain
                      Thanks a million for ur patience......A waiting for ur valuable reply...:)
                      Many Thanx man, i am trying, searching and thinking, i think i will got it
                      :D

                      Regards...

                      Comment

                      • mdfidahussain
                        New Member
                        • Oct 2007
                        • 40

                        #12
                        Any luck Ahmed.... ???

                        thnx

                        Comment

                        • mdfidahussain
                          New Member
                          • Oct 2007
                          • 40

                          #13
                          Awaiting for ur valuable reply Ahmed........

                          thnx a million

                          Comment

                          • AHMEDYO
                            New Member
                            • Nov 2007
                            • 112

                            #14
                            Hey..

                            i wanna ask a question because i think there is no way to check about open ports except scanning as i was post to you the first script

                            the question is , this script will run within domain??, have active directory??, if so, i think we have two ways to do that.

                            first distribute MSWinsock Library in your server then we can use it AS DCOM from any PC within domain

                            second,we can copy MSWinsock.ocx to \\Domain.Com\Sy svol shared (and almost all users have permission to access this folder to access policy) folder and then improve the first script to check if Winsock found in current machine or not , if not it will copy the MSWinsock .OCX file to system32 folder and registry it and then we can use Winsock Object

                            Waiting your reply

                            Best Regards.

                            Comment

                            • mdfidahussain
                              New Member
                              • Oct 2007
                              • 40

                              #15
                              We dont have an AD, is there any way from which we can get ports info from server's registry entry??

                              thnx

                              Comment

                              Working...