-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support range, cidrs in grepip #188
Support range, cidrs in grepip #188
Conversation
BE-2716 Introduce flag in grepip to print subnets too
Ref: #185 (comment) Current:
Want:
cc uman |
$ cat list.txt
lorem33.0.112.175/12
100.0.0.0ipsum
8.8.8.8-10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85/28
2001:db8::1-2001:db8::3
::1,::2
10.0.0.0,101.1.1.1
$ ./ipinfo grepip list.txt --only-matching
33.0.112.175
100.0.0.0
8.8.8.8
10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85
2001:db8::1
2001:db8::3
::1
::2
10.0.0.0
101.1.1.1
$ ./ipinfo grepip list.txt --only-matching --include-cidrs
33.0.112.175/12
100.0.0.0
8.8.8.8
10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85/28
2001:db8::1
2001:db8::3
::1
::2
10.0.0.0
101.1.1.1
$ ./ipinfo grepip list.txt --only-matching --include-ranges
33.0.112.175
100.0.0.0
8.8.8.8-10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85
2001:db8::1-2001:db8::3
::1,::2
10.0.0.0,101.1.1.1
$ ./ipinfo grepip list.txt --only-matching --include-ranges --include-cidrs
33.0.112.175/12
100.0.0.0
8.8.8.8-10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85/28
2001:db8::1-2001:db8::3
::1,::2
10.0.0.0,101.1.1.1
$ ./ipinfo grepip list.txt -o --cidrs-only
33.0.112.175/12
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85/28
$ ./ipinfo grepip list.txt -o --ranges-only
8.8.8.8-10.0.0.0
2001:db8::1-2001:db8::3
::1,::2
10.0.0.0,101.1.1.1 |
b67dd12
to
6127d8d
Compare
ipinfo/cmd_grepip.go
Outdated
@@ -13,6 +13,12 @@ var completionsGrepIP = &complete.Command{ | |||
Flags: map[string]complete.Predictor{ | |||
"-o": predict.Nothing, | |||
"--only-matching": predict.Nothing, | |||
"-c": predict.Nothing, | |||
"--include-cidrs": predict.Nothing, | |||
"-r": predict.Nothing, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not use a short flag for this - we tried to copy some common flag naming conventions from normal grep
(hence -o
), and -r
is too easily associated with recursive lookup
.
ipinfo/cmd_grepip.go
Outdated
@@ -13,6 +13,12 @@ var completionsGrepIP = &complete.Command{ | |||
Flags: map[string]complete.Predictor{ | |||
"-o": predict.Nothing, | |||
"--only-matching": predict.Nothing, | |||
"-c": predict.Nothing, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, this is too easily associated with count
in grep
ipinfo/cmd_grepip.go
Outdated
@@ -35,6 +41,14 @@ Options: | |||
General: | |||
--only-matching, -o | |||
print only matched IP in result line, excluding surrounding content. | |||
--include-cidrs, -c | |||
prints the CIDRs too. | |||
--include-ranges, -r |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rm -r
ipinfo/cmd_grepip.go
Outdated
@@ -35,6 +41,14 @@ Options: | |||
General: | |||
--only-matching, -o | |||
print only matched IP in result line, excluding surrounding content. | |||
--include-cidrs, -c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rm -c
lib/cmd_grepip.go
Outdated
@@ -37,6 +41,26 @@ func (f *CmdGrepIPFlags) Init() { | |||
"only-matching", "o", false, | |||
"print only matched IPs in result line.", | |||
) | |||
pflag.BoolVarP( | |||
&f.IncludeCIDRs, | |||
"include-cidrs", "c", false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rm short
ipV4Regex = regexp.MustCompilePOSIX(IPv4RegexPattern) | ||
ipV6Regex = regexp.MustCompilePOSIX(IPv6RegexPattern) | ||
ipRegex = regexp.MustCompilePOSIX(IPv4RegexPattern + "|" + IPv6RegexPattern) | ||
|
||
v4IpCidrRegex = regexp.MustCompilePOSIX(IPv4RegexPattern + "|" + IPv4CIDRRegexPattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment against each grouped code is warranted now
lib/cmd_grepip.go
Outdated
@@ -121,10 +145,46 @@ func CmdGrepIP( | |||
|
|||
// prepare regexp | |||
var rexp *regexp.Regexp | |||
if ipv == 4 { | |||
if ipv == 4 && f.CIDRsOnly && f.RangesOnly { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we find a grouping that makes this more readable? If nesting if
s helps then let's try that. Needs a judgement
lib/regex.go
Outdated
var v6SubnetRegex *regexp.Regexp | ||
var subnetRegex *regexp.Regexp | ||
|
||
const octet = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a v4Octet
lib/regex.go
Outdated
const IPv6RegexPattern = `(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))` | ||
const IPv6RangeRegexPattern = IPv6RegexPattern + `[:space:]*[-,][:space:]*` + IPv6RegexPattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the infinitely allowed spaces. Our common definition of ranges has always been without any space separation allowed.
lib/regex.go
Outdated
|
||
const octet = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)` | ||
const IPv4RegexPattern = `(` + octet + `\.){3}` + octet | ||
const IPv4RangeRegexPattern = IPv4RegexPattern + `[:space:]*[-,][:space:]*` + IPv4RegexPattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
Ref: #185 (comment)