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;








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.










share|improve this question
























  • 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

















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.










share|improve this question
























  • 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













0












0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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? 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
















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












2 Answers
2






active

oldest

votes


















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/'





share|improve this answer























  • thank you for answer, no, I cannot install external tools

    – Vernux
    Mar 26 at 11:01


















0














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).






share|improve this answer



























    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
    );



    );













    draft saved

    draft discarded


















    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









    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/'





    share|improve this answer























    • thank you for answer, no, I cannot install external tools

      – Vernux
      Mar 26 at 11:01















    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/'





    share|improve this answer























    • thank you for answer, no, I cannot install external tools

      – Vernux
      Mar 26 at 11:01













    1












    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/'





    share|improve this answer













    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/'






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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

















    • 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













    0














    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).






    share|improve this answer





























      0














      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).






      share|improve this answer



























        0












        0








        0







        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).






        share|improve this answer















        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).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        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



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현