将 IP 地址写入文件

发布于 2024-10-07 22:32:52 字数 65 浏览 11 评论 0原文

有没有简单的命令可以将ip地址写入文件?

我知道如何写入文件,但是有 sysvar 之类的东西吗!?

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!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

所谓喜欢 2024-10-14 22:32:52

ipconfig | ipconfig | ipconfig找到“IP地址”> out.txt

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

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.

愿得七秒忆 2024-10-14 22:32:52

我能想到的最简单的:

ipconfig > file

Simplest i can think of:

ipconfig > file
倒数 2024-10-14 22:32:52

这是您要找的吗?

@echo on

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

set ip=%ip:~1%

echo %ip%

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%
匿名的好友 2024-10-14 22:32:52

对于 Windows 7 计算机:

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

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

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

我花了一些尝试和错误,但这只为您提供了分配的 IPv4 地址的行,而不是混乱其他 findstr 迭代结果的“自动配置”内容。

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.

夏末的微笑 2024-10-14 22:32:52

这是一个可以完成这项工作的 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

我不将此脚本归功于我,我只是在我的计算机上的一个我很长时间没有碰过的文件夹中找到了它。

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.

如梦 2024-10-14 22:32:52

只需添加一点即可显示网关(您的路由器)并 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

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
眼藏柔 2024-10-14 22:32:52
@echo off
PowerShell.exe -Command "(Invoke-WebRequest ifconfig.me/ip).Content.Trim()" >> ipToFile.txt
@echo off
PowerShell.exe -Command "(Invoke-WebRequest ifconfig.me/ip).Content.Trim()" >> ipToFile.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文