Sign in to follow this  
Followers 0
gspino

How to determine which net adaptor has valid IP

14 posts in this topic

I've been writing a program and need help with a routine please. I need to be able to determine which of three network adaptors has a valid IP address and put it in a variable. Thank you in advance!

Here's some crude code that I'm playing with that shows what I'm trying to do.

If Not (StringLeft(@IPAddress1, 1) = "0") Or (StringLeft(@IPAddress1, 3) = "127" Or StringLeft(@IPAddress1, 3) = "169") Then $var3 = "1"

If Not (StringLeft(@IPAddress2, 1) = "0") Or (StringLeft(@IPAddress2, 3) = "127" Or StringLeft(@IPAddress2, 3) = "169") Then $var3 = "2"

If Not (StringLeft(@IPAddress3, 1) = "0") Or (StringLeft(@IPAddress3, 3) = "127" Or StringLeft(@IPAddress3, 3) = "169") Then $var3 = "3"

$var4="@ipaddress"&$var3

MsgBox(0, "Network Detected", "Your IP address is : " & $var4)

Share this post


Link to post
Share on other sites



It shouldn't be that hard - a NIC that has no IP will return '0.0.0.0', so just check for that. Untested striaght-out-of-my-brain code follows:

Func GetIpAddys()
  Dim $Addys[1]
  If @IPAddress1 <> '0.0.0.0' Then _ArrayAdd($Addys, @IPAddress1)
  If @IPAddress2 <> '0.0.0.0' Then _ArrayAdd($Addys, @IPAddress2)
  If @IPAddress3 <> '0.0.0.0' Then _ArrayAdd($Addys, @IPAddress3)
  If @IPAddress4 <> '0.0.0.0' Then _ArrayAdd($Addys, @IPAddress4)
  Addys$[0] = UBound($Addys)-1
  Return $Addys
EndFunc

Share this post


Link to post
Share on other sites

Thanks. My biggest obstacle is that Windows XP will autoassign a 169.x.x.x or a 127.0.0.1 address to valid network adaptors even if they're not connected to the network. I'm back to the drawing board on this. My goal is not to just determine if the network adaptor exists, but that it has an ip address outside of localhost or the private range. Help is still needed please.

Share this post


Link to post
Share on other sites

network.  I'm back to the drawing board on this.  My goal is not to just determine if the network adaptor exists, but that it has an ip address outside of localhost or the private range.  Help is still needed please.

What do you want to do with the information about the ip adress? Maybe there is different solution, if we know the problem better

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

your 'staigt-out-of' your brain sucks :(

Func GetIpAddys()
  Local $s_return
  If @IPAddress1 <> '0.0.0.0' Then $s_return = @IPAddress1
  If @IPAddress2 <> '0.0.0.0' Then $s_return = @IPAddress2
  If @IPAddress3 <> '0.0.0.0' Then $s_return = @IPAddress3
  If @IPAddress4 <> '0.0.0.0' Then $s_return = @IPAddress4
  Return $s_return
EndFunc

would have a bit more chance of working.

gonna go to bed now.

srry that it doesnt support multi-ip's like his.

but his is faulty and takes long to execute even if it worked.

add an array tomorrow if noone else did it already.


My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thank you. Our company has hundreds of wan circuits that we support remotely. I'm creating a new sysprep image that runs this utility as a post sysprep task. The goal is to rename the machine, join it to the domain via wan, lan or ras. If the machine is not on network or dialup we can't remote and finish the install. I had everything worked out until we received a new batch of laptops with wireless. So there's the potential to have three network adaptors, none or one of which may be actively on the lan. This raised some issues during testing.

The routine in the attached code that I'm having trouble is: IF LAN NOT DETECTED LAUNCH DIALER

I use the logic to make a decision to bring up the dialer, wait for a connection then continue the processes. I've attached the full code, changing domain specific info, phone numbers, admin passwords and such. Thanks so much for your help. BTW, the only formal programming training I had was Fortran way back in the late 70's - otherwise I'm self taught, so forgive my logic. I know there's always a better or more efficient way - I just want it to work flawlessly. -Glen

Edited by gspino

Share this post


Link to post
Share on other sites

Thanks.  My biggest obstacle is that Windows XP will autoassign a 169.x.x.x or a 127.0.0.1 address to valid network adaptors even if they're not connected to the network.  I'm back to the drawing board on this.  My goal is not to just determine if the network adaptor exists, but that it has an ip address outside of localhost or the private range.  Help is still needed please.

<{POST_SNAPBACK}>

Are you sure about this? I don't have the time to test it, but IIRC:

127.0.0.1 is assigned to a virtual NIC called the loopback adapter. This isn't a physical NIC, just a mapped address.

169.254.x.x is a range called APIPA - Basically, if there is no DHCP server found, you will have an IP in this range.

If there is no network to connect to, the IP should be 0.0.0.0

if you want to check that the IP is outside the private range, just check it against the private IP ranges:

10.0.0.0 - 10.255.255.255

172.16.0.0 - 172.31.255.255

192.168.0.0 - 192.168.255.255

169.254.0.0 -169.254.255.255

But just because an IP falls into one of these ranges doesn't mean that it doesn't access a public network - it could always be proxied or routed.

Share this post


Link to post
Share on other sites

The routine in the attached code that I'm having trouble is: IF LAN NOT DETECTED LAUNCH DIALER

O.K. And what exactly is your question now? Do you want to know if there is LAN or WLAN connectivity, and if not then lauch Dialer?

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Yes. Sounds simple, but it's got me stumped.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Yes.  Sounds simple, but it's got me stumped.

<{POST_SNAPBACK}>

Nah, easy :( Get the default gateway. If there is none or you can't ping() it then start the dialer, as there is no network connection. To get the default gateway run "netstat -rn" and search for the string "default gateway". StringSplit after ":" , trim string and ping(default gw), et Voila!

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Awesome. I can't believe an old command line guy like me didn't think of that. Thanks so much - you're the best. -Glen

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Awesome.  I can't believe an old command line guy like me didn't think of that.  Thanks so much - you're the best. -Glen

you're welcome. I just used that in my current project :(

EDIT: Btw. here is my function...

const $TEXT_DEF_GW = "Default Gateway"

if ping(get_default_gw()) = 0 then
   msgbox(0, "INFO", "No LAN connection")
endif

func get_default_gw()
    
    local $pid, $line, $ipaddr = 0, $tempfile

    $tempfile = _TempFile()

   ;----------------------------------------------------------------------------
   ;-- Run the DOS command "route print" and parse the output for language specific
   ;-- features. 
    
    $pid = RunWait(@ComSpec & " /c route print >" & $tempfile, @SystemDir, @SW_HIDE)

    $fh = FileOpen($tempfile,0)
    
    if $fh = -1 then
   ; show_error($ERROR_FILE_CANNOT_OPEN_TMP_FILE, $tempfile)
    endif
    
   ;-- Parse the standard output of the above command  -------------------------
    while 1
        $line = FileReadLine($fh)
        if @error = 1 then
       ; show_error($ERROR_FILE_CANNOT_READ_TMP_FILE, $tempfile)
        endif 
        
        if @error = -1 then exitloop
        if StringInStr($line, $TEXT_DEF_GW) then
            $fields = StringSplit($line,":")
            $ipaddr = StringStripWS($fields[2],8)
        endif 
    wend
    FileClose($fh)
    FileDelete($tempfile)
    
    return $ipaddr
    
endfunc

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

your 'staigt-out-of' your brain sucks  :(

...

is faulty and takes long to execute even if it worked.

<{POST_SNAPBACK}>

Eh, what's wrong with it? It runs fine and isn't slow. Infact it's so simple, it couldn't be any slower than yours, except for the small fraction of a second it takes to run _ArrayAdd and UBound. I don't understand???

Anyway, try this code:

CODE

#include <Array.au3>

Func _IPtoInt($IP)

Dim $aOctet = StringSplit($IP,'.'), $i, $Return

If $aOctet[0] = 4 Then

For $i = 1 To 4

$Return = $Return * 256 + mod(abs($aOctet[$i]),256)

Next

Return $Return

Else

Return 0

EndIf

EndFunc

Func _IPOnSameNet($IP1, $IP2, $NetMask)

$iIP1 = _IPtoInt($IP1)

$iIP2 = _IPtoInt($IP2)

$iMask = _IPtoInt($NetMask)

Return BitAND($iIP1, $iMask) = BitAND($iIP2, $iMask)

EndFunc

Func _IPIsPublic($IP)

Dim $Public = 1

If $Public Then $Public = Not _IPOnSameNet($IP, '0.0.0.0', '255.255.255.255')

If $Public Then $Public = Not _IPOnSameNet($IP, '10.0.0.0', '255.0.0.0')

If $Public Then $Public = Not _IPOnSameNet($IP, '172.16.0.0', '255.240.0.0')

If $Public Then $Public = Not _IPOnSameNet($IP, '169.254.0.0', '255.255.0.0')

If $Public Then $Public = Not _IPOnSameNet($IP, '192.168.0.0', '255.255.0.0')

Return $Public

EndFunc

Func _IPGetPublicList()

Dim $Addys[1]

If _IPIsPublic(@IPAddress1) Then _ArrayAdd($Addys, @IPAddress1)

If _IPIsPublic(@IPAddress2) Then _ArrayAdd($Addys, @IPAddress2)

If _IPIsPublic(@IPAddress3) Then _ArrayAdd($Addys, @IPAddress3)

If _IPIsPublic(@IPAddress4) Then _ArrayAdd($Addys, @IPAddress4)

$Addys[0] = UBound($Addys)-1

Return $Addys

EndFunc

The function _IPGetPublicList() returns an array of valid public IP addresses

Share this post


Link to post
Share on other sites

Kurt,

Your logic was easy for me to understand. Not only did it solve my issue, but I learned something in the process. Thanks again, and thanks to all that contributed. -Glen

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0