英文:
Extract IP address from .txt file using Powershell and save it into a new text file
问题
I understand that you want to extract the IP address from the text file generated by ngrok and save it to a new text file using PowerShell. To achieve this, you can modify your script to extract only the IP address part. Here's an updated version of your script:
$List_IP = Get-Content -Path "C:\ngrok_log.txt"
$Pattern = '(\d+\.\d+\.\d+\.\d+:\d+)'
$IPs = $List_IP | Select-String -Pattern $Pattern -AllMatches | ForEach-Object { $_.Matches.Value }
if ($IPs.Count -gt 0) {
$Folder = "C:\"
$SaveFile = Join-Path -Path $Folder -ChildPath "final_ip.txt"
$IPs | Out-File -FilePath $SaveFile
Write-Host "SUCCESS - IP addresses saved to $SaveFile"
} else {
Write-Host "ERROR - No IP addresses found in the file."
}
This script does the following:
- Reads the content of the ngrok log file.
- Uses a regular expression pattern to match IP addresses in the format "x.x.x.x:xxxxx" and extracts them.
- If IP addresses are found, it saves them to the "final_ip.txt" file in the "C:" directory.
- If no IP addresses are found, it displays an error message.
Make sure to replace "C:\ngrok_log.txt" with the correct path to your ngrok log file.
英文:
I have a text file and it's contains an IP address what I want to extract and save it to a new text file. The text file that contains the IP address it's an output file from an application called ngrok. That's how the file looks like:
t=2023-07-05T16:17:15+0300 lvl=info msg="no configuration paths supplied"
t=2023-07-05T16:17:15+0300 lvl=info msg="using configuration at default config path" path=C:\\Users\\***\\AppData\\Local/ngrok/ngrok.yml
t=2023-07-05T16:17:15+0300 lvl=info msg="open config file" path=C:\\Users\\***\\AppData\\Local\\ngrok\\ngrok.yml err=nil
t=2023-07-05T16:17:15+0300 lvl=info msg="starting web service" obj=web addr=127.0.0.1:4040 allow_hosts=[]
t=2023-07-05T16:17:15+0300 lvl=info msg="tunnel session started" obj=tunnels.session
t=2023-07-05T16:17:15+0300 lvl=info msg="client session established" obj=csess
t=2023-07-05T16:17:15+0300 lvl=info msg="started tunnel" obj=tunnels name=command_line addr=//localhost:8865 url=tcp://4.tcp.eu.ngrok.io:10355
t=2023-07-05T16:17:16+0300 lvl=info msg="update available" obj=updater
The address that I want to extract is 4.tcp.eu.ngrok.io:10355
Recently I created this powershell script:
$List_IP = Get-ChildItem -Path "C:\ngrok_log.txt"
foreach ($IP in $List_IP)
{
$pattern = '(\d.tcp.eu.ngrok.io\W\d\d\d\d\d)'
$sel = select-string -list $IP -pattern $pattern | Select-Object -ExpandProperty Line
If ($Sel -eq $null)
{
write-host "ERROR - $IP does not contain $pattern"
}
Else
{
write-host "SUCCESS - $pattern `n$sel"
}
Write-host "end"
$Folder = "C:\"
$SaveFile = $Folder + "final_ip.txt"
$sel | Out-File $SaveFile
}
It works, but it's saving the whole line that contains the IP address instead just the IP address:
t=2023-07-05T16:17:15+0300 lvl=info msg="started tunnel" obj=tunnels name=command_line addr=//localhost:8865 url=tcp://4.tcp.eu.ngrok.io:10355
I don't really understand the "pattern" part of this script, everytime I restart the ngrok I get new random IP address like this:
(number between 1-99).tcp.(ap, au, eu, in, jp, sa, us).ngrok.io:(number between 10000-99999)
So.., how can I extract the IP address from that file and save it to a new one?
答案1
得分: 0
你的代码看起来不错,主要问题是当你执行"...$sel"
时,你在强制转换一个 MatchInfo
实例 为字符串,而强制转换的结果是其 .Line
属性,而实际上你要找的是你的模式的匹配值,在$sel | Out-File ...
也会发生同样的情况。
一个简单的例子:
$match = 'foo bar baz' | Select-String bar
$match.Line # foo bar baz
$match.ToString() # foo bar baz
$match.Matches.Value # bar
所以这两行应该更改为:
Write-Host "SUCCESS - $pattern `n$($sel.Matches.Value)"
# 和
$sel.Matches.Value | Out-File ...
这一行似乎也不需要:
$List_IP = Get-ChildItem -Path "C:\ngrok_log.txt"
而且 | Select-Object -ExpandProperty Line
也应该被移除。
至于正则表达式模式,你目前的可能可以正常工作,但如果你想要更精确的匹配,可以尝试这个:
(?<=url=tcp://)[1-9][0-9]?\.tcp\.(?:ap|au|eu|in|jp|sa|us)\.ngrok\.io:[1-9][0-9]{4}\b
详细信息请参见 https://regex101.com/r/abddxD/1。
综上所述,你可以简化你的代码如下:
$pattern = '(?<=url=tcp://)[1-9][0-9]?\.tcp\.(?:ap|au|eu|in|jp|sa|us)\.ngrok\.io:[1-9][0-9]{4}\b'
$match = Select-String -LiteralPath path\to\ngrok_log.txt -Pattern $pattern
if ($match) {
Write-Host "SUCCESS - $pattern `n$($match.Matches.Value)"
$folder = 'C:\'
$saveFile = Join-Path $Folder -ChildPath 'final_ip.txt'
$match.Matches.Value | Out-File $SaveFile
}
else {
Write-Host "ERROR - $IP does not contain $pattern"
}
Write-Host 'end'
英文:
Your code looks good, the main issue is when you do "...$sel"
you're coercing a MatchInfo
instance to be a string and when coerced the result is its .Line
property when what you're actually looking for is the matched value of your pattern, the same would be happening in $sel | Out-File ...
.
A simple example:
$match = 'foo bar baz' | Select-String bar
$match.Line # foo bar baz
$match.ToString() # foo bar baz
$match.Matches.Value # bar
So those 2 lines should be changed to:
Write-Host "SUCCESS - $pattern `n$($sel.Matches.Value)"
# and
$sel.Matches.Value | Out-File ...
This line also seem to be not needed:
$List_IP = Get-ChildItem -Path "C:\ngrok_log.txt"
And | Select-Object -ExpandProperty Line
should definitely be removed.
As for the regex pattern, what you have currently might work fine but if you want more precision you can give this one a try:
(?<=url=tcp://)[1-9][0-9]?\.tcp\.(?:ap|au|eu|in|jp|sa|us)\.ngrok\.io:[1-9][0-9]{4}\b
See https://regex101.com/r/abddxD/1 for details.
Summarizing, you can simplify your code as follows:
$pattern = '(?<=url=tcp://)[1-9][0-9]?\.tcp\.(?:ap|au|eu|in|jp|sa|us)\.ngrok\.io:[1-9][0-9]{4}\b'
$match = Select-String -LiteralPath path\to\ngrok_log.txt -Pattern $pattern
if ($match) {
Write-Host "SUCCESS - $pattern `n$($match.Matches.Value)"
$folder = 'C:\'
$saveFile = Join-Path $Folder -ChildPath 'final_ip.txt'
$match.Matches.Value | Out-File $SaveFile
}
else {
Write-Host "ERROR - $IP does not contain $pattern"
}
Write-Host 'end'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论