+ Post New Thread
Results 1 to 7 of 7
General Chat Thread, Get IP address into a text file? in General; Got a network at home and I'm after a clever way of getting my 'master' IP address and storing it ...
  1. #1
    Guest

    Join Date
    Jun 2009
    Posts
    3,754
    Thank Post
    1,458
    Thanked 493 Times in 376 Posts
    Rep Power
    0

    Get IP address into a text file?

    Got a network at home and I'm after a clever way of getting my 'master' IP address and storing it in a text file. Basically, I run a L4D2 dedicated server and to get it visible, I need to have the IP address in the start up file, however I don't have a static IP. I was hoping to some how get my main IP into a text file so I could create a batch file to start the server and pull in the current IP. I could then create a couple of batch files and scheduled tasks to restart the server every 12 or 24 hours ensuring the server starts with the latest IP address. Would save me having to do it manually!

    Edit: I do use DynDNS however the cvar command in the startup is +net_public_adr and it only seems to be happy with an IP rather then domain name...
    Last edited by Rawns; 31st August 2012 at 04:11 PM.

  2. #2

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,052
    Thank Post
    3,583
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377
    bat file to pipe and find ?


    ipconfig /all | Find "IPv4">c:\ipaddy.txt

    In a bat file ?

    vbscript :

    Code:
    Dim colIPResults, objFile, objFSO, objNIC, objWMI, objWSHNetwork, strAddresses, strIPAddress, strWQL, strComp
    Const FOR_APPENDING = 8
    
    
    Sub DestroyObjects()
    If IsObject(objFile) Then Set objFile = Nothing
    If IsObject(objFSO) Then Set objFSO = Nothing
    If IsObject(objWMI) Then Set objWMI = Nothing
    If IsObject(objWSHNetwork) Then Set objWSHNetwork = Nothing
    ' If IsObject() Then Set = Nothing
    End Sub
    
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objWSHNetwork = CreateObject("WScript.Network")
    Set objWMI = GetObject("WinMGMTS:root\cimv2")
    strWQL = "SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"
    Set colIPResults = objWMI.ExecQuery(strWQL)
    For Each objNIC In colIPResults
    For Each strIPAddress in objNIC.IPAddress
    If strAddresses = "" Then
    strAddresses = strIPAddress
    Else
    strAddresses = strAddresses
    End If
    Next
    Next
    
    strComp = objWSHNetwork.ComputerName
    
    If strAddresses ="0.0.0.0" Or strAddresses ="" or strAddresses = "undefined" Then
        msgbox ("No Connection Detected")
    Else
        msgbox "Network Address - " & strAddresses & vbcrlf & "Computer Name - " & strComp
    End If
    
    DestroyObjects()
    Last edited by mac_shinobi; 31st August 2012 at 04:15 PM.

  3. Thanks to mac_shinobi from:

    Rawns (31st August 2012)

  4. #3
    Guest

    Join Date
    Jun 2009
    Posts
    3,754
    Thank Post
    1,458
    Thanked 493 Times in 376 Posts
    Rep Power
    0
    Quote Originally Posted by mac_shinobi View Post
    bat file to pipe and find ?


    ipconfig /all | Find "IPv4">c:\ipaddy.txt

    In a bat file ?
    It returns my LAN address 192.168.0.3 rather then the IP from my ISP.

  5. #4

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,052
    Thank Post
    3,583
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377
    Quote Originally Posted by Rawns View Post
    It returns my LAN address 192.168.0.3 rather then the IP from my ISP.
    oh - sorry. There is a way but would have to dabble about a bit

  6. #5


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,796
    Thank Post
    262
    Thanked 2,964 Times in 2,179 Posts
    Rep Power
    846
    Here are a couple of ways...

    IP.cmd
    Code:
    @echo off
    cd /d "%~dp0"
    curl -s http://slurpware.org/ > ip.txt
    ^ CURL for Windows can be downloaded from here.

    IP.ps1
    Code:
    (New-Object System.Net.WebClient).DownloadString("http://slurpware.org")
    or

    Code:
    (New-Object System.Net.WebClient).DownloadString("http://checkip.dyndns.org") -replace "[^\d\.]"
    With PowerShell you could write the IP directly to your L4D2 startup file. I don't have the dedicated server installed myself, so I am unable to test.
    Last edited by Arthur; 31st August 2012 at 04:45 PM.

  7. 2 Thanks to Arthur:

    mac_shinobi (31st August 2012), Rawns (31st August 2012)

  8. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,052
    Thank Post
    3,583
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377
    Last comment from here :

    How to get my external IP address (over NAT) from the Windows command-line? - Super User

    Then just a case of writing the output of this to a text file which is easy enough :

    DevGuru VBScript Method: FileSystemObject.CreateTextFile


    Combined Code :

    Code:
    dim filesys, filetxt, getname, path
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set filetxt = filesys.CreateTextFile("c:\ip to txt\somefile.txt", True)
    path = filesys.GetAbsolutePathName("c:\ip to txt\somefile.txt")
    getname = filesys.GetFileName(path)
    filetxt.WriteLine WAN_IP()
    filetxt.Close
    
    wscript.echo WAN_IP()
    
    function WAN_IP()
        set obj = createobject("Microsoft.XMLHTTP")
        call obj.open("get", "http://ifconfig.me/ip", false)
        obj.send()
    
        strresponse = obj.responsetext
        set obj = nothing
    
        if strresponse <> "" then
            strIP = strresponse
        else
            strIP = "Unavailable"
        end if
    
        WAN_IP = trim(strIP)
    
    end function
    You would need to edit lines 3 and 4 to make it create the text file in the relevant path and name the text file something a bit better than somefile.txt

    I think the 4th line is just to get the path and getname etc you may not need so you could probably delete those lines

  9. Thanks to mac_shinobi from:

    Rawns (31st August 2012)

  10. #7
    Guest

    Join Date
    Jun 2009
    Posts
    3,754
    Thank Post
    1,458
    Thanked 493 Times in 376 Posts
    Rep Power
    0
    Thanks both. That's worked a treat.

    I'd +rep but for some reason, the box is not loading properly... :S

  11. Thanks to Rawns from:

    mac_shinobi (31st August 2012)



SHARE:
+ Post New Thread

Similar Threads

  1. [SIMS] SOLUS 3 - Unable to Get an IP Address For This Agent ... ?
    By MyBrainHurts in forum MIS Systems
    Replies: 5
    Last Post: 11th September 2013, 07:17 PM
  2. Wireless problems. (cant get ip address) (solved)
    By newpersn in forum Wireless Networks
    Replies: 1
    Last Post: 2nd May 2012, 12:30 PM
  3. Remote Access to files without public IP address
    By Admiral208 in forum How do you do....it?
    Replies: 5
    Last Post: 19th July 2011, 06:21 PM
  4. 802.1x PCs won't get IP addresses
    By jamesfed in forum Windows Server 2008
    Replies: 2
    Last Post: 6th December 2010, 11:24 AM
  5. Import a text file into a live web page
    By mrlegs in forum Web Development
    Replies: 9
    Last Post: 29th January 2008, 03:56 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •