Solved

powershell Search IP's

Posted on 2013-06-13
18
203 Views
Last Modified: 2013-07-17
The code below works fine. I does exactly what I want it to do it searches thru files and sub-folders for the given IP string.

The issue is that i think it is to limited. Is there a way to change this script where it looks for any IP any Range? I can cut out  10.102. to only search that range. I would like it to find any IP and any subnet?

Help!




CLS       
$RootString = "C:\Temp\"
$SearchString = "10.102.72.55"


$FileName = Get-ChildItem $RootString  -recurse | Select-String -pattern $SearchString | group path | select name

$FileName | Out-GridView

$FileName | Out-file -width 300 -filepath "C:\Users\ltorres\Desktop\DataDumps_PS\pshellOutPut_ConfigVar.txt"

Open in new window

0
Comment
Question by:Leo Torres
  • 9
  • 5
  • 4
18 Comments
 
LVL 39

Expert Comment

by:footech
Comment Utility
Change
$SearchString = "10.102.72.55"
to
$SearchString = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Try with
$SearchString = '^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$'

Open in new window

Or try the following for an accurate match..
$SearchString = '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

Open in new window

0
 
LVL 8

Author Comment

by:Leo Torres
Comment Utility
This did not work becasue I have tried Hardcoded IP and search had results.

This returned nothing
$SearchString = '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
                                           
The pattern has to be [1-255] .[0-255].[0-255].[0-255] not sure how that translates in Powershell.
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
This is what I get..
PS C:\> $SearchString = '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
PS C:\> "10.26.35.45" -match $searchstring
True
PS C:\> "1.26.35.45" -match $searchstring
True
PS C:\> "1.26.35.255" -match $searchstring
True
PS C:\> "01.26.35.255" -match $searchstring
True
PS C:\> "999.999.999.999" -match $searchstring
False

Open in new window

Can you give a sample input file?
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
$SearchString = '^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

Open in new window

is the exact match for [1-255] .[0-255].[0-255].[0-255].
0
 
LVL 8

Author Comment

by:Leo Torres
Comment Utility
I dont userstand I ran it again and got nothing back..

i ran this
$SearchString = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

and got back results..
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
What about this..
$SearchString = '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'

Open in new window

0
 
LVL 8

Author Comment

by:Leo Torres
Comment Utility
I also may need to add filer because

This
Microsoft.SqlServer.ScriptTask, Version=9.0.242.0
 
The 9.0.242.0 looks like an IP

and yes now the last one returned the same count of records as
$SearchString = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

But since I dont the IP it found I dont know whats what.

Is there a way also with the path string that gets returned to file return the actual IP it found?
That will help me remove the  9.0.242.0 version
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
If you can give a sample input file? and the result which you are looking then it will be easy for us...
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Author Comment

by:Leo Torres
Comment Utility
sure let me create one
0
 
LVL 8

Author Comment

by:Leo Torres
Comment Utility
I am just searching dtsx packages
0
 
LVL 8

Author Comment

by:Leo Torres
Comment Utility
Here is a sample file.
278-CLS-test.txt
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
I'm not seeing an IP anywhere in that file.  Plenty of version references that look like an IP though.  Would need an examples of lines that actually have an IP to see if there is some portion of the string to help recognize what is and isn't an IP.

I don't know if there is a better way to approach this as it seems to be SQL related, and I know very little about SQL and related PowerShell commands.
0
 
LVL 8

Author Comment

by:Leo Torres
Comment Utility
Thats just it I got a bunch of results not because it had an IP but because it had a version number for SQL. Could you do a case where "SQL" or "Version" not within a distance from Said version Number and bring back those?
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
I believe so.  That's why I asked for some samples of lines with an IP to see the differences.  I want to make sure nothing else is being missed.  Then we should be able to construct the appropriate regex.
0
 
LVL 8

Author Comment

by:Leo Torres
Comment Utility
0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
I get the following match from the file...I think it's all version not IP's.. Do you have a file which contain both IP's and version? Even you can post the sample line's which contain IP's..
2.0.0.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0
9.0.242.0

Open in new window


We are trying to find the pattern’s for IP address in your file, for example the version has following patern's in your file..
\<version>
Version=<version>

Open in new window

0
 
LVL 8

Author Closing Comment

by:Leo Torres
Comment Utility
Sorry Have not had time to create sample
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now