Mendo wrote: "there are multiple network cards on my computer, including some virtual ones: when I request the IP address, the first one is always returned to me, which is not that of the physical card connected to the network. How is it possible to have the complete list of network cards, their addresses and determine the one connected to the internet?"
First, to poll local network devices and retrieve their (local) addresses on Windows:
SetErrorMode(2)
// set window properties
SetWindowTitle( "Network Devices" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 )
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )
dim DeviceName$[20]
dim DeviceIP$[20]
TotalDevices=0
DevicesFile$="media/devicestemp.txt" ` name and location of temporary file to write network data to, change as desired
s=RunApp("cmd","/C ipconfig /all > "+DevicesFile$) ` this creates the temp file and writes network device data to it
` s=RunApp("cmd","/C ipconfig /all & pause") ` to view the results and wait for key
Sync():Sleep(200):Sync() ` seems to help in waiting for ipconfig to finish
rem now that the file has been created, we need to retrieve the network device names and IP addresses
OpenToRead(1,"devicestemp.txt")
repeat
s$=ReadLine(1)
rem check for device names
DeviceNameLabel$=" Description . . . . . . . . . . . : "
if left(s$,len(DeviceNameLabel$))=DeviceNameLabel$
rem increment device counter here
TotalDevices=TotalDevices+1
DeviceName$[TotalDevices]=right(s$,len(s$)-len(DeviceNameLabel$))
endif
rem check for IP addresses
DeviceIPLabel$=" IPv4 Address. . . . . . . . . . . : "
if left(s$,len(DeviceIPLabel$))=DeviceIPLabel$
DeviceIP$[TotalDevices]=right(s$,len(s$)-len(DeviceIPLabel$))
rem clear out any redundant text characters after the IP address so it remains just the IP
ss$=""
for i = 1 to len(DeviceIP$[TotalDevices])
e$=mid(DeviceIP$[TotalDevices],i,1)
if asc(e$)=>48 and asc(e$)<=57 then ss$=ss$+e$
if asc(e$)=46 then ss$=ss$+e$
next i
DeviceIP$[TotalDevices]=ss$
endif
until FileEOF(1)>0
CloseFile(1)
Sync()
do
print("PID for ipconfig: "+str(s))
for i = 1 to TotalDevices
print(DeviceName$[i])
print(DeviceIP$[i])
next i
Sync()
if GetRawKeyState(27)>0 then end
loop
The 'Name' array will hold the names of each device and the 'IP' array will hold their addresses. From there, you can connect to any of the available local addresses to bind a network connection to a desired device.