Solved

powershell Search IP's

Posted on 2013-06-13
18
207 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 40

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 40

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 40

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 40

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

713 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