{"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 class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">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 < 32; i++ )); do\n        num=0\n        if [ $i -lt $prefix ]; then\n            num=1\n        fi\n\n        space=\n        if [ $(( 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 <= 4; i++ )); do\n        net_octet=$(echo $net | cut -d '.' -f $i)\n        mask_octet=$(echo $wildcard_mask | cut -d ' ' -f $i)\n        if [ $mask_octet -gt 0 ]; then\n            if [ $(( $net_octet&$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 [ $force = 'h' ]; then\n    echo \"\"\n    echo -e \"THIS SCRIPT WILL EXPAND A CIDR ADDRESS.\\n\\nSYNOPSIS\\n  .\/cidr-to-ip.sh [OPTION(only one)] [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 [ $force = 'i' ] || [ $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[@]}; 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 [ $force = 'f' ] && [ $is_net_boundary -ne 1 ] || [ $force = 'b' ] && [ $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 [[ $REPLY =~ ^[Yy]$ ]]; then\n            do_processing=1;\n        else\n            do_processing=0;\n        fi\n    fi  \n\n    if [ $do_processing -eq 1 ]; then\n        str=\n        for (( i = 1; i <= 4; i++ )); do\n            range=$(echo $net | cut -d '.' -f $i)\n            mask_octet=$(echo $wildcard_mask | cut -d ' ' -f $i)\n            if [ $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=\"pVdtb9s2EP7uX3FVtMZCI9ny2qKd465B7W4GEteIA7QFnBmyRFtsFFITlbpe4mH\/Yf9wv2RH6s2ypaQv+iBR1N3xePfwnlPjOAQ3cIToaavQnAfcvTJDJ3KWkRP62qsxv\/mLxpT8ctwKXzXwOg4jsqfgco9or47lA4UAL9OHPhVh4KwFxD4V4JMgBOFGhLBUYgFveeQSAQ64PnGvYMEjYCRe8egK5vyGeU60hpVPGCzpZ7w7MLk4H45+awojNUHhPQ0CiIjjwSLi1+AwGLLwJoYFDQg01V34\/CbwwOUsdigDzgi8GfbPISQRBJQRA5qM7y+sfMoWmicLeRw3gxadawKOgP\/++ZfCHFdb0divsXDcUlHB8EUkDeC98Y7oVeB462+Nt9VyqReZMTdpaAkf7Jcdy37+wmpbdqvz9GtkwMZnG1\/arc6LagVMWaHTbtnPa8QoUJkEGX0r\/hLXSM13pCoi9cDWDx615pS15g6aw6vROLjnwo8AZyT2uSfuFZSWcOUF\/TKL+WxO4xlm9toRV00DbtVWkq893e6qV+HTRdzTm034uQNm+hUMo5tsHC1I7Z6mqVeJchSlvXYXKDwO4i6q4fDJE6mCEFNS8mI31712\/kYX8Pjgpd0FnYIZxKCn61x2JSZZLpdp2vnMgjbysQgdl\/QqjEqX4Cd4gU6ASf6EdqXhRF0DrVtpPd+qfpsON\/qt0sEnOrVJQuDhGVQD4voc9FS0sWk0tqItg7+igec6kbeXgWyhLAW5oJrN48zdmMQIs3yNUnzLSuhzaWKjYtJ59gxT2jnQE1OGUbmDkmJXbkSdfen2LCsHue84WeO33klmqZi5PIqIG\/dSuQw00AM7g00PnlbDBhdV3iIkE\/dwBu7AxUJlenBoHcqDrFMj11DhLquUHNtShj3lAkKFGTCXcQ2CSojTc18fO9dhd9uChCFW6mojO0Fqd0tfEZE7w510FaoqV\/cWjJPhqFbg3fhiOOrLHC1JzMNYgLagc1+T6XIJHn9VGeROE0nTNoxGEQAlhRk99A9Lu1ROptVCjU0C2sXvwwlM3pwPxxfwfnh6CoMP45NRH04SMjvp988Hk4k1ZVM2+Th6N54MJ1O2X3XV0tKbd6MmZ8Fa0qFxmUwn7Np6OzwdjE7OBpdT1h8kK6I0GkNOryd1+X0BP0Dp0ltF6Q9wej1XSwvIKt9N1TJ2gw8nZ+PTwSTxZjd6O5SphB6QKdFqVUb2WbVm7V1aRbF9mfmuTAqjLzRuyEpdgT6K6IO7u735eYFKZYMH3mw4nvR0vKkJ9XI4ZYfqTbZSotfUm66DkEekwwE4UeSsC9nUQn46SSBI6eQmNvTXylWJHRqq6n2rPigHX19u8mrHtgoWShZVqqWqlG10S3RdL9lJJT0+CyOOABaULeW5zsgmIyA0UtkaZGyccf5OXW8+wGug6VsSmpHTwDaBoJl9VkmKu1au11rmRkW2FzLbqtqqW95TlNdStdeux8U3WNgp3+pcmyFoIx7j8d89iY\/gDfbplN3gqWXrlbOG5ro1Mn4FDQ2iPTMq0COziReW6Wv+mUDMlb2VQtEePaVeng\/Gpx+h9zf8oSY+ri91uKyimD0o1IK2LNnu7pLPgsrWtJyOko7qt+w9ohNxVDRq39IAqDA7bEm+ugH44SbgOxqBwkvtVlcDy1I9gRrjauVuYKPVknwWLOzg8LFJLWiNIkHbeAhFvj+UxnUE8UATR3A0nVpHSzw7ElERQZpDvKvmVSSsgT+MFH8ejhBmnzhllmUVnS\/57ARZdxEKtIqmZZxUeVSQUSU4a5ilS+X\/nf8B\" \/> \t\t<a href=\"https:\/\/www.pdf24.org\" target=\"_blank\" title=\"www.pdf24.org\" rel=\"nofollow\"><img src=\"https:\/\/blog.mhasin.eu\/wp-content\/plugins\/pdf24-post-to-pdf\/img\/pdf_32x32.png\" alt=\"\" border=\"0\" height=\"32\" \/><\/a> \t\t<span class=\"pdf24Plugin-cp-space\">\u00a0\u00a0<\/span> \t\t<span class=\"pdf24Plugin-cp-text\">Send article as PDF<\/span> \t\t<span class=\"pdf24Plugin-cp-space\">\u00a0\u00a0<\/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: \u00a0\u00a0 Send article as PDF \u00a0\u00a0\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}]}}