2010-12-15 72 views
3

是否有任何簡單的命令將ip地址寫入文件?將IP地址寫入文件

我知道如何寫在一個文件,但有沒有一個sysvar或什麼!?

+3

當你說* ip地址*時,我認爲你做出了一個危險的假設。許多計算機具有多個IP地址,因爲它們具有多個網絡適配器,所以請確保您所做的任何操作都支持多個IP。 – 2010-12-15 15:26:41

+0

萬一你需要看到你的外部IP地址(你的型號/路由器分配的),而不是你本地局域網的IP地址,請看這裏http://stackoverflow.com/questions/41669330/windows-batch-how-to-get -the-external-ip-into-a-batch-file-variable – 2017-04-29 04:00:57

回答

3

ipconfig | find "IP Address" > out.txt

您仍然需要從「IP地址.............提取IP地址:0.0。 0.0「並修剪任何空格。

+3

請記住,可能有多個IP地址,如果您有多個網絡適配器。 – Vicky 2010-12-15 15:26:22

3

最簡單的我能想到的:

ipconfig > file 
+0

是的,但是你知道如何獲得IP地址嗎? * hm * – 2010-12-15 15:18:09

+1

@fuzi:你可以像'ipconfig |找到「IPv4」>「文件」,然後「for/f」標記= 2 delims =:「%i in(file)do echo%i >> ip.txt' – 2010-12-15 15:44:04

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% 
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 
+1

哪個IP地址?哪個網關? – 2011-10-13 08:45:04

2

對於Windows 7的機器:

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

有是開頭引號和IPv4之間的三個空格字符,因爲該行技術上以空格開頭。我不知道在findtr命令之前剝離它的方法。

請記住,即使它的技術上是正則表達式,Windows命令行不會像C#或其他任何方式解析它們。有一個可接受的序列/通配符列表(標記爲XP,但它在Win7環境中適用於我)here

花了我一些試驗和錯誤,但這隻讓你分配IPv4地址的行,而不是「自動配置」的東西,混亂其他findtr迭代的結果。

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 

我不邀功這個劇本,我只是覺得,我沒有在摸我的計算機上的文件夾中長時間。