Page 1 of 1

How to get Network IP over LAN

Posted: Mon Aug 04, 2003 9:57 pm
by Motu23
I try to find out the IPs of all computers that are connected to my PC over LAN. Anybody got an idea how to manage this ?

thx, Martin :-)

Posted: Mon Aug 04, 2003 10:50 pm
by Flype
an example of what you are expecting exists in CodeArchive. This is in german ( so, take your dictionnary close to you :wink: ) and make use of the MS API structure BROWSEINFOS. here is the entire code :

Code: Select all

; German forum: http://robsite.de/php/pureboard/viewtopic.php?t=669&highlight=
; Author: Manne
; Date: 19. April 2003

; Beispiel wie man Clients im LAN remote herunterfahren oder neustarten kann. 
; Optional kann auch eine Nachricht gesendet werden. 
; Logischerweise muß der Initiator auf dem Client Adminrechte haben

#ERROR_SUCCESS = 0 
#MAX_PATH = 260 
#CSIDL_NETWORK = $12 
#BIF_BROWSEFORCOMPUTER = $1000 

Structure BROWSEINFOS 
   hwndOwner.l 
   pidlRoot.l 
   pszDisplayName.s 
   lpszTitle.s 
   ulFlags.l 
   lpfn.l 
   lParam.l 
   iImage.l 
EndStructure 

Procedure.l ShutDownComputer(CompName.s, MessageToUser.s, SecondsUntilShutdown.l, ForceAppsClosed.l, RebootAfter.l) 
  ShutDown = InitiateSystemShutdown_(CompName, MessageToUser, SecondsUntilShutdown, ForceAppsClosed, RebootAfter) 
  ProcedureReturn ShutDown 
EndProcedure 

Procedure.l AbortShutdown(CompName.s) 
     AbortShutDown = AbortSystemShutdown_(CompName) 
     ProcedureReturn AbortShutDown 
EndProcedure 


; PureBasic Visual Designer v3.62 


;- Window Constants 
; 
#Window_0 = 0 

;- Gadget Constants 
; 
#Gadget_0 = 0 
#Gadget_1 = 1 
#Gadget_2 = 2 
#Gadget_3 = 3 
#Gadget_4 = 4 
#Gadget_5 = 5 
#Gadget_6 = 6 
#Gadget_7 = 7 
#Gadget_8 = 8 
#Gadget_9 = 9 
#Gadget_10 = 10 
#Gadget_11 = 11 
#Gadget_12 = 12 

Procedure.s GetBrowseNetworkWorkstation() 
    BI.BROWSEINFOS 
    pidl.l 
    sPath.s 
    pos.l 
    hwnd = WindowID(#Window_0) 
    
    If SHGetSpecialFolderLocation_(hwnd, #CSIDL_NETWORK, @pidl) = #ERROR_SUCCESS 
        BI\hwndOwner = hwnd 
        BI\pidlRoot = pidl 
        BI\pszDisplayName = Space(256) 
        BI\lpszTitle = "Select a network computer." 
        BI\ulFlags = #BIF_BROWSEFORCOMPUTER 
        
        If SHBrowseForFolder_(BI) <> 0 
            Name.s = "\\"+ BI\pszDisplayName 
            ProcedureReturn Name 
        EndIf 
        CoTaskMemFree_(@pidl) 
    EndIf 
EndProcedure 


Procedure Open_Window_0() 
  If OpenWindow(#Window_0, 216, 0, 302, 365,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "ShutGUI") 
    If CreateGadgetList(WindowID()) 
      TextGadget(#Gadget_0, 20, 20, 80, 20, "ComputerName:") 
      StringGadget(#Gadget_1, 110, 20, 120, 20, "") 
      ButtonGadget(#Gadget_2, 250, 20, 30, 20, "...") 
      Frame3DGadget(#Gadget_3, 30, 60, 250, 80, "Optionen") 
      CheckBoxGadget(#Gadget_4, 60, 80, 190, 20, "Anwendungen forciert beenden") 
      CheckBoxGadget(#Gadget_5, 60, 110, 190, 20, "Neustart nach Herunterfahren") 
      TextGadget(#Gadget_6, 30, 160, 220, 20, "zu sendende Nachricht (127 Zeichen max)") 
      StringGadget(#Gadget_7, 30, 190, 250, 80, "", #PB_String_MultiLine|#ES_AUTOVSCROLL) 
      TextGadget(#Gadget_8, 60, 280, 80, 20, "Delay (Sek.):") 
      StringGadget(#Gadget_9, 150, 277, 30, 20, "30") 
      ButtonGadget(#Gadget_10, 20, 310, 80, 30, "Herunterfahren") 
      ButtonGadget(#Gadget_11, 120, 310, 80, 30, "Abbrechen") 
      ButtonGadget(#Gadget_12, 210, 310, 80, 30, "Beenden") 
      
    EndIf 
  EndIf 
EndProcedure 


Open_Window_0() 

SetGadgetState(#Gadget_5, 1) 

Repeat 
  Event = WaitWindowEvent() 
  If Event = #PB_EventCloseWindow 
    quit = 1 
  ElseIf Event = #PB_Event_Gadget 
    Select EventGadgetID() 
      Case 2 
        SetGadgetText(#Gadget_1, GetBrowseNetworkWorkstation()) 
      Case 10 
        Name.s = GetGadgetText(#Gadget_1) 
        message.s = GetGadgetText(#Gadget_7) 
        pause.s = GetGadgetText(#Gadget_9) 
        chkKillApps.l = GetGadgetState(#Gadget_4) 
        chkReboot.l = GetGadgetState(#Gadget_5) 
        If Name = "" 
          MessageRequester("Client wählen", "Es wurde kein Client ausgewählt.  ", #MB_ICONEXCLAMATION) 
        Else 
          goShutdown = ShutDownComputer(Name, message, Val(pause), chkKillApps, chkReboot) 
        If goShutdown = 1 
          MessageRequester("Kommando erfolgreich", "Herunterfahren erfolgreich eingeleitet für  " + Name + ".  " + Chr(13) + "Der Client wird heruntergefahren in " + pause + " Sekunden.  ", #MB_ICONINFORMATION) 
        ElseIf goShutdown = 0 
            MessageRequester("Fehler", "Herunterfahren nicht möglich für  " + Name + ".  Mögliche Ursachen:  " + Chr(13) + Chr(13) + "1. ClientName falsch geschrieben." + Chr(13) + "2. Der Computer ist kein Window NT-Client." + Chr(13) + "3. Sie haben nicht die erforderlichen Rechte." + Chr(13) + "4. Der Client kann im Netz nicht gefunden werden.", #MB_ICONEXCLAMATION) 
        EndIf 
    EndIf 
      Case 11 
        Name.s = GetGadgetText(#Gadget_1) 
        If Name = "" 
          MessageRequester("Client wählen", "Es wurde kein Client ausgewählt", #MB_ICONSTOP) 
        Else 
          cancelShutdown = AbortShutdown(Name) 
          If cancelShutdown = 1 
            MessageRequester("Abbruch erfolgreich", "Herunterfahren erfolgreich abgebrochen für  " + Name + ".", #MB_ICONINFORMATION) 
          ElseIf cancelShutdown = 0 
            MessageRequester("Abbruch unmöglich", "Abbruch nicht möglich für  " + Name + ".  Mögliche Ursachen:  " + Chr(13) + Chr(13) + "1. ClientName falsch geschrieben." + Chr(13) + "2. Der Computer ist kein Window NT-Client." + Chr(13) + "3. Sie haben nicht die erforderlichen Rechte." + Chr(13) + "4. Der Client kann im Netz nicht gefunden werden." + Chr(13) + "5. Zu spät.", #MB_ICONEXCLAMATION) 
          EndIf 
       EndIf 
      Case 12 
        quit = 1 
    EndSelect 
  EndIf 
Until quit 
End

Posted: Mon Aug 04, 2003 11:16 pm
by Flype

Thanks for the fast reply, but...

Posted: Mon Aug 04, 2003 11:43 pm
by Motu23
the problem is that you must select the other computer by a dialog window. I need all the IP in the LAN without any user request. (just a list or something)...

It can be done with a batch:
Net View > NetTemp.txt
Exit

and then you can get the data from the NetTemp.txt, but i don't want to use old Win95 Batch Programms to write IPs in a Text file. I need something like Net View as an API (or what ever) command.

Any idea about that ?

Re: Thanks for the fast reply, but...

Posted: Tue Aug 05, 2003 9:09 am
by Rings
Motu23 wrote:but i don't want to use old Win95 Batch Programms to write IPs in a Text file.
Have you ever (you'r german) read the actually CT ?
Batch-programming is not restricted to W89, and also available under XP.
Motu23 wrote: I need something like Net View as an API (or what ever) command.
Any idea about that ?
Use the Capture-Pipes snippet i wrote in the Tips&tricks or study the WinAPI.
Another solution is to ping every IP and Port
also there are snippets for that .

Posted: Tue Aug 05, 2003 9:12 am
by AngelSoul
Here's a simple example:

------------------------------
InitNetwork()
ExamineIPAddresses()

Repeat
rr=NextIPAddress()
If rr:a$=IPString(rr):EndIf
If Mid(a$,1,7)="192.168":LANIP$=a$:EndIf
Until rr=0

Debug LANIP$
------------------------------

Typically LAN addresses starts with 192.168

Posted: Tue Aug 05, 2003 6:29 pm
by Karbon
Typically LAN addresses starts with 192.168
That's a *really* big assumption. There are several "private" ip address classes..

10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255

... and some people may use public IPs for their workstations so that opens up every other IP as a possiblility :-)

What you might think about is getting the IP of the computer running the program, use the first two or three octets from that to do the scanning. It's far from perfect but better than hard coding addresses or partial addresses in there I think.

Good luck!

Posted: Wed Aug 06, 2003 11:15 am
by AngelSoul
I agree it's a BIG assumption however if no user intervention is a must, an assumption has to be considered.