[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

powershell Search IP's

Posted on 2013-06-13
18
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 4
18 Comments
 
LVL 41

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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 41

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 2000 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 41

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 41

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

650 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