Jump to content

Recommended Posts

Posted (edited)

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...

Edited by Rawns
Posted (edited)

bat file to pipe and find ?

 

 

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

 

In a bat file ?

 

vbscript :

 

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()

Edited by mac_shinobi
  • Thanks 1
Posted
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. :(

Posted (edited)

Here are a couple of ways...

 

IP.cmd

@echo off
cd /d "%~dp0"
curl -s http://slurpware.org/ > ip.txt

 

^ CURL for Windows can be downloaded from here.

 

IP.ps1

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

 

or

 

(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.

Edited by Arthur
  • Thanks 2
Posted

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 :

 

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

  • Thanks 1

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



×
×
  • Create New...