从.txt文件中提取IP地址,使用Powershell并将其保存到新的文本文件中。

huangapple go评论210阅读模式
英文:

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:

  1. $List_IP = Get-Content -Path "C:\ngrok_log.txt"
  2. $Pattern = '(\d+\.\d+\.\d+\.\d+:\d+)'
  3. $IPs = $List_IP | Select-String -Pattern $Pattern -AllMatches | ForEach-Object { $_.Matches.Value }
  4. if ($IPs.Count -gt 0) {
  5. $Folder = "C:\"
  6. $SaveFile = Join-Path -Path $Folder -ChildPath "final_ip.txt"
  7. $IPs | Out-File -FilePath $SaveFile
  8. Write-Host "SUCCESS - IP addresses saved to $SaveFile"
  9. } else {
  10. Write-Host "ERROR - No IP addresses found in the file."
  11. }

This script does the following:

  1. Reads the content of the ngrok log file.
  2. Uses a regular expression pattern to match IP addresses in the format "x.x.x.x:xxxxx" and extracts them.
  3. If IP addresses are found, it saves them to the "final_ip.txt" file in the "C:" directory.
  4. 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:

  1. t=2023-07-05T16:17:15+0300 lvl=info msg="no configuration paths supplied"
  2. t=2023-07-05T16:17:15+0300 lvl=info msg="using configuration at default config path" path=C:\\Users\\***\\AppData\\Local/ngrok/ngrok.yml
  3. t=2023-07-05T16:17:15+0300 lvl=info msg="open config file" path=C:\\Users\\***\\AppData\\Local\\ngrok\\ngrok.yml err=nil
  4. t=2023-07-05T16:17:15+0300 lvl=info msg="starting web service" obj=web addr=127.0.0.1:4040 allow_hosts=[]
  5. t=2023-07-05T16:17:15+0300 lvl=info msg="tunnel session started" obj=tunnels.session
  6. t=2023-07-05T16:17:15+0300 lvl=info msg="client session established" obj=csess
  7. 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
  8. 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:

  1. $List_IP = Get-ChildItem -Path "C:\ngrok_log.txt"
  2. foreach ($IP in $List_IP)
  3. {
  4. $pattern = '(\d.tcp.eu.ngrok.io\W\d\d\d\d\d)'
  5. $sel = select-string -list $IP -pattern $pattern | Select-Object -ExpandProperty Line
  6. If ($Sel -eq $null)
  7. {
  8. write-host "ERROR - $IP does not contain $pattern"
  9. }
  10. Else
  11. {
  12. write-host "SUCCESS - $pattern `n$sel"
  13. }
  14. Write-host "end"
  15. $Folder = "C:\"
  16. $SaveFile = $Folder + "final_ip.txt"
  17. $sel | Out-File $SaveFile
  18. }

It works, but it's saving the whole line that contains the IP address instead just the IP address:

  1. 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 ...也会发生同样的情况。

一个简单的例子:

  1. $match = 'foo bar baz' | Select-String bar
  2. $match.Line # foo bar baz
  3. $match.ToString() # foo bar baz
  4. $match.Matches.Value # bar

所以这两行应该更改为:

  1. Write-Host "SUCCESS - $pattern `n$($sel.Matches.Value)"
  2. # 和
  3. $sel.Matches.Value | Out-File ...

这一行似乎也不需要:

  1. $List_IP = Get-ChildItem -Path "C:\ngrok_log.txt"

而且 | Select-Object -ExpandProperty Line 也应该被移除。

至于正则表达式模式,你目前的可能可以正常工作,但如果你想要更精确的匹配,可以尝试这个:

  1. (?<=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

综上所述,你可以简化你的代码如下:

  1. $pattern = '(?<=url=tcp://)[1-9][0-9]?\.tcp\.(?:ap|au|eu|in|jp|sa|us)\.ngrok\.io:[1-9][0-9]{4}\b'
  2. $match = Select-String -LiteralPath path\to\ngrok_log.txt -Pattern $pattern
  3. if ($match) {
  4. Write-Host "SUCCESS - $pattern `n$($match.Matches.Value)"
  5. $folder = 'C:\'
  6. $saveFile = Join-Path $Folder -ChildPath 'final_ip.txt'
  7. $match.Matches.Value | Out-File $SaveFile
  8. }
  9. else {
  10. Write-Host "ERROR - $IP does not contain $pattern"
  11. }
  12. Write-Host 'end'
英文:

Your code looks good, the main issue is when you do &quot;...$sel&quot; 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:

  1. $match = &#39;foo bar baz&#39; | Select-String bar
  2. $match.Line # foo bar baz
  3. $match.ToString() # foo bar baz
  4. $match.Matches.Value # bar

So those 2 lines should be changed to:

  1. Write-Host &quot;SUCCESS - $pattern `n$($sel.Matches.Value)&quot;
  2. # and
  3. $sel.Matches.Value | Out-File ...

This line also seem to be not needed:

  1. $List_IP = Get-ChildItem -Path &quot;C:\ngrok_log.txt&quot;

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:

  1. (?&lt;=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:

  1. $pattern = &#39;(?&lt;=url=tcp://)[1-9][0-9]?\.tcp\.(?:ap|au|eu|in|jp|sa|us)\.ngrok\.io:[1-9][0-9]{4}\b&#39;
  2. $match = Select-String -LiteralPath path\to\ngrok_log.txt -Pattern $pattern
  3. if ($match) {
  4. Write-Host &quot;SUCCESS - $pattern `n$($match.Matches.Value)&quot;
  5. $folder = &#39;C:\&#39;
  6. $saveFile = Join-Path $Folder -ChildPath &#39;final_ip.txt&#39;
  7. $match.Matches.Value | Out-File $SaveFile
  8. }
  9. else {
  10. Write-Host &quot;ERROR - $IP does not contain $pattern&quot;
  11. }
  12. Write-Host &#39;end&#39;

huangapple
  • 本文由 发表于 2023年7月11日 01:31:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定