Solved

powershell Search IP's

Posted on 2013-06-13
18
204 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
 
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Does Powershell have you tied up in knots?

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

896 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

19 Experts available now in Live!

Get 1:1 Help Now