将IP地址写入文件

问题描述 投票:3回答:7

有没有什么简单的命令可以把ip-address写到文件里?

我知道怎么写到文件里,但是有没有sysvar什么的!?

batch-file command cmd ip
7个回答
3
投票

ipconfig | find "IP Address" > out.txt

你还是需要从 "IP地址......:0.0.0.0 "中提取出IP地址,并修剪掉所有的空格。


3
投票

这是我能想到的最简单的方法。

ipconfig > file

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%

2
投票

对于Windows 7机器。

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

在开头的引号和IPv4之间有三个空白字符 因为这一行是以空白字符开始的。 我不知道有什么方法可以在findstr命令之前将其去掉。

请记住,即使它是 技术上 正则表达式,Windows命令行对它们的解析方式与C#之类的不一样。 这里有一个可接受的序列wildcards的列表(标记为XP,但我在Win7环境下也能用) 此处.

我花了一点试验和错误,但这让你只为分配的IPv4地址行,而不是 "自动配置 "的东西,杂乱的其他findstr迭代的结果。


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

0
投票

这里是一个vbs脚本,将做这项工作。 注意,这是针对外部IP地址的。 只要看看上面的internalallocal 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

这个脚本的功劳不在我,我只是在电脑上的一个文件夹里发现了它,我已经很久没有碰过了。


0
投票
@echo off
PowerShell.exe -Command "(Invoke-WebRequest ifconfig.me/ip).Content.Trim()" >> ipToFile.txt
© www.soinside.com 2019 - 2024. All rights reserved.