Bash shell to make RANGE from list of IP addressesCalling shell commands from RubyGet the source directory of a Bash script from within the script itselfHow do I iterate over a range of numbers defined by variables in Bash?Shell command to sum integers, one per line?How to check if a program exists from a Bash script?Assigning default values to shell variables with a single command in bashAutomatic exit from bash shell script on errorCheck existence of input argument in a Bash shell scriptMake a Bash alias that takes a parameter?How to call shell script from another shell script?
How to not confuse readers with simultaneous events?
manipulate a list: replace random position of a specific integer n times by 0
In this iconic lunar orbit rendezvous photo of John Houbolt, why do arrows #5 and #6 point the "wrong" way?
Did Hitler say this quote about homeschooling?
Is straight-up writing someone's opinions telling?
Compiler only complains about the ambiguous overloaded functions when the parameter is 0
How to remove the first colon ':' from a timestamp?
Is encryption still applied if you ignore the SSL certificate warning for self signed?
Why won't some unicode characters print to my terminal?
Strategy to pay off revolving debt while building reserve savings fund?
How can one convert an expression to a string while keeping the quotation marks of strings that are part of the expression?
Demographic consequences of closed loop reincarnation
What were the problems on the Apollo 11 lunar module?
Why did my "seldom" get corrected?
Can Error correction and detection be done with out adding extra bits?
What causes a rotating object to rotate forever without external force—inertia, or something else?
Should I have shared a document with a former employee?
Company looks for long-term employees, but I know I won't be interested in staying long
Practical example in using (homotopy) type theory
Is surviving this (blood loss) scenario possible?
Is it possible to have a career in SciComp without contributing to arms research?
Why are there few or no black super GMs?
How fast does a character need to move to be effectively invisible?
Can firbolgs cast their racial Detect Magic spell as a ritual?
Bash shell to make RANGE from list of IP addresses
Calling shell commands from RubyGet the source directory of a Bash script from within the script itselfHow do I iterate over a range of numbers defined by variables in Bash?Shell command to sum integers, one per line?How to check if a program exists from a Bash script?Assigning default values to shell variables with a single command in bashAutomatic exit from bash shell script on errorCheck existence of input argument in a Bash shell scriptMake a Bash alias that takes a parameter?How to call shell script from another shell script?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I need bash
code to generate a RANGE of given IP addresses.
For example if I have the following simple list:
1.1.1.1
1.1.1.2
1.1.1.3
1.1.1.5
1.1.2.1
1.2.3.4
100.100.1.2
100.100.1.3
My output will be:
(1.1.1.1-1.1.1.3)
1.1.1.5
1.1.2.1
1.2.3.4
(100.100.1.2-100.100.1.3)
...and so on.
ONLY nearest IPS will included in RANGE.
bash shell ip range
add a comment |
I need bash
code to generate a RANGE of given IP addresses.
For example if I have the following simple list:
1.1.1.1
1.1.1.2
1.1.1.3
1.1.1.5
1.1.2.1
1.2.3.4
100.100.1.2
100.100.1.3
My output will be:
(1.1.1.1-1.1.1.3)
1.1.1.5
1.1.2.1
1.2.3.4
(100.100.1.2-100.100.1.3)
...and so on.
ONLY nearest IPS will included in RANGE.
bash shell ip range
If you'd like it for bash, you should probably tag it. Also, why makefile? Usein the editor to markup data - and the code you've tried so far. Thanks.
– James Brown
Mar 26 at 10:06
add a comment |
I need bash
code to generate a RANGE of given IP addresses.
For example if I have the following simple list:
1.1.1.1
1.1.1.2
1.1.1.3
1.1.1.5
1.1.2.1
1.2.3.4
100.100.1.2
100.100.1.3
My output will be:
(1.1.1.1-1.1.1.3)
1.1.1.5
1.1.2.1
1.2.3.4
(100.100.1.2-100.100.1.3)
...and so on.
ONLY nearest IPS will included in RANGE.
bash shell ip range
I need bash
code to generate a RANGE of given IP addresses.
For example if I have the following simple list:
1.1.1.1
1.1.1.2
1.1.1.3
1.1.1.5
1.1.2.1
1.2.3.4
100.100.1.2
100.100.1.3
My output will be:
(1.1.1.1-1.1.1.3)
1.1.1.5
1.1.2.1
1.2.3.4
(100.100.1.2-100.100.1.3)
...and so on.
ONLY nearest IPS will included in RANGE.
bash shell ip range
bash shell ip range
edited Mar 26 at 12:23
agc
5,24316 silver badges39 bronze badges
5,24316 silver badges39 bronze badges
asked Mar 26 at 9:59
VernuxVernux
11 bronze badge
11 bronze badge
If you'd like it for bash, you should probably tag it. Also, why makefile? Usein the editor to markup data - and the code you've tried so far. Thanks.
– James Brown
Mar 26 at 10:06
add a comment |
If you'd like it for bash, you should probably tag it. Also, why makefile? Usein the editor to markup data - and the code you've tried so far. Thanks.
– James Brown
Mar 26 at 10:06
If you'd like it for bash, you should probably tag it. Also, why makefile? Use
in the editor to markup data - and the code you've tried so far. Thanks.– James Brown
Mar 26 at 10:06
If you'd like it for bash, you should probably tag it. Also, why makefile? Use
in the editor to markup data - and the code you've tried so far. Thanks.– James Brown
Mar 26 at 10:06
add a comment |
2 Answers
2
active
oldest
votes
While it is certainly possible to write this in bash, covering all the corner cases is quite hard. Can you install a special-purpose tool for this task, like iprange
?
iprange -j
output is very close to what you need, and you can eliminate the singleton ranges like this:
iprange -j | sed 's/^(.*)-1$/1/'
thank you for answer, no, I cannot install external tools
– Vernux
Mar 26 at 11:01
add a comment |
This is a solution inside GNUmake, using gmtt, a library for general purpose programming in GNUmake.
include gmtt-master/gmtt-master/gmtt.mk
ip-inc = $(strip $(call -ip-inc,$(word 1,$(subst ., ,$1)),$(word 2,$(subst ., ,$1)),$(word 3,$(subst ., ,$1)),$(word 4,$(subst ., ,$1))))
-ip-inc = $(if $(findstring :$4:,:255:),$(if $(findstring :$3:,:255:),$(if $(findstring :$2:,:255:),$(if $(findstring :$1:,:255:),0.0.0.0,$(call decimal-inc,$1).0.0.1),$1.$(call decimal-inc,$2).0.1),$1.$2.$(call decimal-inc,$3).1),$1.$2.$3.$(call decimal-inc,$4))
until-neq = $(if $(call str-eq,$(word 2,$1),$(call ip-inc,$2)),$(call until-neq,$(wordlist 2,2147483647,$1),$(call ip-inc,$2)),$2$(rparen) $(call gen-ip-range,$(wordlist 2,2147483647,$1)))
gen-ip-range = $(if $1,$(if $(call str-eq,$(call ip-inc,$(firstword $1)),$(word 2,$1)),$(lparen)$(firstword $1)-$(call until-neq,$1,$(firstword $1)),$(firstword $1) $(call gen-ip-range,$(wordlist 2,2147483647,$1))))
IP_LIST = 1.1.1.1 1.1.1.2 1.1.1.3 1.1.1.5 1.1.1.255 1.1.2.1 1.2.3.4 1.255.255.255 100.100.1.2 100.100.1.3
IP_RANGES := $(call gen-ip-range,$(IP_LIST))
$(info $(IP_RANGES))
Effectively it is a functional program which converts a sorted list of IP addresses to your range format. Funny IP addresses with leading 0's or unsorted lists are not handled in this simple example (but could be with gmtt onboard functions).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55354275%2fbash-shell-to-make-range-from-list-of-ip-addresses%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
While it is certainly possible to write this in bash, covering all the corner cases is quite hard. Can you install a special-purpose tool for this task, like iprange
?
iprange -j
output is very close to what you need, and you can eliminate the singleton ranges like this:
iprange -j | sed 's/^(.*)-1$/1/'
thank you for answer, no, I cannot install external tools
– Vernux
Mar 26 at 11:01
add a comment |
While it is certainly possible to write this in bash, covering all the corner cases is quite hard. Can you install a special-purpose tool for this task, like iprange
?
iprange -j
output is very close to what you need, and you can eliminate the singleton ranges like this:
iprange -j | sed 's/^(.*)-1$/1/'
thank you for answer, no, I cannot install external tools
– Vernux
Mar 26 at 11:01
add a comment |
While it is certainly possible to write this in bash, covering all the corner cases is quite hard. Can you install a special-purpose tool for this task, like iprange
?
iprange -j
output is very close to what you need, and you can eliminate the singleton ranges like this:
iprange -j | sed 's/^(.*)-1$/1/'
While it is certainly possible to write this in bash, covering all the corner cases is quite hard. Can you install a special-purpose tool for this task, like iprange
?
iprange -j
output is very close to what you need, and you can eliminate the singleton ranges like this:
iprange -j | sed 's/^(.*)-1$/1/'
answered Mar 26 at 10:06
Florian WeimerFlorian Weimer
19.2k3 gold badges14 silver badges49 bronze badges
19.2k3 gold badges14 silver badges49 bronze badges
thank you for answer, no, I cannot install external tools
– Vernux
Mar 26 at 11:01
add a comment |
thank you for answer, no, I cannot install external tools
– Vernux
Mar 26 at 11:01
thank you for answer, no, I cannot install external tools
– Vernux
Mar 26 at 11:01
thank you for answer, no, I cannot install external tools
– Vernux
Mar 26 at 11:01
add a comment |
This is a solution inside GNUmake, using gmtt, a library for general purpose programming in GNUmake.
include gmtt-master/gmtt-master/gmtt.mk
ip-inc = $(strip $(call -ip-inc,$(word 1,$(subst ., ,$1)),$(word 2,$(subst ., ,$1)),$(word 3,$(subst ., ,$1)),$(word 4,$(subst ., ,$1))))
-ip-inc = $(if $(findstring :$4:,:255:),$(if $(findstring :$3:,:255:),$(if $(findstring :$2:,:255:),$(if $(findstring :$1:,:255:),0.0.0.0,$(call decimal-inc,$1).0.0.1),$1.$(call decimal-inc,$2).0.1),$1.$2.$(call decimal-inc,$3).1),$1.$2.$3.$(call decimal-inc,$4))
until-neq = $(if $(call str-eq,$(word 2,$1),$(call ip-inc,$2)),$(call until-neq,$(wordlist 2,2147483647,$1),$(call ip-inc,$2)),$2$(rparen) $(call gen-ip-range,$(wordlist 2,2147483647,$1)))
gen-ip-range = $(if $1,$(if $(call str-eq,$(call ip-inc,$(firstword $1)),$(word 2,$1)),$(lparen)$(firstword $1)-$(call until-neq,$1,$(firstword $1)),$(firstword $1) $(call gen-ip-range,$(wordlist 2,2147483647,$1))))
IP_LIST = 1.1.1.1 1.1.1.2 1.1.1.3 1.1.1.5 1.1.1.255 1.1.2.1 1.2.3.4 1.255.255.255 100.100.1.2 100.100.1.3
IP_RANGES := $(call gen-ip-range,$(IP_LIST))
$(info $(IP_RANGES))
Effectively it is a functional program which converts a sorted list of IP addresses to your range format. Funny IP addresses with leading 0's or unsorted lists are not handled in this simple example (but could be with gmtt onboard functions).
add a comment |
This is a solution inside GNUmake, using gmtt, a library for general purpose programming in GNUmake.
include gmtt-master/gmtt-master/gmtt.mk
ip-inc = $(strip $(call -ip-inc,$(word 1,$(subst ., ,$1)),$(word 2,$(subst ., ,$1)),$(word 3,$(subst ., ,$1)),$(word 4,$(subst ., ,$1))))
-ip-inc = $(if $(findstring :$4:,:255:),$(if $(findstring :$3:,:255:),$(if $(findstring :$2:,:255:),$(if $(findstring :$1:,:255:),0.0.0.0,$(call decimal-inc,$1).0.0.1),$1.$(call decimal-inc,$2).0.1),$1.$2.$(call decimal-inc,$3).1),$1.$2.$3.$(call decimal-inc,$4))
until-neq = $(if $(call str-eq,$(word 2,$1),$(call ip-inc,$2)),$(call until-neq,$(wordlist 2,2147483647,$1),$(call ip-inc,$2)),$2$(rparen) $(call gen-ip-range,$(wordlist 2,2147483647,$1)))
gen-ip-range = $(if $1,$(if $(call str-eq,$(call ip-inc,$(firstword $1)),$(word 2,$1)),$(lparen)$(firstword $1)-$(call until-neq,$1,$(firstword $1)),$(firstword $1) $(call gen-ip-range,$(wordlist 2,2147483647,$1))))
IP_LIST = 1.1.1.1 1.1.1.2 1.1.1.3 1.1.1.5 1.1.1.255 1.1.2.1 1.2.3.4 1.255.255.255 100.100.1.2 100.100.1.3
IP_RANGES := $(call gen-ip-range,$(IP_LIST))
$(info $(IP_RANGES))
Effectively it is a functional program which converts a sorted list of IP addresses to your range format. Funny IP addresses with leading 0's or unsorted lists are not handled in this simple example (but could be with gmtt onboard functions).
add a comment |
This is a solution inside GNUmake, using gmtt, a library for general purpose programming in GNUmake.
include gmtt-master/gmtt-master/gmtt.mk
ip-inc = $(strip $(call -ip-inc,$(word 1,$(subst ., ,$1)),$(word 2,$(subst ., ,$1)),$(word 3,$(subst ., ,$1)),$(word 4,$(subst ., ,$1))))
-ip-inc = $(if $(findstring :$4:,:255:),$(if $(findstring :$3:,:255:),$(if $(findstring :$2:,:255:),$(if $(findstring :$1:,:255:),0.0.0.0,$(call decimal-inc,$1).0.0.1),$1.$(call decimal-inc,$2).0.1),$1.$2.$(call decimal-inc,$3).1),$1.$2.$3.$(call decimal-inc,$4))
until-neq = $(if $(call str-eq,$(word 2,$1),$(call ip-inc,$2)),$(call until-neq,$(wordlist 2,2147483647,$1),$(call ip-inc,$2)),$2$(rparen) $(call gen-ip-range,$(wordlist 2,2147483647,$1)))
gen-ip-range = $(if $1,$(if $(call str-eq,$(call ip-inc,$(firstword $1)),$(word 2,$1)),$(lparen)$(firstword $1)-$(call until-neq,$1,$(firstword $1)),$(firstword $1) $(call gen-ip-range,$(wordlist 2,2147483647,$1))))
IP_LIST = 1.1.1.1 1.1.1.2 1.1.1.3 1.1.1.5 1.1.1.255 1.1.2.1 1.2.3.4 1.255.255.255 100.100.1.2 100.100.1.3
IP_RANGES := $(call gen-ip-range,$(IP_LIST))
$(info $(IP_RANGES))
Effectively it is a functional program which converts a sorted list of IP addresses to your range format. Funny IP addresses with leading 0's or unsorted lists are not handled in this simple example (but could be with gmtt onboard functions).
This is a solution inside GNUmake, using gmtt, a library for general purpose programming in GNUmake.
include gmtt-master/gmtt-master/gmtt.mk
ip-inc = $(strip $(call -ip-inc,$(word 1,$(subst ., ,$1)),$(word 2,$(subst ., ,$1)),$(word 3,$(subst ., ,$1)),$(word 4,$(subst ., ,$1))))
-ip-inc = $(if $(findstring :$4:,:255:),$(if $(findstring :$3:,:255:),$(if $(findstring :$2:,:255:),$(if $(findstring :$1:,:255:),0.0.0.0,$(call decimal-inc,$1).0.0.1),$1.$(call decimal-inc,$2).0.1),$1.$2.$(call decimal-inc,$3).1),$1.$2.$3.$(call decimal-inc,$4))
until-neq = $(if $(call str-eq,$(word 2,$1),$(call ip-inc,$2)),$(call until-neq,$(wordlist 2,2147483647,$1),$(call ip-inc,$2)),$2$(rparen) $(call gen-ip-range,$(wordlist 2,2147483647,$1)))
gen-ip-range = $(if $1,$(if $(call str-eq,$(call ip-inc,$(firstword $1)),$(word 2,$1)),$(lparen)$(firstword $1)-$(call until-neq,$1,$(firstword $1)),$(firstword $1) $(call gen-ip-range,$(wordlist 2,2147483647,$1))))
IP_LIST = 1.1.1.1 1.1.1.2 1.1.1.3 1.1.1.5 1.1.1.255 1.1.2.1 1.2.3.4 1.255.255.255 100.100.1.2 100.100.1.3
IP_RANGES := $(call gen-ip-range,$(IP_LIST))
$(info $(IP_RANGES))
Effectively it is a functional program which converts a sorted list of IP addresses to your range format. Funny IP addresses with leading 0's or unsorted lists are not handled in this simple example (but could be with gmtt onboard functions).
edited Mar 26 at 14:03
answered Mar 26 at 13:03
VroomfondelVroomfondel
1,2848 silver badges21 bronze badges
1,2848 silver badges21 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55354275%2fbash-shell-to-make-range-from-list-of-ip-addresses%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you'd like it for bash, you should probably tag it. Also, why makefile? Use
in the editor to markup data - and the code you've tried so far. Thanks.
– James Brown
Mar 26 at 10:06