{"id":740,"date":"2020-03-15T14:58:48","date_gmt":"2020-03-15T13:58:48","guid":{"rendered":"https:\/\/blog.mhasin.eu\/?p=740"},"modified":"2020-03-15T14:58:48","modified_gmt":"2020-03-15T13:58:48","slug":"bash-vygenerovanie-ip-listu-z-ip-prefixu","status":"publish","type":"post","link":"https:\/\/blog.mhasin.eu\/?p=740","title":{"rendered":"Bash vygenerovanie IP listu z IP prefixu"},"content":{"rendered":"\n<p>Pouzitie:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n    -h Displays this help screen\n\n    -f Forces a check for network boundary when given a STRING(s)\n\n    -i Will read from an Input file (file should contain one CIDR per line) (no network boundary check)\n\n    -b Will do the same as \u2013i but with network boundary check<\/code><\/pre>\n\n\n\n<p>Priklady:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n    .\/cidr-to-ip.sh 192.168.0.1\/24\n\n    .\/cidr-to-ip.sh 192.168.0.1\/24 10.10.0.0\/28\n\n    .\/cidr-to-ip.sh -f 192.168.0.0\/16\n\n    .\/cidr-to-ip.sh -i inputfile.txt\n\n    .\/cidr-to-ip.sh -b inputfile.txt\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash    \n\n############################\n##  Methods\n############################   \nprefix_to_bit_netmask() {\n    prefix=$1;\n    shift=$(( 32 - prefix ));\n\n    bitmask=\"\"\n    for (( i=0; i &lt; 32; i++ )); do\n        num=0\n        if &#91; $i -lt $prefix ]; then\n            num=1\n        fi\n\n        space=\n        if &#91; $(( i % 8 )) -eq 0 ]; then\n            space=\" \";\n        fi\n\n        bitmask=\"${bitmask}${space}${num}\"\n    done\n    echo $bitmask\n}\n\nbit_netmask_to_wildcard_netmask() {\n    bitmask=$1;\n    wildcard_mask=\n    for octet in $bitmask; do\n        wildcard_mask=\"${wildcard_mask} $(( 255 - 2#$octet ))\"\n    done\n    echo $wildcard_mask;\n}\n\ncheck_net_boundary() {\n    net=$1;\n    wildcard_mask=$2;\n    is_correct=1;\n    for (( i = 1; i &lt;= 4; i++ )); do\n        net_octet=$(echo $net | cut -d '.' -f $i)\n        mask_octet=$(echo $wildcard_mask | cut -d ' ' -f $i)\n        if &#91; $mask_octet -gt 0 ]; then\n            if &#91; $(( $net_octet&amp;$mask_octet )) -ne 0 ]; then\n                is_correct=0;\n            fi\n        fi\n    done\n    echo $is_correct;\n}\n\n#######################\n##  MAIN\n#######################\nOPTIND=1;\ngetopts \"fibh\" force;\n\nshift $((OPTIND-1))\nif &#91; $force = 'h' ]; then\n    echo \"\"\n    echo -e \"THIS SCRIPT WILL EXPAND A CIDR ADDRESS.\\n\\nSYNOPSIS\\n  .\/cidr-to-ip.sh &#91;OPTION(only one)] &#91;STRING\/FILENAME]\\nDESCRIPTION\\n -h  Displays this help screen\\n -f  Forces a check for network boundary when given a STRING(s)\\n    -i  Will read from an Input file (no network boundary check)\\n  -b  Will do the same as \u2013i but with network boundary check\\n\\nEXAMPLES\\n    .\/cidr-to-ip.sh  192.168.0.1\/24\\n   .\/cidr-to-ip.sh  192.168.0.1\/24 10.10.0.0\/28\\n  .\/cidr-to-ip.sh  -f 192.168.0.0\/16\\n    .\/cidr-to-ip.sh  -i inputfile.txt\\n .\/cidr-to-ip.sh  -b inputfile.txt\\n\"\n    exit\nfi\n\nif &#91; $force = 'i' ] || &#91; $force = 'b' ]; then\n\n    old_IPS=$IPS\n    IPS=$'\\n'\n    lines=($(cat $1)) # array\n    IPS=$old_IPS\n        else\n            lines=$@\nfi\n\nfor ip in ${lines&#91;@]}; do\n    net=$(echo $ip | cut -d '\/' -f 1);\n    prefix=$(echo $ip | cut -d '\/' -f 2);\n    do_processing=1;\n\n    bit_netmask=$(prefix_to_bit_netmask $prefix);\n\n    wildcard_mask=$(bit_netmask_to_wildcard_netmask \"$bit_netmask\");\n    is_net_boundary=$(check_net_boundary $net \"$wildcard_mask\");\n\n    if &#91; $force = 'f' ] &amp;&amp; &#91; $is_net_boundary -ne 1 ] || &#91; $force = 'b' ] &amp;&amp; &#91; $is_net_boundary -ne 1 ] ; then\n        read -p \"Not a network boundary! Continue anyway (y\/N)? \" -n 1 -r\n        echo    ## move to a new line\n        if &#91;&#91; $REPLY =~ ^&#91;Yy]$ ]]; then\n            do_processing=1;\n        else\n            do_processing=0;\n        fi\n    fi  \n\n    if &#91; $do_processing -eq 1 ]; then\n        str=\n        for (( i = 1; i &lt;= 4; i++ )); do\n            range=$(echo $net | cut -d '.' -f $i)\n            mask_octet=$(echo $wildcard_mask | cut -d ' ' -f $i)\n            if &#91; $mask_octet -gt 0 ]; then\n                range=\"{$range..$(( $range | $mask_octet ))}\";\n            fi\n            str=\"${str} $range\"\n        done\n        ips=$(echo $str | sed \"s, ,\\\\.,g\"); ## replace spaces with periods, a join...\n\n        eval echo $ips | tr ' ' '\\n'\nelse\nexit\n    fi\n\ndone<\/code><\/pre>\n<div class=\"pdf24Plugin-cp\"> \t<form name=\"pdf24Form0\" method=\"post\" action=\"https:\/\/doc2pdf.pdf24.org\/wordpress.php\" target=\"pdf24PopWin\" onsubmit=\"var pdf24Win = window.open('about:blank', 'pdf24PopWin', 'resizable=yes,scrollbars=yes,width=600,height=250,left='+(screen.width\/2-300)+',top='+(screen.height\/3-125)+''); pdf24Win.focus(); if(typeof pdf24OnCreatePDF === 'function'){void(pdf24OnCreatePDF(this,pdf24Win));}\"> \t\t<input type=\"hidden\" name=\"blogCharset\" value=\"Cw1x07UAAA==\" \/><input type=\"hidden\" name=\"blogPosts\" value=\"MwQA\" \/><input type=\"hidden\" name=\"blogUrl\" value=\"yygpKSi20tdPyslP18vNSCzOzNNLLQUA\" \/><input type=\"hidden\" name=\"blogName\" value=\"c\/LxdwcA\" \/><input type=\"hidden\" name=\"blogValueEncoding\" value=\"gzdeflate base64\" \/><input type=\"hidden\" name=\"postId_0\" value=\"MzcxAAA=\" \/><input type=\"hidden\" name=\"postTitle_0\" value=\"c0oszlAoq0xPzUstyi9LzMtMVfAMUMjJLC4pVagCMQuKUtMyK0oB\" \/><input type=\"hidden\" name=\"postLink_0\" value=\"yygpKSi20tdPyslP18vNSCzOzNNLLdW3L7A1NzEAAA==\" \/><input type=\"hidden\" name=\"postAuthor_0\" value=\"y00syfcwNAIA\" \/><input type=\"hidden\" name=\"postDateTime_0\" value=\"MzIwMtA1MNY1NFUwNLEyMLYysQAA\" \/><input type=\"hidden\" name=\"postContent_0\" value=\"pVdtc5tGEP6uX7HBNBYTg4SaZJLKuPFESqsZR9FYnkkyI1eD4GQuxneUw1FUR53+h\/7D\/pLuHW9CgjgvfIDj2N3b231un6V1HJ1M+O1fNKHkl+NOdNLC6ziKCXihK4SjrSJzEXLv2vS4T7STY\/lAIcDLDGBARRS6awFJQAUEJIxAeDEhLJNYwisee0SAC15AvGtY8hgYSVY8voYFv2W+G69hFRAGV\/Qj3l2YXpyPxr+1hZGZoPCWhiHExPVhGfMbcBmMWHSbwJKGBNrqLgJ+G\/rgcZa4lAFnBF6OBucQkRhCyogBbcb3F1Y+5Qst0oV8jptBi+4NAVfAf\/\/8S2GBq61oEjRYOO6oqGD4YpIF8GQS0+vQ9dffGlSr41E\/NhNu0sgSAdjPe5b99JnVtexO7\/HXyICNzy6+dDu9Z\/UKmJdSp9uxnzaIUaAy0jLEVvIpaZBa7EjVhOOerR886Cwo6yxcNIdXq3XwhQs\/ArwmScB98UVBaQlXXtJP84TPFzSZY\/puXHHdNuBObSX96uh2X72KgC4TR2+34ecemNlXMIx+unG0ILUdTVOvEsooSp1uHyg8DJM+quHw0SOpgjhSUvJitzdOt3ijS3h48Nzug07BDBPQs3Uu+xJ4rJDLNe1iZklbxVhErkecGqPSJfgJnqETYJI\/oVtrOFXXQOvXWi+2qt9lw41+p3TwiU5t0hD4eNDUgHgBBz0TbW1ara1oy+CvaOh7buzvZSBfKE9BIahmizhzLyEJwqxYoxLfqhL6XJnYqJj0njzBlPYO9NSUYdTuoKLYlxtRB1y6Pc\/PfOE7Tjb4rffSWSrmHo9j4iVOJpeDBhywc9g48LgeNrio8hYhmbqHM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-to-pdf\/img\/pdf_32x32.png\" alt=\"\" border=\"0\" height=\"32\" \/><\/a> \t\t<span class=\"pdf24Plugin-cp-space\">&nbsp;&nbsp;<\/span> \t\t<span class=\"pdf24Plugin-cp-text\">Send article as PDF<\/span> \t\t<span class=\"pdf24Plugin-cp-space\">&nbsp;&nbsp;<\/span> \t\t<input class=\"pdf24Plugin-cp-input\" style=\"margin: 0px;\" type=\"text\" name=\"sendEmailTo\" placeholder=\"Enter email address\" \/> \t\t<input class=\"pdf24Plugin-cp-submit\" style=\"margin: 0px;\" type=\"submit\" value=\"Send\" \/> \t<\/form> <\/div>","protected":false},"excerpt":{"rendered":"Pouzitie: Priklady: &nbsp;&nbsp; Send article as PDF &nbsp;&nbsp;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"arc_restricted_post":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-740","post","type-post","status-publish","format-standard","hentry","category-programovanie"],"_links":{"self":[{"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=\/wp\/v2\/posts\/740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=740"}],"version-history":[{"count":1,"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=\/wp\/v2\/posts\/740\/revisions"}],"predecessor-version":[{"id":741,"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=\/wp\/v2\/posts\/740\/revisions\/741"}],"wp:attachment":[{"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mhasin.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}