将IP地址写入文件

时间:2010-12-15 15:09:45

标签: batch-file command cmd ip

是否有任何简单的命令将ip-address写入文件?

我知道如何写一个文件,但是有一个sysvar或什么的东西!?

7 个答案:

答案 0 :(得分:3)

最简单的我能想到:

ipconfig > file

答案 1 :(得分:3)

ipconfig | find "IP Address" > out.txt

您仍然需要从“IP地址.............:0.0.0.0”中提取IP地址并修剪任何空格。

答案 2 :(得分:2)

这是你要找的吗?

@echo on

for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IP Address"') do set ip=%%b

set ip=%ip:~1%

echo %ip%

答案 3 :(得分:2)

对于Windows 7计算机:

ipconfig | findstr /b /c:"   IPv4" > output.txt

开头引号和IPv4之间有三个空白字符,因为该行在技术上以空格开头。我不知道在findstr命令之前删除它的方法。

请记住,即使它是技术上正则表达式,Windows命令行也不会像C#或其他任何方式那样解析它们。有一个可接受的序列/通配符列表(标记为XP,但它在Win7环境中适用于我)here

我尝试了一些试验和错误,但这只能获得分配的IPv4地址的行,而不是“自动配置”的东西,这会混淆其他findtr迭代的结果。

答案 4 :(得分:0)

只需添加一点以显示网关(您的路由器)并ping并查看您的DNS是否正常工作:

@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

答案 5 :(得分:0)

这是一个可以完成这项工作的vbs脚本。请注意,这是针对外部IP地址的。只需查看上面的内部/本地IP地址。

这是代码。只需创建一个文本文档,粘贴它,然后将其重命名为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

我不赞成这个脚本,我只是在计算机上的一个文件夹中找到了我很长时间没有碰过的文件。

答案 6 :(得分:0)

@echo off
PowerShell.exe -Command "(Invoke-WebRequest ifconfig.me/ip).Content.Trim()" >> ipToFile.txt