Sign in to follow this  
Followers 0
zasxes

How do I get a script to pickup the network IP

3 posts in this topic

Lets say ipconfig shows the ip address of 192.1.2.3

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

Share this post


Link to post
Share on other sites



gosh i was gonna say that, but wasn't sure if that was it or not... :)


FootbaG

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Lets say ipconfig shows the ip address of 192.1.2.3

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

<{POST_SNAPBACK}>

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 = "0.0.0.0"
    RunWait("cmd /c ipconfig > c:\ipconfig.txt", "l:\windows", @SW_HIDE);dumps ip info into file
    Sleep(500)
    $file = FileOpen("c:\ipconfig.txt", 0);opens that file read-only
    If $file = -1 Then;file open failed
        Return $myIP
    EndIf
    $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
                ExitLoop
            EndIf
        EndIf
        If StringInStr($line, "adapter " & $connectionName & ":") > 0 Then $getIP = 1;accept next IP
    WEnd
    FileClose($file)
    Return $myIP
EndFunc

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. . . . . . . . . . . . : 172.31.0.152
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 172.31.0.1

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.

edit:

@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

/edit

Edited by saracoth

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