Shop OBEX P1 Docs P2 Docs Learn Events
Programmers: I need some help with a Windows Tool — Parallax Forums

Programmers: I need some help with a Windows Tool

Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
edited 2011-07-08 07:01 in General Discussion
Guys,

There is a LOT of programming talent here, and I need your help.

I need a Windows tool that does the following...

Checks your LAN and returns the following information (In a window)

Recommended IP address: 192.168.1.X
(Please use the follow criteria to determine the recommended address:
* Check the workstation's address.
If it is in the 10's make X a number around 100
If it is in the 100's make X a number around 200
Check to see if the address is already alive, move forward if it is.)
The idea here is to find an open number, away from other IPs on the lan.

Report the Subnet Mask (255.255.255.0 -- yada yada)
Report the Gateway
Report the Outside IP address (The one that is live to the Internet)

The window should have four pieces of information:

Recommended IP
Subnet Mask
Current Gateway
Outside "live" IP Address

I can't promise any riches for this, but I will put your name in "lights" in the credits of material I'm creating for the Spinneret.

Thanks
OBC
«1

Comments

  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 16:56
    Don't think you'll have much luck in this approach. I assume you are using a router based on the fact that you mention 192.168.1.xxx which is a local IP address. A Windows API is not going to be able to see all IP addresses in the Local Area Network.
    Dynamic Host Configuration Protocol ( DHCP) inthe router handles assigning IP addresses, unless they are manually
    assigned or the router allows DHCP Reservation.

    I use DHCP Reservation to make sure that IP addresses remain constant without manually assigining them (see image).

    The only method I can envision that might work is a kludge - basically displaying HTML IP status pages from your particular router in a browser object and then parsing the web page using the Document Object Model (DOM) to extract the addresses. This method would be specific to your router (and firmware version).

    Extracting data in this fashion is sometimes called "screen scraping" or "web scraping" http://en.wikipedia.org/wiki/Web_scraping

    IPaddrs.jpg
    482 x 265 - 42K
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 17:12
    I don't need to see them all, just the one that is used on the workstation the program is running on.

    Should be able to make a determination from that address which are being used.. If it's in the 10's then the safety region is around 100. If it's in the 100's then the safety region should be around 200. A simple PING should be able to confirm the address is likely unused.

    OBC
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 17:26
    Should be able to make a determination from that address which are being used.. If it's in the 10's then the safety region is around 100. If it's in the 100's then the safety region should be around 200. A simple PING should be able to confirm the address is likely unused.

    IP addresses don't have to be assigned sequentially so there is no way "to determine" what ranges are in use (or reserved). A PING is not a reliable way since many security programs block PING requests to prevent hacker attacks.

    - Ron

    PS There are other reserved Private IP addresses (as specified by the Internet Assigned Numbers Authority (IANA)) you might be able to use besides 192.168.0.0 – 192.168.255.255.
    They are:
    10.0.0.0 – 10.255.255.255
    172.16.0.0 – 172.31.255.255
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 17:36
    @Ron,

    Home networking routers typically hand out addresses starting either in the 10's or 100's. I know there is no exact way to determine an address, but we can take some reasonable "guesses" for home users based on the workstation's IP address. Anyone using the device on a larger network should contact their network administrator to make sure they don't cause a problem.

    In my experience home routers generally don't hand out more than a few addresses given most of us don't have more than a handful of devices.

    OBC
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 18:10
    Here are two VBScripts, one uses Windows Management Instrumentation (WMI) to display your local IP address e.g. 192.168.1.100

    The other displays your WAN IP Address by using an HTTPRequest to browse to http://checkip.dyndns.org/ to get your WAN (Wide Area Network) address which is what I think you are calling the "Outside IP address".

    Copy this code into notepad and save them as WANIP.vbs and GetIP.vbs and double click them to run.

    This may get you started...

    WANIP.vbs
    option explicit
    Const strURL = "[URL]http://checkip.dyndns.org/[/URL]"
    Dim resp, i, cnt, objArgs, duration, btn
    Dim WSHShell, objHTTP
    duration = 10
    Set WSHShell = wscript.CreateObject("WScript.Shell")
    Set objHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
    objHTTP.open "GET", strURL, False
    'in case of error once, retry every second...
     On Error Resume Next
     cnt = 1
     objHTTP.send
     If err.Number <> 0 Then
       Do While err.Number <> 0
         objHTTP.send
         WScript.Sleep 1000
         cnt = cnt + 1
         msgbox  cnt
         if cnt >3 then 
           WSHShell.PopUP "Could not connect to " & strURL, 10, "WAN IP Address",  0    '  + 64
           wscript.quit
         end if
       Loop
     End If
     resp = objHTTP.responseText 
     
     i = instr(resp, "IP Address: ")
     if i > 0 then
      resp = mid(resp, i+12)
    end if
     i = instr(resp, "</body>")
     if i > 0 then
      resp = mid(resp, 1, i-1)
    end if
    'resp = resp & vbcrlf & vbcrlf & strURL
    'msgbox objHTTP.readyState & vbcrlf & objHTTP.statusText & vbcrlf & objHTTP.status
    btn = WSHShell.PopUP(resp, duration, "WAN IP Address", 1)    '  + 64
    set WSHShell = nothing
    

    GetIP.vbs
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
     
    Set IPConfigSet = objWMIService.ExecQuery _ 
        ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE") 
     
    For Each IPConfig in IPConfigSet 
        If Not IsNull(IPConfig.IPAddress) Then  
            For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress) 
                WScript.Echo IPConfig.IPAddress(i) 
            Next 
        End If 
    Next 
    
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 18:15
    @Ron,

    Ah ha! Yes.. This is going the direction we need. Getting an error in GetIP.vbs though..

    OBC
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 18:17
    What is the error? What version of Windows are you running?
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 18:20
    Windows XP. Here's the error..

    GetIP.vbs

    Windows Script Host

    Script: C:\GetIP.vbs
    Line: 8
    Char: 1
    Error: 0x80041013
    Code: 80041013
    Source: (null)

    OBC
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 18:40
    Looks like you have no network adapters where "TCP/IP is bound and enabled"

    Try running "IPCONFIG /ALL>C:\Jeff.txt" from a DOS prompt. This will create C:\Jeff.txt with the IPConfig output. Open Jeff.txt with notepad and post the results...
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 18:57
    Windows IP Configuration
    
    
    
            Host Name . . . . . . . . . . . . : family-4003ac1f
    
            Primary Dns Suffix  . . . . . . . : 
    
            Node Type . . . . . . . . . . . . : Unknown
    
            IP Routing Enabled. . . . . . . . : No
    
            WINS Proxy Enabled. . . . . . . . : No
    
    
    
    Ethernet adapter Local Area Connection 3:
    
    
    
            Connection-specific DNS Suffix  . : 
    
            Description . . . . . . . . . . . : Intel(R) PRO/100 VE Network Connection
    
            Physical Address. . . . . . . . . : 00-13-D4-47-D8-F1
    
            Dhcp Enabled. . . . . . . . . . . : Yes
    
            Autoconfiguration Enabled . . . . : Yes
    
            IP Address. . . . . . . . . . . . : 192.168.2.8
    
            Subnet Mask . . . . . . . . . . . : 255.255.255.0
    
            Default Gateway . . . . . . . . . : 192.168.2.1
    
            DHCP Server . . . . . . . . . . . : 192.168.2.1
    
            DNS Servers . . . . . . . . . . . : 192.168.2.1
    
            Lease Obtained. . . . . . . . . . : Tuesday, July 05, 2011 3:49:13 PM
    
            Lease Expires . . . . . . . . . . : Friday, July 08, 2011 3:49:13 PM
    
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2011-07-05 18:58
    Jeff, I'm assuming that you're working on some form of GUI for the Spinneret, and I'm guessing it's in a .Net language. If this is the case, you can do what you want via the .Net framework. This is a better solution than WSH, since WSH may be disabled for security reasons.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 19:15
    Jeff,
    Try running this version - the WHERE clause is commented out.

    Does it display 192.168.2.8?
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
     
    Set IPConfigSet = objWMIService.ExecQuery _ 
        ("Select * from Win32_NetworkAdapterConfiguration")    ' Where IPEnabled=TRUE")   
    For Each IPConfig in IPConfigSet 
      If Not IsNull(IPConfig.IPAddress) Then  
        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress) 
    '     WScript.Echo IPConfig.IPAddress(i)
          msgbox IpConfig.Caption & vbcrlf & IPConfig.IPAddress(i), vbinformation, IPConfig.DNSHostName
        Next 
      End If 
    Next 
     
    
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 19:22
    No change..
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 19:30
    Not sure what to tell you!?! I tried it my desktop (hardwired Ethernet) and my NetBook (with a disconnected Ethernet and wireless-N WIFI adapter) and it works fine.

    Since your IPConfig output looks simple and straightforward, I am stumped.
  • Mike GMike G Posts: 2,702
    edited 2011-07-05 19:45
    See if this works LocalNetworkInfo.zip

    Run it from the command line.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 20:21
    @MikeG

    We're getting closer..

    OBC
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 20:32
    Here is another vbscript which basically grabs the values from the IPCONFIG command:
      option explicit
      dim WSHShell, sExec, x, strLineIn
      dim msgtxt
      Set WSHShell = CreateObject("WScript.Shell")
      set sExec = WSHShell.Exec("ipconfig /all")
      Do While Not sExec.StdOut.AtEndOfStream
        strLineIn = sExec.StdOut.ReadLine()
        if instr(strLineIn, "IP Address") > 0 then
          msgtxt = msgtxt & "IP Addr: " & ParseIt(strLineIn) & vbcrlf    
        end if
        if instr(strLineIn, "Subnet Mask") > 0 then
          msgtxt = msgtxt & "Subnet mask: " & ParseIt(strLineIn) & vbcrlf    
        end if
        if instr(strLineIn, "Description") > 0 then
          msgtxt = msgtxt & "Adapter: " & ParseIt(strLineIn) & vbcrlf    
        end if
      Loop
      msgbox msgtxt, vbinformation, "IP Address"
     
    public function ParseIt(strIN)
      dim i
      i = instr(strIN, ":")
      if i > 0 then
         Parseit = trim(mid(strIN, i+1))
      else
         ParseIt = strIN
      end if
    end function
    
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 20:36
    @Ron,

    That's the ticket!

    Now we just need to make an "educated" guess as to an available IP outside of the typical address ranges..

    OBC
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-05 21:02
    This parses the IPCONFIG and when it finds the first adapter with an IPV4 address (ignores IPV6),
    it replaces the last node with 254 for a recommended address

    e.g. 192.168.1.100 --> 192.168.1.254

    This is rather a crude way to do this. A compiled program doing API calls would be better - especially since the WMI approach did not work for you...

    Also you might have multiple adapters.
      option explicit
      dim WSHShell, sExec, x, strLineIn, IPaddr, subnet, adapter, iparray, foundsw, recommend
      dim msgtxt
      Set WSHShell = CreateObject("WScript.Shell")
      set sExec = WSHShell.Exec("ipconfig /all")
      foundsw = false
      Do While Not sExec.StdOut.AtEndOfStream
        strLineIn = sExec.StdOut.ReadLine()
        if instr(strLineIn, "Description") > 0 then
          Adapter = ParseIt(strLineIn) 
        end if
        if instr(strLineIn, "IP Address") > 0 then
          ipaddr = ParseIt(strLineIn) 
          iparray = split(ipaddr, ".", -1, 1)
          if ubound(iparray) = 3 then
            foundsw = true
            recommend = iparray(0) & "." & iparray(1) & "." & iparray(2) & "." & "254"
          end if       
        end if
        if instr(strLineIn, "Subnet Mask") > 0 then
          subnet = ParseIt(strLineIn) 
          if foundsw then
            exit do 
          end if
        end if
      Loop
      msgtxt = "Adapter: " & adapter & vbcrlf & "IP Address: " & IPaddr & vbcrlf & _
                   "Subnet mask: " &  subnet & vbcrlf & "Recommended: " & recommend
      msgbox msgtxt, vbinformation, "IP Address"
     
    public function ParseIt(strIN)
      dim i
      i = instr(strIN, ":")
      if i > 0 then
         Parseit = trim(mid(strIN, i+1))
      else
         ParseIt = strIN
      end if
    end function
    
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-05 21:40
    @Ron,

    Not bad.. Can you slip the Gateway and Outside IP into this single script?

    OBC
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-06 06:21
    Jeff,
    Here you go!

    The bill is on the way - I don't take PAYPAL :smile:

    - Ron
      option explicit
      Const strURL = "[URL]http://checkip.dyndns.org/[/URL]"
      dim WSHShell, sExec, x, strLineIn, IPaddr, subnet, adapter, gateway, iparray, foundsw, recommend
      dim msgtxt, temp
      dim objHTTP, cnt, resp, WAN, i
      Set WSHShell = CreateObject("WScript.Shell")
    ' get WAN IP address using HTTPRequest 
      Set objHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
      objHTTP.open "GET", strURL, False
    'in case of error once, retry every second...
      On Error Resume Next
      cnt = 1
      objHTTP.send
      If err.Number <> 0 Then
        Do While err.Number <> 0
          objHTTP.send
          WScript.Sleep 1000
          cnt = cnt + 1
          msgbox  cnt
          if cnt >3 then 
            WSHShell.PopUP "Could not connect to " & strURL, 10, "WAN IP Address",  0    '  + 64
            wscript.quit
          end if
        Loop
      End If
      WAN = objHTTP.responseText 
      i = instr(WAN, "IP Address: ")
      if i > 0 then
        WAN = mid(WAN, i+12)
      end if
      i = instr(WAN, "</body>")
      if i > 0 then
        WAN = mid(WAN, 1, i-1)
      end if
     
    'execute IPCONFIG /ALL and extract info from Exec output stream
      set sExec = WSHShell.Exec("ipconfig /all")
      foundsw = false
      Do While Not sExec.StdOut.AtEndOfStream
        strLineIn = sExec.StdOut.ReadLine()
        if instr(strLineIn, "Description") > 0 then
          Adapter = ParseIt(strLineIn) 
        end if
        if instr(strLineIn, "IP Address") > 0 then
          temp = ParseIt(strLineIn) 
          iparray = split(temp, ".", -1, 1)     'IPv4 format "." separator - NOT IPv6 which uses ":"
          if ubound(iparray) = 3 then          
            IPaddr = temp
            foundsw = true                        
            recommend = iparray(0) & "." & iparray(1) & "." & iparray(2) & "." & "254"
          end if       
        end if
        if instr(strLineIn, "Subnet Mask") > 0 then
          subnet = ParseIt(strLineIn) 
        end if
        if instr(strLineIn, "Default Gateway") > 0 then
          gateway = ParseIt(strLineIn) 
          if foundsw then
            exit do                         'found IP address and related info - stop processing stream 
          end if
        end if
      Loop
      msgtxt = "Adapter: " & adapter & vbcrlf & "IP Address: " & IPaddr & vbcrlf & _
                   "Subnet mask: " &  subnet & vbcrlf & _
                   "Gateway: " & gateway & vbcrlf & _
                   "WAN address: " & WAN & vbcrlf & vbcrlf & _
                   "Recommended: " & recommend
      msgbox msgtxt, vbinformation, "LAN/WAN info"
     
    public function ParseIt(strIN)
      dim i
      i = instr(strIN, ":")
      if i > 0 then
         Parseit = trim(mid(strIN, i+1))
      else
         ParseIt = strIN
      end if
    end function
    

    If you have Microsoft TCP/IP version 6 installed, it complicates the IPCONFIG results, but the script handles it.
    e.g.
    Windows IP Configuration
            Host Name . . . . . . . . . . . . : czapala1
            Primary Dns Suffix  . . . . . . . :
            Node Type . . . . . . . . . . . . : Unknown
            IP Routing Enabled. . . . . . . . : No
            WINS Proxy Enabled. . . . . . . . : No
    Ethernet adapter Local Area Connection:
            Connection-specific DNS Suffix  . :
            Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller
            Physical Address. . . . . . . . . : 00-1D-92-71-AD-F6
            Dhcp Enabled. . . . . . . . . . . : Yes
            Autoconfiguration Enabled . . . . : Yes
            IP Address. . . . . . . . . . . . : 192.168.1.100
            Subnet Mask . . . . . . . . . . . : 255.255.255.0
            IP Address. . . . . . . . . . . . : fe80::21d:92ff:fe71:adf6%4
            Default Gateway . . . . . . . . . : 192.168.1.1
            DHCP Server . . . . . . . . . . . : 192.168.1.1
            DNS Servers . . . . . . . . . . . : 208.67.222.222
                                                74.128.17.114
                                                208.67.220.220
                                                fec0:0:0:ffff::1%1
                                                fec0:0:0:ffff::2%1
                                                fec0:0:0:ffff::3%1
            Lease Obtained. . . . . . . . . . : Wednesday, July 06, 2011 7:56:17 AM
            Lease Expires . . . . . . . . . . : Friday, July 08, 2011 7:56:17 AM
    Tunnel adapter Teredo Tunneling Pseudo-Interface:
            Connection-specific DNS Suffix  . :
            Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
            Physical Address. . . . . . . . . : FF-FF-FF-FF-FF-FF-FF-FF
            Dhcp Enabled. . . . . . . . . . . : No
            IP Address. . . . . . . . . . . . : fe80::ffff:ffff:fffd%5
            Default Gateway . . . . . . . . . :
            NetBIOS over Tcpip. . . . . . . . : Disabled
    Tunnel adapter Automatic Tunneling Pseudo-Interface:
            Connection-specific DNS Suffix  . :
            Description . . . . . . . . . . . : Automatic Tunneling Pseudo-Interface
            Physical Address. . . . . . . . . : C0-A8-01-64
            Dhcp Enabled. . . . . . . . . . . : No
            IP Address. . . . . . . . . . . . : fe80::5efe:192.168.1.100%2
            Default Gateway . . . . . . . . . :
            DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                                fec0:0:0:ffff::2%1
                                                fec0:0:0:ffff::3%1
            NetBIOS over Tcpip. . . . . . . . : Disabled
    
  • Mike GMike G Posts: 2,702
    edited 2011-07-06 06:36
    This one just enumerates all your network interfaces and I believe fulfills your requirements. I was not too keen on the Suggested IP stuff but added it anyway.
    http://forums.parallax.com/attachment.php?attachmentid=82686&d=1309959165
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-07-06 07:00
    Can this also be be done with Windows 7 64 bit

    and if you can how do you do it

    I am not trying to hijack this post
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-06 07:02
    Mike G wrote: »
    This one just enumerates all your network interfaces and I believe fulfills your requirements.

    Mike, it looks like you wrote this in Visual Studio 2010 .NET so I am curious, why does it need to run from a command line?

    Maybe it would appeal more to Jeff if it displayed in a window.

    I haven't done much with dotnet - I still use VB6 alot - old habits are to break.

    I don't think these suggested addresses from your program are legitimate
    Suggested IP(s) ............... : 254.128.0.10
    Suggested IP(s) ............... : 254.128.0.10
    Suggested IP(s) ............... : 254.128.0.10
    Suggested IP(s) ............... : 0.0.0.10
    Suggested IP(s) ............... : 254.128.0.10


    Could you post the source code?
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-06 07:44
    Thanks Ron

    Does this script also work successfully on Windows 7?

    OBC
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-06 07:49
    Thanks Ron

    Does this script also work successfully on Windows 7?

    OBC

    Don't know - maybe someone can try it and let us know.
    I am still a big fan of XP Pro. VISTA was terrible but Windows 7 sounds good - but I am afraid that some development tools I still use may not work under WIN7.
    There are still a LOT of XP users out there...
  • Mike GMike G Posts: 2,702
    edited 2011-07-06 08:36
    Can this also be be done with Windows 7 64 bit
    Ran fine on my Win 7 box. Did you try and it did not work? If so, what was the error?
    Mike, it looks like you wrote this in Visual Studio 2010 .NET so I am curious, why does it need to run from a command line?
    It doesn't really... I did not put in a pause so when the program completes the console goes away. That's an easy thing to fix... lazy I guess. I'll post the source when I get home from work.
    I don't think these suggested addresses from your program are legitimate
    Suggested IP(s) ............... : 254.128.0.10
    Suggested IP(s) ............... : 254.128.0.10
    Suggested IP(s) ............... : 254.128.0.10
    Suggested IP(s) ............... : 0.0.0.10
    Suggested IP(s) ............... : 254.128.0.10

    I simply executed the logic given by OBC's requirements. I can easily filter out the x.x.x.0 IPs.
    Maybe it would appeal more to Jeff if it displayed in a window.
    It is in a window? do you mean a Win form?
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2011-07-06 09:08
    Mike,
    The only valid Private IP addresses according to the IANA are:

    192.168.0.0 – 192.168.255.255.
    10.0.0.0 – 10.255.255.255
    172.16.0.0 – 172.31.255.255
  • Mike GMike G Posts: 2,702
    edited 2011-07-06 09:24
    Thanks Ron;

    I can add IP filtering to a config file so that way thte user can restrict the results. Plus I will not allow redundant entries or adapters that are not "UP".

    Ron, fyi, I'm getting a recommend IP of 170.68.14.254 but I'm at work.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-07-06 10:19
    I've adjusted it down to .200 instead of .254.

    I've seen some printer installs want that address.

    OBC
Sign In or Register to comment.