Page 1 of 1 1
Topic Options
#61430 - 2001-12-18 02:37 AM How do you obtain "active" IPADDRESS0,1,2 etc..
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
I'm looking for a way or method to get back the IP address of the "active" Network connection.

Sample:

code:
SELECT
CASE (SUBSTR(@IPADDRESS0,1,11)) = "177. 21.128"
$logsrv = "SERVER5"
$spserver = "SERVER4"
CASE (SUBSTR(@IPADDRESS0,1,7)) = "132.105"
$logsrv = "SERVER2"
$spserver = "SERVER2"
CASE (SUBSTR(@IPADDRESS0,1,7)) = "155. 6"
$logsrv = "SERVER3"
$spserver = "SERVER3"
CASE 1
$logsrv = "SERVER1"
$spserver = "unknown"
ENDSELECT

This will get me back the IP Address of the 0 interface but not the 1, 2, 3 etc...

Some systems may have other PCMCIA cards, or AOL, Dialup connections, or a dual nic card.

How can I detemine which card is active and get the IP address of that one and not just refer to the 0 one.

On some systems you get back the built in default IP for Windows 2000
169.254.101.152


Top
#61431 - 2001-12-18 05:39 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
bleonard Offline
Seasoned Scripter
*****

Registered: 2001-01-19
Posts: 581
Loc: Chicago, IL
NTDOC -
Thinking off the top of my head, you could:

+ Win2K-class, run NBTSTAT -n to a file and use Readline for the value appended to 'Node IpAddress' in the 3rd line of the file.

+ WinNT-class, obtain IP address(es) using @IPADDRESSx, run NBTSTAT -A (ipaddress) to file, and check for 'Host not found' in the 1st line of the file.

Neither process is especially fast for login script usage, but shoud provide what you are looking for using existing NT tools with minimal scripting on your part.

Bill

[ 18 December 2001: Message edited by: bleonard ]

Top
#61432 - 2001-12-18 06:44 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
look at this and see if it is helpful. It only works under w2k
code:

; find ipconfig info (for up to 10 adapters)
$interfaces="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
$nic=0 dim $ipaddrs[9] dim $gateway[9] dim $subnetm[9] dim $namesvr[9]
:nicloop
$niccard=enumkey("$interfaces",$nic)
if @error=0
$Isdhcp=readvalue("$interfaces\$niccard","EnableDHCP")
if $Isdhcp="1" $adddhcp="Dhcp" else $adddhcp="" endif
$ipaddrs[nic]=readvalue("$interfaces\$niccard","$adddhcp"+"IPAddress")
$gateway[nic]=readvalue("$interfaces\$niccard","$adddhcp"+"DefaultGateway")
$subnetm[nic]=readvalue("$interfaces\$niccard","$adddhcp"+"SubnetMask")
$namesvr[nic]=readvalue("$interfaces\$niccard","$adddhcp"+"NameServer")
$gateway[nic]=left($gateway[nic],len($gateway[nic])-1)
if $gateway[nic] > "0.0.0.0" $mygw=$gateway[nic] endif
$nic=$nic+1
goto nicloop
endif

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#61433 - 2001-12-19 01:35 AM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Thanks for the replies Bill and Rad.

Bill your idea would probably work, but as you said it would slow down the logon script even more just to try and catch a few odd machines.

Radimus,
I could not get valid data back from your idea on my Windows 2000 system.

At this point I would not call it a bug, but perhaps a request for improvement or added feature for an upcoming KiXtart update.

Top
#61434 - 2001-12-19 02:11 AM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
bleonard Offline
Seasoned Scripter
*****

Registered: 2001-01-19
Posts: 581
Loc: Chicago, IL
NTDOC -
You could incorporate both of our suggestions to reduce impact on users. Use Radimus's to find systems with multiple NIC's, then mine to query only those systems for more precise info. Just trying to make more work for you if at all possible...

Bill

PS - appears typo in Radimus post for variables using [nic] instead of [$nic]

Top
#61435 - 2001-12-19 08:40 AM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Here is what I get back from Radimus's code.

code:
; find ipconfig info (for up to 10 adapters)
$interfaces="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
$nic=0 dim $ipaddrs[9] dim $gateway[9] dim $subnetm[9] dim $namesvr[9]
:nicloop
$niccard=enumkey("$interfaces",$nic)
if @error=0
$Isdhcp=readvalue("$interfaces\$niccard","EnableDHCP")
if $Isdhcp="1" $adddhcp="Dhcp" else $adddhcp="" endif
$ipaddrs[$nic]=readvalue("$interfaces\$niccard","$adddhcp"+"IPAddress")
$gateway[$nic]=readvalue("$interfaces\$niccard","$adddhcp"+"DefaultGateway")
$subnetm[$nic]=readvalue("$interfaces\$niccard","$adddhcp"+"SubnetMask")
$namesvr[$nic]=readvalue("$interfaces\$niccard","$adddhcp"+"NameServer")
$gateway[$nic]=left($gateway[nic],len($gateway[$nic])-1)
if $gateway[$nic] > "0.0.0.0" $mygw=$gateway[$nic] endif
$nic=$nic+1
goto nicloop
endif
? $nic
? $mygw
? $gateway[$nic]

code:

C:\SCRIPTS\KiX2001>kix32 checknic.kix

4



Maybe I'm doing something wrong... I only get back the number 4 on screen. I have 4 Nic Adapters... As shown by the WMI Code..

code:
System Manufacturer is  = System Manufacturer
System Model is = System Name
System Serial Number is = SYS-1234567890
System BIOS Version is = ASUS CUSL2 ACPI BIOS Revision 1009
System BIOS Date is = Award Medallion BIOS v6.0
CPU Speed is = 938 Mhz
System Memory = 255 MB
Dimm Size = 128 MB
Dimm Size = 128 MB
Video Card is = All-in-Wonder RADEON
Video Res is = 1600 x 1200 x 4294967296 colors
Modem is =
Network Card is = Cisco Systems VPN Adapter
Network Card is = Cisco Systems VPN Adapter
Network Card is = Cisco Systems VPN Adapter
Network Card is = WAN Network Driver
Network Card is = Linksys LNE100TX(v5) Fast Ethernet Adapter

Please point me in the right direction here Radimus...

Top
#61436 - 2001-12-19 03:04 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
You guys are picky... wanting code to actually work

It also seems to work under XP... (of course 2k and xp are about the same thing...)

code:

; find ipconfig info (for up to 10 adapters)
$interfaces="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces"
$nic=0 dim $ipaddrs[9] dim $gateway[9] dim $subnetm[9] dim $namesvr[9]
:nicloop
$niccard=enumkey("$interfaces",$nic)
if @error=0
$Isdhcp=readvalue("$interfaces\$niccard","EnableDHCP")
if $Isdhcp="1" $adddhcp="Dhcp" else $adddhcp="" endif

$ipaddrs[$nic]=readvalue("$interfaces\$niccard","$adddhcp"+"IPAddress")
$gateway[$nic]=readvalue("$interfaces\$niccard","$adddhcp"+"DefaultGateway")
$subnetm[$nic]=readvalue("$interfaces\$niccard","$adddhcp"+"SubnetMask")
$namesvr[$nic]=readvalue("$interfaces\$niccard","$adddhcp"+"NameServer")

; trim off pipe char from end of reg_multi
$ipaddrs[$nic]=left($ipaddrs[$nic],len($ipaddrs[$nic])-1)
$gateway[$nic]=left($gateway[$nic],len($gateway[$nic])-1)
$subnetm[$nic]=left($subnetm[$nic],len($subnetm[$nic])-1)

if $gateway[$nic] > "0.0.0.0"
$mygw=$gateway[$nic]+"|"+$mygw
? "$adddhcp NIC number $nic"
? "IP :"$ipaddrs[$nic]
? "GW :"$gateway[$nic]
? "SM :"$subnetm[$nic]
? "NS :"$namesvr[$nic]
endif
$nic=val($nic)+1
goto nicloop
endif

$mygw=left($mygw,len($mygw)-1)
? "My Gateways are: "$mygw


output for me is:

code:

NIC number 1
IP :192.168.110.12
GW :192.168.110.1
SM :255.255.255.0
NS :65.32.2.130,65.32.1.70
My Gateways are: 192.168.110.1

[ 19 December 2001: Message edited by: Radimus ]

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#61437 - 2001-12-19 04:43 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
bleonard Offline
Seasoned Scripter
*****

Registered: 2001-01-19
Posts: 581
Loc: Chicago, IL
Radimus -
No complaints from me, except this code seems to work, which is causing me problems as I now have to use it. Can I get some more of that buggy stuff that doesn't work?

Bill

[ 19 December 2001: Message edited by: bleonard ]

Top
#61438 - 2001-12-19 05:22 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Tested Radimus' latest code on my system. This is the result:
quote:

Dhcp NIC number 0
IP :10.198.50.11
GW :10.198.1.1
SM :255.255.0.
NS :10.198.10.1 10.198.10.18
My Gateways are: 10.198.1.1

Note missing 0 on SM.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#61439 - 2001-12-19 05:51 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
bleonard Offline
Seasoned Scripter
*****

Registered: 2001-01-19
Posts: 581
Loc: Chicago, IL
NTDOC and Radimus -
Poking some more in the Win2k registry and my login script I've some more things that may be useful.

1. Check key 'HKLM\Software\MS\Windows NT\CV\NetworkCards\(enum key)' ($WinKey). This will provide descriptive name of NIC that is displayed under LAN connection. If multiple NIC cards installed on the system they will be listed here. Most importantly, Will also provide the 32-character alphanumeric string name used elsewhere to identify the NIC under value '$WinKey, ServiceName' ($Nic).


2. You can also check key 'HKLM\System\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}' ($ClassKey). This key contains more NIC details. Specifically, you would want to check '$ClassKey\(enum subkey), NetCfgInstanceId' ($IPKey) for value that matches $Nic found in step 1. Then you could check '$IPKey\Linkage, UpperBind' for value 'Tcpip' to reconfirm this device is using the Tcpip protocol and is a valid NIC selection.

3. Can also examine key 'HKLM\System\CCS\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\$Nic, Name' to confirm the connection type.

4. Check key 'HKLM\System\CCS\Services\$Nic\Parameters\Tcpip'. This will display some of the IP configuration info stored in other reg keys referenced below in point 7.

5. Check 'HKLM\System\CCS\Services\RemoteAccess\Interfaces\(enum key), InterfaceName'. If value is = $Nic you have a NIC. Other values I know of are loopback, Internal.

6. Check key 'HKLM\System\CCS\Services\Tcpip\Linkage, Bind' to track the adapters bound to TCPIP protocol. Value under 'HKLM\System\CCS\Services\Tcpip\Linkage, Route' will likely define the NIC that is live on the network, which was NTDOC's original goal. The entries here are REG_MULTI_SZ, so I've yet to test the values returned with KiX 3.63.

7. Check key 'HKLM\System\CCS\Services\Tcpip\Parameters\Interfaces\$Nic'. As noted by Radimus, this contains the most detailed info of the IP configuration of the NIC.

It appears 'HKLM\System\CCS\Services\Tcpip\Parameters\Interfaces\' will list IP-aware devices beyond specifically the installed Ethernet adapters. Therefore, use values from 1 above to narrow the keys to check.

As for WinNT systems, check 'HKLM\System\CCS\NetBT\Adapters' to obtain NIC value ($Nic).

Next, check key 'HKLM\System\CCS\Services\$Nic, Start'. This value will let you know specifically if the NIC is started when the system is booted. I believe a value of '3' indicates this is the case, though I am not positive of this value just yet. This will help for systems with multiple NIC's which are not run simultaneously.

Hope this info is useful.

Bill

Top
#61440 - 2001-12-19 08:47 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
You can try to use this to clean up the output instead of the left statepemts

code:

$mygw=split($mygw,"|")
? "My Gateways are: " for each $gw in $mygw "$gw " next

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#61441 - 2001-12-19 09:27 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Ok, Radimus' code now works on my system as well.

code:
Dhcp    NIC number 1
IP :125.104.217.19
GW :125.104.217.254
SM :255.255.255.
NS :125.104.100.1 155.2.254.252
My Gateways are: 125.104.217.254

As for Bill's stuff - Wow... that will take me a little while to digest and test...
Thanks Bill.

Thanks Radimus... cool stuff.

Top
#61442 - 2001-12-19 10:31 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Bill, (or others)

How can I enum the subkey of indexes in a loop? to get and set $WinKey for "ServiceName"

code:
$Index = 0
:Loop1
$WinKey = ENUMKEY("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards", $Index)
If @ERROR = 0
? "Name found: $WinKey"
$Index = $Index + 1
goto Loop1
Endif

I think I have found another way also. The key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0000\MWIEnable when set to 1 appears to "indicate" that this is the live connecting NIC card. I'm not sure how to do an Enumkey based on another enumkey of the main key.

I'm not positive, but I think MWI stands for Message Waiting Indicator. I have looked at a couple of remote systems and compared this entry to the pinging address and each one matches the interface when this setting is set to 1

[ 19 December 2001: Message edited by: NTDOC ]

Top
#61443 - 2001-12-19 11:45 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
try this:

code:

$netcardkey="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards"
$servicekey="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services"
$Index1 = 0
:Loop1
$WinKey = ENUMKEY("$netcardkey", $Index1)
If @ERROR = 0
? "Card found: HKLM\S\M\WNT\CV\NetworkCards\$winkey"
$nicID=readvalue("$netcardkey\$winkey","Servicename")
$nicCN=readvalue("$netcardkey\$winkey","Description")
if existkey("$servicekey\$nicID")=0
$ip=readvalue("$servicekey\$nicID\parameters\tcpip","IPAddress")
$sm=readvalue("$servicekey\$nicID\parameters\tcpip","SubnetMask")
$gw=readvalue("$servicekey\$nicID\parameters\tcpip","DefaultGateway")
$ed=readvalue("$servicekey\$nicID\parameters\tcpip","EnableDHCP")
? $nicCN
if $ed="0" ? "Static" else ? "DHCP" endif
? "IPAddress :"$ip
? "SubnetMask :"$sm
? "DefaultGateway :"$gw
?
endif
$Index1 = $Index1 + 1
goto Loop1
Endif

[ 20 December 2001: Message edited by: Radimus ]

[ 20 December 2001: Message edited by: Radimus ]

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#61444 - 2001-12-20 12:00 AM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Cool Radimus,

That works... (worse then a nested IF statement though ) Now let me work on doing some other matching to see what I can come up with.

Thanks again for the help. I appreciate it.

Top
#61445 - 2001-12-20 10:50 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Radimus,
Not sure whether you caught my subtle hint, but your routine to strip off the pipe char, will strip the last char regardless of whether it is pipe or not. Perhaps an IF condition to remove the pipe only idf it exists.

if substr($subnetm[$nic],len($subnetm[$nic]),1)="|" $subnetm[$nic]=left($subnetm[$nic],len($subnetm[$nic])-1) endif

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#61446 - 2001-12-20 11:36 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
I noticed that...

I intended to use split to knock off the end pipe

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#61447 - 2001-12-27 02:37 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Anonymous
Unregistered


Maybe using enumipinfo will make it a lot easier?
When I read many threads it seems that not many people knows this function exist but is really worth to use it.

Top
#61448 - 2001-12-27 06:46 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
bleonard Offline
Seasoned Scripter
*****

Registered: 2001-01-19
Posts: 581
Loc: Chicago, IL
AWinkel -
While this is useful, will only work under KiX 4.x, and will not work for WinNT systems or systems not running IE 5.x or higher. Great solution for all WinXP/2K environment though.
Bill

Top
#61449 - 2001-12-27 07:31 PM Re: How do you obtain "active" IPADDRESS0,1,2 etc..
Anonymous
Unregistered


and for W9x machines.
Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.304 seconds in which 0.234 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org