Reputation: 1682
Is there any simple command to write the ip-address into a file?
I know how to write in a file, but is there a sysvar or something!?
Upvotes: 3
Views: 19235
Reputation: 21
@echo off
PowerShell.exe -Command "(Invoke-WebRequest ifconfig.me/ip).Content.Trim()" >> ipToFile.txt
Upvotes: 0
Reputation:
here is a vbs script that will do the job. Note that this is for the external IP address. Just look above for an internal/local ip address.
here is the code. just create a text document, paste this, and rename it to something.vbs
Const ForReading = 1
Const ForAppending = 8
Dim ipLog, objHTTP, strHTML, varStart
Dim varStop, strIP, strCurrIP, objFSO
Dim txtFile, strLine, objShell
' Log for tracking external IP addresses
ipLog = "ExternalIP.txt"
' Get current external IP address from web
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
objHTTP.Send()
strHTML = objHTTP.ResponseText
' Extarct IP from HTML if HTML was recieved
If strHTML <> "" Then
varStart = InStr(1, strHTML, "Current IP Address:", vbTextCompare) + 19
If varStart Then varStop = InStr(varStart, strHTML, "</body>", vbTextCompare)
If varStart And varStop Then strIP = Mid(strHTML, varStart, varStop - varStart)
Else
strIP = "Unavailable"
End If
' Remove preceeding or trailing spaces
strCurrIP = Trim(strIP)
' Check for log file and last log entry
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not (objFSO.FileExists(ipLog)) Then
' If log file doesn't exist create it
Set txtFile = objFSO.CreateTextFile(ipLog, True)
strIP = ""
Else
' Get last external IP address entry from log file
Set txtFile = objFSO.OpenTextFile(ipLog, ForReading)
Do Until txtFile.AtEndOfStream
strLine = txtFile.ReadLine
If Len(strLine) > 0 Then
strIP = strLine
End If
Loop
End If
txtFile.Close
' Extarct last external IP from log file entry
If strIP <> "" Then
varStart = 1
varStop = InStr(varStart, strIP, ",", vbTextCompare) - 1
If varStop Then strIP = Mid(strIP, varStart, varStop - varStart)
' Remove preceeding or trailing spaces
Trim(strIP)
Else
strIP = "Unavailable"
End If
' Copy IP to clipboard
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "CMD /C ECHO " & strCurrIP & " | CLIP", 2
' Check if external IP has changed
If strCurrIP = strIP Then
' If unchanged display IP
MsgBox "External IP: " & strCurrIP & " is unchanged"
Else
' If changed log to file and display IP
Set txtFile = objFSO.OpenTextFile(ipLog, ForAppending)
txtFile.Write(strCurrIP & vbTab & vbCrLf)
txtFile.Close
MsgBox "External IP: " & strCurrIP & vbCrLf & "This IP address has been logged"
End If
' Clear variables
Set ipLog = Nothing
Set objHTTP = Nothing
Set strHTML = Nothing
Set varStart = Nothing
Set varStop = Nothing
Set strIP = Nothing
Set strCurrIP = Nothing
Set objFSO = Nothing
Set txtFile = Nothing
Set strLine = Nothing
Set objShell = Nothing
I do not take credit for this script, I just found it in a folder on my computer that I hadn't touched in a long time.
Upvotes: 1
Reputation: 21
For Windows 7 machines:
ipconfig | findstr /b /c:" IPv4" > output.txt
There are three whitespace characters between the opening quotation mark and IPv4 since that line technically begins with whitespace. I am unaware of a way to strip that prior to the findstr command.
Remember that, even though it's technically regular expressions, the Windows command line doesn't parse them the same way as, say, C# or whatever. There's a list of the acceptable sequences/wildcards (marked for XP, but it worked for me in a Win7 environment) here.
Took me a little trial and error, but this gets you ONLY the lines for assigned IPv4 addresses, and not the "Autoconfigured" stuff that clutters the results of other findstr iterations.
Upvotes: 2
Reputation: 1
Just added a little to also display the gateway (your router) and ping to see if your DNS is working:
@echo off
:ipaddress
::Get IP address and save it to ip
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IP Address"') do set ip=%%b
set ip=%ip:~1%
:gateway
::Get Gateway address and save it to gateway
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Gateway"') do set gateway=%%b
set gateway=%gateway:~1%
echo IP address is %ip%
echo You router address is %gateway%
pause
cls
ping %gateway% -a
pause
:end
Upvotes: 0
Reputation: 685
Is this what you're looking for?
@echo on
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IP Address"') do set ip=%%b
set ip=%ip:~1%
echo %ip%
Upvotes: 2
Reputation: 55334
ipconfig | find "IP Address" > out.txt
You still need to extract the IP Address from "IP Address.............: 0.0.0.0" and trim any whitespace.
Upvotes: 3