Jump to content

How do I get a script to pickup the network IP

Recommended Posts

Lets say ipconfig shows the ip address of

how do I get that to load into a script as a variable?


Awesome -- I just went over this with my fellow students in class :)

I use a script to grab my dialup IP address and upload it to an ftp server somewhere.

Below is the main function I use (I haven't updated it in a while, so there may be ways to shorten it with new features).

Func getIP($connectionName)
    $myIP = ""
    RunWait("cmd /c ipconfig > c:\ipconfig.txt", "l:\windows", @SW_HIDE);dumps ip info into file
    $file = FileOpen("c:\ipconfig.txt", 0);opens that file read-only
    If $file = -1 Then;file open failed
        Return $myIP
    $getIP = 0;used to determine when the next IP Address line will be the right one
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop;EOF
        If $getIP Then;ready to grab next IP
            If StringInStr($line, "IP Address") > 0 Then;this is the IP address line
                $getIP = 2 + StringInStr($line, ":");IP is after colon and space; +1 moves on to space; +1 moves on to next character
                If StringLen($line) <= $getIP Then ExitLoop;if nothing appears after colon and space
                $myIP = StringMid($line, $getIP, StringLen($line) - $getIP + 1);gets IP
        If StringInStr($line, "adapter " & $connectionName & ":") > 0 Then $getIP = 1;accept next IP
    Return $myIP

Basically, you dump ipconfig output into a file somewhere and go through that file line-by-line until you find the line where it names the adapter you want. It checks for "adapter " & the name & ":" to avoid false positives (like connections named "IP Address" :D

Ethernet adapter 3Com:

   Connection-specific DNS Suffix  . :
   IP Address. . . . . . . . . . . . :
   Subnet Mask . . . . . . . . . . . :
   Default Gateway . . . . . . . . . :

The next time a line contains "IP Address", it has the IP address you want. So you just trim the fat so you have only the IP address.

This script could be improved a number of ways. Mainly, it's possible for a NIC to have multiple IP addresses assigned to it. Still, do you get the jist of it?

Also, this assumes you know the adapter name, like "Local Area Connection". Otherwise, you could use lines starting with "Ethernet adapter" (LAN) or "PPP adapter" (dialup) or or something else, depending on what you need.


@IPAddress1 !?!? You can do that now? Wow....

But still, if you need a specific connection name when you have more than one, or if you want to adapt the code for a type of connection, there you go :D


Edited by saracoth
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...