Author Topic: Read Multiple IP using Function but getting subscript error...  (Read 3185 times)

0 Members and 1 Guest are viewing this topic.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Read Multiple IP using Function but getting subscript error...
« on: October 17, 2021, 01:58:18 am »
So I am working n a TCPIP tool to make identifying issues faster and this program works but has errors on lines 15 and 16 so I am trying to figure out how to fix that.
Code: QB64: [Select]
  1. Print GetLocalIP$
  2.  
  3. Function GetLocalIP$
  4.  
  5.     'Shell _Hide "cmd /c ipconfig > IPconfig.tmp"
  6.     Open "IPconfig.tmp" For Input As #3
  7.     Do
  8.         If Not EOF(3) Then Line Input #3, ipline$ Else GoTo 888
  9.         If Left$(LTrim$(ipline$), 2) = "IP" Then
  10.             'Close #3
  11.             GetLocalIP$ = Mid$(ipline$, InStr(ipline$, ":") + 1)
  12.             If Left$(ipline$, 3) = "169" Then ipline$ = "Invalid IP" + ipline$
  13.             Print Mid$(ipline$, InStr(ipline$, ":") + 1): IPP = 1 + IPP
  14.             Found$(IPP) = Mid$(ipline$, InStr(ipline$, ":") + 1)
  15.             Print Found$(IPP)
  16.             'Exit Do
  17.         End If
  18.     Loop Until EOF(3)
  19.    888
  20.     Close #3
  21.     'Kill "IPconfig.tmp" 'kill the messenger?
  22.  

The file that is loaded and searched is this IPconfig.tmp

Quote
Windows IP Configuration


Unknown adapter VPN - VPN Client:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter TAP-Windows Adapter V9:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : autumn.com

Wireless LAN adapter Local Area Connection* 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 4:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Ethernet 2 Dock:

   Connection-specific DNS Suffix  . : owentechnologysolutions.com
   Link-local IPv6 Address . . . . . : fe80::b509:444b:7e06:265a%24
   IPv4 Address. . . . . . . . . . . : 10.1.1.105
   Subnet Mask . . . . . . . . . . . : 255.0.0.0
   IPv4 Address. . . . . . . . . . . : 10.11.1.105
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.0.105
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.51.105
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 169.254.198.161
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :

Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Bluetooth Network Connection 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

So it runs and finds each IP but gives a subscript error for each instance...

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #1 on: October 17, 2021, 02:17:10 am »
REDIM Found$(1000)
Print GetLocalIP$

If you start your code with the REDIM statement, it will allow, as written, 1000 instances (elements) for the array. Without DIM or REDIM, QB64 considers the array undefined as for the number of elements to keep in memory, and therefore throws a subscript out of range error.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #2 on: October 17, 2021, 02:32:28 am »
I forgot that command...
But I get a error line one which is that line...

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #3 on: October 17, 2021, 02:35:38 am »
'$DYNAMIC
Still have one error to look at.
Spoke to soon still have the out or subscription error...

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #4 on: October 17, 2021, 09:09:32 am »
Found() is an array, as Pete said, if you want more than 10 items in it you have to use DIM Found(MaxNumberItems) in Function or Shared with main program you can Dim or REDIM (for Dynamic array that can be resized).

If you don't Dim it bigger you will get subscript error when try to go beyond 10.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #5 on: October 17, 2021, 09:16:55 am »
Oh I see:
Quote
IPP = 1 + IPP
            Found$(IPP) = Mid$(ipline$, InStr(ipline$, ":") + 1)


start of function add:
Code: QB64: [Select]
  1. REDIM Found$(0)  ' <<<<<<<<<<<<<<<<<<< makes Found$ a Dynamic array
  2.  

after IPP = IPP + 1:
Code: QB64: [Select]
  1. IPP = 1 + IPP
  2. REDIM _PRESERVE Found$(IPP) ' <<<<<<<<<<<<<<<<<<<<< add this line
  3. Found$(IPP) = Mid$(ipline$, InStr(ipline$, ":") + 1)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #6 on: October 17, 2021, 09:33:33 am »
You can also pass Found$() back to main code for use later BUT you will have to ... here's the whole thing.

Code: QB64: [Select]
  1. ReDim IPs$(0) ' <<<<
  2. Print GetLocalIP$(IPs$())
  3. For i = 0 To UBound(IPs$) 'OK it does look like it starts at item #1
  4.     Print i, IPs$(i)
  5.  
  6.  
  7. Function GetLocalIP$ (Found$())
  8.     ReDim Found$(0)
  9.     Shell _Hide "cmd /c ipconfig > IPconfig.tmp" ' << still need this uncommented for current conditions
  10.     Open "IPconfig.tmp" For Input As #3
  11.     Do
  12.         If Not EOF(3) Then Line Input #3, ipline$ Else GoTo 888
  13.         If Left$(LTrim$(ipline$), 2) = "IP" Then
  14.             'Close #3
  15.             GetLocalIP$ = Mid$(ipline$, InStr(ipline$, ":") + 1)
  16.             If Left$(ipline$, 3) = "169" Then ipline$ = "Invalid IP" + ipline$
  17.             Print Mid$(ipline$, InStr(ipline$, ":") + 1) ' for debug see what's going on inside Function
  18.             IPP = 1 + IPP
  19.             ReDim _Preserve Found$(IPP)
  20.             Found$(IPP) = Mid$(ipline$, InStr(ipline$, ":") + 1)
  21.             'Print IPP, Found$(IPP) ' <<< index + item  ' for debug see what's going on inside Function
  22.             'Exit Do
  23.         End If
  24.     Loop Until EOF(3)
  25.    888
  26.     Close #3
  27.     Kill "IPconfig.tmp" 'kill the messenger?  still need this for not cluttering folder
  28.  

I think there may be more string manipulation ahead?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #7 on: October 17, 2021, 10:02:55 am »
Yeah you will probably not want to put invalid IP's in the Found$().

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #8 on: October 17, 2021, 08:45:03 pm »
Aww Let me try these things, then.
I will post results.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #9 on: October 17, 2021, 09:40:24 pm »
So I made the changes and a few tweaks

Code: QB64: [Select]
  1. '$DYNAMIC
  2. ReDim IPs$(0) ' <<<<
  3. ReDim Found$(0)
  4. Print GetLocalIP$(IPs$()); ""
  5. For i = 1 To UBound(IPs$) 'OK it does look like it starts at item #1
  6.        Stack$ = Stack$ + " " + IPs$(i)
  7. Print Stack$
  8.  
  9.  
  10. Function GetLocalIP$ (Found$())
  11.  
  12.     'Shell _Hide "cmd /c ipconfig > IPconfig.tmp" ' << still need this commented for current conditions
  13.     Open "IPconfig.tmp" For Input As #3
  14.     Do
  15.         If Not EOF(3) Then Line Input #3, ipline$ Else GoTo 888
  16.         If Left$(LTrim$(ipline$), 2) = "IP" Then
  17.             'Close #3
  18.             GetLocalIP$ = Mid$(ipline$, InStr(ipline$, ":") + 1)
  19.             If Left$(ipline$, 3) = "169" Then ipline$ = "Invalid IP" + ipline$
  20.             IPP = 1 + IPP
  21.             ReDim _Preserve Found$(IPP)
  22.             Found$(IPP) = Mid$(ipline$, InStr(ipline$, ":") + 1)
  23.             'Exit Do
  24.         End If
  25.     Loop Until EOF(3)
  26.    888
  27.     Close #3
  28.     'Kill "IPconfig.tmp" 'kill the messenger? still need this for not cluttering folder
  29.  
When it is run I get the print out of the

Print GetLocalIP$(IPs$()); ""

But I get the great idea that I would have eventually tried to figure out, I probably would have made a file and then loaded it again in the main program, this is a much better way.

For i = 1 To UBound(IPs$) 'OK it does look like it starts at item #1
    Print i, IPs$(i)
Next


So now I am trying to figure out how to "CALL" the function with out printing the extra line.

This is a part of another program and the invalid IP is part of the devices that we may need to factory reset, its IP sets to a 169 by default, so the idea is to set the Stack to each IP we use.

I am thinking of pulling the network card from the file to create the batch file also...
Thanks for the kick in the right direction bplus

Edit:
Quick read only print function works...
« Last Edit: October 17, 2021, 10:14:21 pm by Chris80194 »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Read Multiple IP using Function but getting subscript error...
« Reply #10 on: October 17, 2021, 10:27:21 pm »
Well how about we use a sub, instead of a function then?

First post converted to sub.

Code: QB64: [Select]
  1. REDIM IPs$(0) ' <<<<
  2. CALL LocalIP(IPs$())
  3. FOR i = 0 TO UBOUND(IPs$) 'OK it does look like it starts at item #1
  4.     PRINT i, IPs$(i)
  5.  
  6. SUB LocalIP (IPs$())
  7.     SHELL _HIDE "ipconfig>IPconfig.tmp" ' << still need this uncommented for current conditions
  8.     OPEN "IPconfig.tmp" FOR INPUT AS #3
  9.     DO UNTIL EOF(3)
  10.         LINE INPUT #3, ipline$
  11.         IF LEFT$(LTRIM$(ipline$), 2) = "IP" THEN
  12.             'Close #3
  13.             GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 1)
  14.             IF LEFT$(ipline$, 3) = "169" THEN ipline$ = "Invalid IP" + ipline$
  15.             PRINT MID$(ipline$, INSTR(ipline$, ":") + 1) ' for debug see what's going on inside Function
  16.             IPP = 1 + IPP
  17.             REDIM _PRESERVE IPs$(IPP)
  18.             IPs$(IPP) = MID$(ipline$, INSTR(ipline$, ":") + 1)
  19.             'Print IPP, IPs$(IPP) ' <<< index + item  ' for debug see what's going on inside Function
  20.             'Exit Do
  21.         END IF
  22.     LOOP
  23.     CLOSE #3
  24.     ' KILL "IPconfig.tmp" 'kill the messenger?  still need this for not cluttering folder

Second post converted to sub.

Code: QB64: [Select]
  1. REDIM IPs$(0)
  2.  
  3. CALL LocalIP(IPs$())
  4.  
  5. FOR i = 1 TO UBOUND(IPs$) 'OK it does look like it starts at item #1
  6.     Stack$ = Stack$ + " " + IPs$(i)
  7. PRINT Stack$
  8.  
  9. SUB LocalIP (IPs$())
  10.  
  11.     SHELL _HIDE "ipconfig>IPconfig.tmp" ' << still need this commented for current conditions
  12.     OPEN "IPconfig.tmp" FOR INPUT AS #3
  13.     DO UNTIL EOF(3)
  14.         LINE INPUT #3, ipline$
  15.         IF LEFT$(LTRIM$(ipline$), 2) = "IP" THEN
  16.             'Close #3
  17.             GetLocalIP$ = MID$(ipline$, INSTR(ipline$, ":") + 1)
  18.             IF LEFT$(ipline$, 3) = "169" THEN ipline$ = "Invalid IP" + ipline$
  19.             IPP = 1 + IPP
  20.             REDIM _PRESERVE IPs$(IPP)
  21.             IPs$(IPP) = MID$(ipline$, INSTR(ipline$, ":") + 1)
  22.             'Exit Do
  23.         END IF
  24.     LOOP
  25.  
  26.     CLOSE #3
  27.     ' KILL "IPconfig.tmp" 'kill the messenger? still need this for not cluttering folder

Pete
« Last Edit: October 17, 2021, 10:40:11 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/