Solved

powershell Search IP's

Posted on 2013-06-13
18
205 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
ID: 39245547
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
ID: 39245575
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
ID: 39245685
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 40

Expert Comment

by:Subsun
ID: 39245713
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
ID: 39245721
$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
ID: 39245738
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
ID: 39245750
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
ID: 39245786
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
ID: 39245803
If you can give a sample input file? and the result which you are looking then it will be easy for us...
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 39245922
sure let me create one
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 39245924
I am just searching dtsx packages
0
 
LVL 8

Author Comment

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

Expert Comment

by:footech
ID: 39260175
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
ID: 39260539
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
ID: 39260679
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
ID: 39262618
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39263176
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
ID: 39334087
Sorry Have not had time to create sample
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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 …

803 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