Announcement

Collapse
No announcement yet.

Read the ip address of remote desktop connection

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Read the ip address of remote desktop connection

    Is it possible to detect the ip address of a computer connected to the computer running my powerbasic windows program?

  • #2
    On same LAN? Can you put a program on both PCs? DNS server on LAN?
    Dale

    Comment


    • #3
      Yes they are both on the same LAN

      I need the computer that the remote desktop is connected to be able to know the ip of the computer that's connecting to it.

      Comment


      • #4
        Assuming your remote desktop uses default port 3389
        Code:
         
         netstat -n | find ":3389" | find "ESTABLISHED"

        Comment


        • #5
          Yes that will work. I can pipe the result to a file and check the ip address

          TCP 10.1.0.50:3389 10.14.3.112:53678 ESTABLISHED

          Any way to do this in powerbasic itself?

          Comment


          • #6
            I can pipe the result to a file and check the ip address
            ...
            Any way to do this in powerbasic itself?
            Yes. (several ways, actually).

            Hint: That 'netstat' command is a third party program which you could launch from your PB program.

            Comment


            • #7
              Originally posted by Steve Bouffe View Post
              Yes that will work. I can pipe the result to a file and check the ip address

              TCP 10.1.0.50:3389 10.14.3.112:53678 ESTABLISHED

              Any way to do this in powerbasic itself?
              What? Open a file and parse single line?

              psuedo code -
              KILL filename 'delete previous if any.ERROR 53 is okay, 70 needs fixing then KILL again
              SHELL "NETSTAT -n blah-blah"
              nFile = FREEFILE
              OPEN filename FOR blah AS nFile blah
              read/get the string
              CLOSE nFile
              parse the string for IP

              Cheers,

              P.S. REMOTE DESKTOP not running on my PC. Appears it is on yours, so go for it.
              Dale

              Comment


              • #8
                Trying to create a SHELL statement that will successfully execute the commandline from msg#4 above, having trouble with proper syntax passing DQs to CMD.exe...

                I'm asking because I want to know how to manage these levels of syntax translation. (In fact, I've succeeded in getting the info by skipping the pipes to FIND, and simply redirecting the NETSTAT -n into a tempfile, then using PB to open that file and have PB do the find...) But I still would like to learn this DQ syntax thing...


                Based on straight syntax in a CMD window vs attempt from a .BAT file, I see that DQs need to be tripled! So I've tried a bunch of things; here's the jumble of tests...

                I'm compiling under PBCC 6.04

                Oh BTW, :3389 is not on my machine, so I used one that was. But for the sake of consistency in this thread, my code below uses the same one mentioned above.

                Thanks in advance for any constructive comments or pointers to info that I can study!
                -John



                Code:
                #Compile Exe
                #Dim All
                
                Function PBMain () As Long
                   Local lExitCode As Long
                   Local m As String
                
                   'Shell "NETSTAT -n | find " & $DQ & ":3389" & $Dq & " | find " & $Dq & "ESTABLISHED" & $Dq, exit to lExitCode     'most straightforward;  won't run
                
                   'm = "||" & "NETSTAT -n | find " & $DQ & ":3389" & $Dq & " | find " & $Dq & "ESTABLISHED" & $Dq & "||"     ' use ||  as marker to confirm string is as expected
                   'm = $Dq & "NETSTAT -n | find " & $Dq & ":3389" & $Dq & " | find " & $Dq & "ESTABLISHED" & $Dq & " > tempfile.txt" & $Dq    'try redirecting to a file
                
                   'm = "NETSTAT -n | find " & $Dq & $Dq & $Dq & ":3389" & $Dq & $Dq & $Dq & " > tempfile.txt"    'try reduced command with triple DQs
                   m = "NETSTAT -n "           ' try simplest command to verify it is even being run! (yes it runs)
                
                   'm =         "NETSTAT -n | find " & $DQ & ":3389" & $Dq & "  > tempfile.txt"      '  verify redirection works
                
                   ? m
                   Shell m , Exit To lExitCode                      ' use message as above
                   Shell $Dq & m & $Dq, Exit To lExitCode  ' enclose the whole damn thing in an extra set of DQs...
                   ? "lExitCode = " & Trim$(lExitCode)
                
                   waitkey$
                
                End Function


                Another approach...

                Code:
                'try forcing another shell
                   'Shell ENVIRON$("COMSPEC") & " /C " & $DQ & m & $DQ & " > tempfile.txt", Exit To lExitCode
                   SHELL ENVIRON$("COMSPEC") & " /C " & m & " > tempfile.txt", EXIT TO lExitCode

                Comment


                • #9
                  Why not use Currports from nirsoft which can see which all remote IP addresses that had established a connection with the current computer?

                  https://www.nirsoft.net/utils/cports.html

                  Comment


                  • #10
                    TCPLogView is another good tool to log remote connections to a particular computer. It can output a log file in csv format

                    https://www.nirsoft.net/utils/tcp_log_view.html

                    Comment


                    • #11
                      I do use this function to determine if in a remote session.

                      GETSYSTEMMETRICS( %SM_REMOTESESSION )

                      I'm having trouble getting the shell to work also.

                      Comment


                      • #12
                        I found this which will do the trick.

                        https://support.microsoft.com/en-ca/...d-line-utility

                        Comment


                        • #13
                          Steve, How did you implement it, could you please provide some codes?

                          Comment


                          • #14
                            Originally posted by Anne Wilson View Post
                            Steve, How did you implement it, could you please provide some codes?
                            Mr. Bouffe's link is perfect and provides you all the information you need to "give it a try."

                            Please show the code you tried - presumably it's failing - and someone here will help.

                            (It looks pretty easy from the link).

                            MCM




                            Comment


                            • #15
                              Using WINAPI GetTcpTable
                              PBWin10, Jose Roca Includes.
                              Code:
                              #COMPILE EXE
                              #DIM ALL
                              #INCLUDE "WIN32API.INC"
                              #INCLUDE "IPHlpApi.inc"
                              
                              FUNCTION PBMAIN () AS LONG
                              LOCAL lResult, I AS LONG
                              LOCAL lngSize AS LONG
                              LOCAL pTcpTable AS MIB_TCPTABLE POINTER
                              LOCAL TcpTable AS STRING
                              LOCAL sRemoteA, sLocalA, sRemoteP, sLocalP, sState, sTemp, sBuffer AS STRING
                              
                              lngSize = 0
                              lResult = GetTcpTable(BYVAL 0&, lngSize, 0)
                              
                              IF lResult = %ERROR_NOT_SUPPORTED THEN
                                  MSGBOX "IP Helper is not supported by this system."
                                  EXIT FUNCTION
                              END IF
                              
                              IF lResult = %ERROR_INSUFFICIENT_BUFFER THEN     'Increase Buffer to lngsize
                                   TcpTable  = NUL$(lngSize)
                                   pTcpTable = STRPTR(TcpTable)
                              END IF
                              
                              lResult = GetTcpTable(BYVAL pTcpTable, lngSize, 0)
                                  FOR I = 0 TO @pTcpTable.dwNumEntries - 1
                                      sTemp =    HEX$(@pTcpTable.table(I).dwRemoteAddr,8)   ' Address is reversed, 4 bytes 1B01010A =  27.01.01.10 => IP: 10.1.1.27
                                      sRemoteA = FORMAT$(VAL("&H"+MID$(sTemp,7,2))) + "." + FORMAT$(VAL("&H"+MID$(sTemp,5,2))) + "." + FORMAT$(VAL("&H"+MID$(sTemp,3,2))) + "." +  FORMAT$(VAL("&H"+MID$(sTemp,1,2)))
                                      sRemoteP = HEX$(@pTcpTable.table(I).dwRemotePort,8)      ' Bytes are reversed  0000BB01  => 01BB => 443   (only FFFF ports max)
                                      sRemoteP = FORMAT$(VAL("&H0" + MID$(sRemoteP,7,2) + MID$(sRemoteP,5,2)))
                              
                                      sTemp =   HEX$(@pTcpTable.table(I).dwLocalAddr,8)   ' Address is reversed, 4 bytes 1B01010A =  27.01.01.10 => IP: 10.1.1.27
                                      sLocalA = FORMAT$(VAL("&H"+MID$(sTemp,7,2))) + "." + FORMAT$(VAL("&H"+MID$(sTemp,5,2))) + "." + FORMAT$(VAL("&H"+MID$(sTemp,3,2))) + "." +  FORMAT$(VAL("&H"+MID$(sTemp,1,2)))
                                      sLocalP = HEX$(@pTcpTable.table(I).dwLocalPort,8)        ' Bytes are reversed  0000BB01  => 01BB => 443   (only FFFF ports max)
                                      sLocalP = FORMAT$(VAL("&H0" + MID$(sLocalP,7,2) + MID$(sLocalP,5,2)))
                              
                                      sState = GetPortState (@pTcpTable.table(I).State)
                              
                                        'for remote Desktop change to 3389
                                      IF sRemoteP = "443" THEN  sBuffer +=  sLocalA + ":" + sLocalP + "    " + sRemoteA + ":" + sRemoteP + "   " + sState + $CRLF
                              
                                  NEXT I
                                          MSGBOX  sBuffer
                              END FUNCTION
                              
                              FUNCTION GetPortState(lPortState AS LONG) AS STRING
                              SELECT CASE lPortState
                                  CASE %MIB_TCP_STATE_CLOSED: FUNCTION = "Closed"
                                  CASE %MIB_TCP_STATE_LISTEN: FUNCTION = "Listening"
                                  CASE %MIB_TCP_STATE_SYN_SENT: FUNCTION = "SYN Sent"
                                  CASE %MIB_TCP_STATE_SYN_RCVD: FUNCTION = "SYN Received"
                                  CASE %MIB_TCP_STATE_ESTAB: FUNCTION = "Established"
                                  CASE %MIB_TCP_STATE_FIN_WAIT1: FUNCTION = "FIN Wait1"
                                  CASE %MIB_TCP_STATE_FIN_WAIT2: FUNCTION = "FIN Wait2"
                                  CASE %MIB_TCP_STATE_CLOSE_WAIT: FUNCTION = "Close Wait"
                                  CASE %MIB_TCP_STATE_CLOSING: FUNCTION = "Closing"
                                  CASE %MIB_TCP_STATE_LAST_ACK: FUNCTION = "Last Ack"
                                  CASE %MIB_TCP_STATE_TIME_WAIT: FUNCTION = "Time Wait"
                                  CASE %MIB_TCP_STATE_DELETE_TCB: FUNCTION = "Deleted"
                                  CASE ELSE: FUNCTION = "Unknown"
                              END SELECT
                              END FUNCTION

                              Comment


                              • #16
                                Click image for larger version  Name:	net.png Views:	1 Size:	18.4 KB ID:	769902

                                Code:
                                'Use José Roca's includes
                                #COMPILE EXE '#Win 9.07 (D:\Basic\Bas\Jose Roca\Forum\Jose\Windows API Headers\1.19 (PB9.x)\uz)#
                                #DIM ALL
                                #INCLUDE "Win32Api.inc"
                                #INCLUDE "CommCtrl.inc"
                                #INCLUDE "IPExport.inc"
                                #INCLUDE "LMCons.inc"
                                #INCLUDE "LMServer.inc"
                                #INCLUDE "LMErr.inc"
                                #INCLUDE "WinSock2.inc"
                                #INCLUDE "IPHlpApi.inc"
                                #INCLUDE "ListViewCtrl.inc"
                                #INCLUDE "LMAPIbuf.inc"
                                '#RESOURCE ".pbr"
                                
                                UNION IN_ADDR
                                  s_addr AS LONG
                                  s AS STRING * 4
                                END UNION
                                
                                %Listview = 101
                                
                                $AppName  = "ScanNet"
                                
                                GLOBAL hDlg AS DWORD
                                '_____________________________________________________________________________
                                
                                FUNCTION uniStringPointerToString(BYVAL uStringPointer AS DWORD)AS STRING
                                 LOCAL CharCount AS DWORD
                                 LOCAL sAnsi     AS STRING
                                
                                 CharCount = LStrlenW(BYVAL uStringPointer)
                                 sAnsi     = NUL$(CharCount)
                                 WideCharToMultiByte(%CP_ACP, %NULL, BYVAL uStringPointer, CharCount, _
                                                     BYVAL STRPTR(sAnsi), CharCount, BYVAL %NULL, BYVAL %NULL)
                                 FUNCTION = sAnsi
                                
                                END FUNCTION
                                '______________________________________________________________________________
                                
                                FUNCTION ipHostNameToDot(sHostName AS STRING) AS STRING
                                 LOCAL pHostEnt         AS HOSTENT POINTER 'PB use HostEntStru
                                 LOCAL WindowSocketData AS WSADATA
                                 LOCAL lpAddr           AS IN_ADDR
                                 LOCAL pz               AS ASCIIZ POINTER
                                 LOCAL pPointer         AS DWORD POINTER
                                
                                 IF WSAStartup(MAKLNG(2, 0), WindowSocketData) = %IP_SUCCESS  THEN
                                   pHostEnt  = GetHostByName(BYVAL STRPTR(SHostName))
                                   IF pHostEnt THEN
                                     #IF %PB_REVISION > &H0907
                                     pPointer      = @phostent.h_addr_list
                                     lpaddr.s_addr = @@pPointer
                                     pz            = Inet_ntoa(lpaddr.s)
                                     #ELSE
                                     pPointer      = @phostent.h_list
                                     lpaddr.s_addr = @@pPointer
                                     pz            = Inet_ntoa(lpaddr.s_addr)
                                     #ENDIF
                                     FUNCTION      = @pz
                                   END IF
                                   WSACleanup
                                 ELSE
                                   FUNCTION =  "0.0.0.0"
                                 END IF
                                
                                END FUNCTION
                                '______________________________________________________________________________
                                
                                FUNCTION ipIpToMac(sIp AS STRING) AS STRING
                                 LOCAL RetVal      AS LONG
                                 DIM   Mac(0 TO 5) AS BYTE
                                
                                 RetVal = SendARP(Inet_Addr(BYVAL STRPTR(sIp)), 0, BYVAL VARPTR(Mac(0)), 6)
                                 IF RetVal = %NO_ERROR THEN
                                   FUNCTION = HEX$(Mac(0), 2) & "-" & HEX$(Mac(1), 2) & "-" & HEX$(Mac(2), 2) & "-" & _
                                              HEX$(Mac(3), 2) & "-" & HEX$(Mac(4), 2) & "-" & HEX$(Mac(4), 2)
                                 ELSE
                                   FUNCTION = "00-00-00-00-00-00"
                                 END IF
                                
                                END FUNCTION
                                '______________________________________________________________________________
                                
                                FUNCTION ListViewAdd()AS LONG
                                 LOCAL pServerInfo101 AS SERVER_INFO_101 POINTER
                                 LOCAL ListViewColumn AS LVCOLUMN
                                 LOCAL ListViewItem   AS LVITEM
                                 LOCAL sBuffer        AS STRING
                                 LOCAL hListview      AS DWORD
                                 LOCAL EntriesRead    AS DWORD
                                 LOCAL TotalEntries   AS DWORD
                                 LOCAL dwResumeHandle AS DWORD
                                 LOCAL InsertionPoint AS LONG
                                 LOCAL ColCount       AS LONG
                                 LOCAL COL            AS LONG
                                 LOCAL RowCount       AS LONG
                                 LOCAL ROW            AS LONG
                                 LOCAL index          AS LONG
                                 LOCAL RetVal         AS LONG
                                
                                 hListview = GetDlgItem(hDlg, %Listview)
                                 ColCount  = 6
                                
                                 SendMessage(hListview, %LVM_DELETEALLITEMS, 0, 0)
                                 WHILE SendMessage(hListview, %LVM_DELETECOLUMN, 0, 0) : WEND
                                
                                 LISTVIEW_SETEXTENDEDLISTVIEWSTYLE(hListview, ListView_GetExtendedListViewStyle(hListview) OR _
                                 %LVS_EX_FULLROWSELECT OR %LVS_EX_GRIDLINES OR %LVS_EX_HEADERDRAGDROP)
                                
                                 'Set column headers.
                                 ListViewColumn.mask    = %LVCF_FMT OR %LVCF_TEXT OR %LVCF_SUBITEM
                                 ListViewColumn.fmt     = %LVCFMT_LEFT
                                 FOR COL = 0 TO ColCount - 1
                                   sBuffer                = PARSE$("Server name,ip,mac,Windows version,Comment,Server type", ",", COL + 1)
                                   ListViewColumn.pszText = STRPTR(sBuffer)
                                   ListViewColumn.iOrder  = COL
                                   ListViewColumn.fmt     = %LVCFMT_CENTER
                                   SendMessage(hListview, %LVM_INSERTCOLUMN, COL, VARPTR(ListviewColumn))
                                 NEXT
                                
                                 'Set data
                                 DO
                                   RetVal = NetServerEnum(BYVAL 0, _               'Must be NULL.
                                                          BYVAL 101, _             'Information level of the data.
                                                          pServerInfo101, _        'Pointer to the buffer that receives the data.
                                                          %MAX_PREFERRED_LENGTH, _ 'MAX_PREFERRED_LENGTH, &HFFFFFFFF, the function allocates the amount of memory required for the data.
                                                          EntriesRead, _           'DWORD pointer that receives the count of elements actually enumerated.
                                                          TotalEntries, _          'DWORD pointer, total number of visible servers and workstations on the network.
                                                          %SV_TYPE_ALL, _          'Or'ed DWORD filters server type.
                                                          BYVAL 0, _               'Null or Unicode domain name.
                                                          dwResumeHandle)          'Reserved, must be set to zero.
                                   IF RetVal = %NERR_SUCCESS OR RetVal = %ERROR_MORE_DATA THEN
                                     FOR index = 0 TO EntriesRead - 1
                                       'Server/Host name
                                       sBuffer               = uniStringPointerToString(@pServerInfo101.sv101_name)
                                       ListViewItem.pszText  = STRPTR(sBuffer)
                                       ListViewItem.iItem    = index
                                       ListViewItem.iSubItem = 0
                                       ListViewItem.mask     = %LVIF_TEXT
                                       InsertionPoint        = SendMessage(hListview, %LVM_INSERTITEM, %NULL, VARPTR(ListViewItem))
                                       ListViewItem.iItem    = InsertionPoint
                                
                                       'Comment
                                       sBuffer               = uniStringPointerToString(@pServerInfo101.sv101_comment)
                                       IF LEN(sBuffer)       = 0 THEN sBuffer = "[nil"
                                       ListViewItem.pszText  = STRPTR(sBuffer)
                                       ListViewItem.iSubItem = 4
                                       SendMessage(hListview, %LVM_SETITEM, 0, VARPTR(ListViewItem))
                                
                                       'Windows version
                                       sBuffer = CHOOSE$(@pServerInfo101.sv101_platform_id \ 100 - 2 , "DOS", "OS2", "NT", "VMS") & $SPC & _
                                                 FORMAT$(@pServerInfo101.sv101_version_major) & "." & _
                                                 FORMAT$(@pServerInfo101.sv101_version_minor)
                                       ListViewItem.pszText  = STRPTR(sBuffer)
                                       ListViewItem.iSubItem = 3
                                       SendMessage(hListview, %LVM_SETITEM, 0, VARPTR(ListViewItem))
                                
                                       'Type of server, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa370623(v=vs.85).aspx
                                       sBuffer = HEX$(@pServerInfo101.sv101_type, 8)
                                       ListViewItem.pszText  = STRPTR(sBuffer)
                                       ListViewItem.iSubItem = 5
                                       SendMessage(hListview, %LVM_SETITEM, 0, VARPTR(ListViewItem))
                                
                                       'Dotted ip address from host
                                       sBuffer = ipHostNameToDot(uniStringPointerToString(@pServerInfo101.sv101_name))
                                       ListViewItem.pszText  = STRPTR(sBuffer)
                                       ListViewItem.iSubItem = 1
                                       SendMessage(hListview, %LVM_SETITEM, 0, VARPTR(ListViewItem))
                                
                                       'Mac address
                                       sBuffer = ipIpToMac(ipHostNameToDot(uniStringPointerToString(@pServerInfo101.sv101_name)))
                                       ListViewItem.pszText  = STRPTR(sBuffer)
                                       ListViewItem.iSubItem = 2
                                       SendMessage(hListview, %LVM_SETITEM, 0, VARPTR(ListViewItem))
                                
                                       pServerInfo101 = pServerInfo101 + SIZEOF(SERVER_INFO_101)
                                     NEXT
                                     NetApiBufferFree(pServerInfo101) 'Free mem, even if ERROR_MORE_DATA
                                   END IF
                                   IF RetVal <> %ERROR_MORE_DATA THEN EXIT DO
                                 LOOP
                                
                                 'Auto size columns.
                                 FOR COL = 0 TO ColCount - 1
                                   SendMessage(hListview, %LVM_SETCOLUMNWIDTH, COL, MAKLNG(%LVSCW_AUTOSIZE_USEHEADER, 0)) 'Auto size columns
                                 NEXT
                                
                                END FUNCTION
                                '______________________________________________________________________________
                                
                                CALLBACK FUNCTION ShowDIALOG1Proc()
                                 LOCAL  nmLvPtr   AS NM_LISTVIEW POINTER
                                 STATIC hListview AS DWORD
                                
                                 SELECT CASE CBMSG
                                
                                   CASE %WM_INITDIALOG
                                     hListview = GetDlgItem(hDlg, %Listview)
                                     ListViewAdd
                                
                                   CASE %WM_COMMAND
                                     SELECT CASE CBCTL
                                       CASE %Listview
                                     END SELECT
                                
                                   CASE %WM_NOTIFY
                                     IF LOWRD(CBWPARAM) = %Listview THEN
                                       nmLvPtr = CBLPARAM
                                       SELECT CASE @nmLvPtr.hdr.code
                                         CASE %LVN_COLUMNCLICK, %NM_RCLICK, %NM_DBLCLK
                                           ListViewAdd
                                           SetWindowText(hDlg, $AppName & " - Refreshed at " & TIME$)
                                       END SELECT
                                     END IF
                                
                                   CASE %WM_SIZE
                                     SetWindowPos(hListview, %NULL, 2, 2, LOWRD(CBLPARAM) - 4, HIWRD(CBLPARAM) - 4, %SWP_NOZORDER)
                                
                                 END SELECT
                                
                                END FUNCTION
                                '______________________________________________________________________________
                                
                                FUNCTION PBMAIN()
                                 LOCAL hIcon AS DWORD
                                
                                 hIcon = ExtractIcon(GetModuleHandle(""), "%SystemRoot%\system32\powrprof.dll", 1)
                                 DIALOG FONT "Segoe UI", 9
                                 DIALOG NEW %HWND_DESKTOP, $AppName, , , 320, 100, _
                                 %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_THICKFRAME OR %WS_SYSMENU, 0 TO hDlg
                                
                                 CONTROL ADD "SysListView32", hDlg, %Listview, "SysListView321", 5, 5, 210, 115, _
                                 %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %WS_TABSTOP OR %LVS_REPORT OR _
                                 %LVS_SORTASCENDING OR %LVS_SHOWSELALWAYS, %WS_EX_LEFT OR %WS_EX_RIGHTSCROLLBAR
                                
                                 SetClassLong(hDlg, %GCL_HICON, hIcon)
                                
                                 DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc
                                
                                 DestroyIcon(hIcon)
                                
                                END FUNCTION
                                '______________________________________________________________________________
                                '
                                Last edited by Pierre Bellisle; 16 Feb 2018, 08:43 PM.

                                Comment


                                • #17
                                  Thanks for the code examples.

                                  Pierre post compiles but nothing is displayed in the listview (I did download Jose's includes)

                                  The example given by Rod works for me. I had to disable ipv6 on the network as connection via ipv6 are not shown. Be good if it could.

                                  Comment


                                  • #18
                                    Be good if it could.
                                    This forum more than any other makes me wonder why the majority of the members became programmers when they seem dead set against actually doing any programming themselves.

                                    Comment


                                    • #19
                                      Problem solvers look for solutions, not necessarily want to create them!

                                      -John

                                      Comment


                                      • #20
                                        WinAPI GetExtendedTcpTable can be used to get both TCP IPV4 and IPV6.

                                        Comment

                                        Working...
                                        X