Posted By

JD75 on 06/02/15


Tagged

list ip address enumerator ips ipls


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

JD75


ipls


 / Published in: Bash
 

ipls

list all IPv4 addresses within the specified range

Example Usage:

# ipls 79.60.22.120 79.60.23.255

  1. #!/bin/bash
  2. #
  3. # the primary IPV4 address enumerator from the `ipls' program
  4. # The original program was written in C
  5. # For convenience, I have implemented the same functionality in bash.
  6. #
  7. # Usage:
  8. # ipls start-ip end-ip
  9. #
  10. #>@[email protected] 2015-06-01 11:00:07
  11. IP_COUNT_THRESHOLD=0xFE00 # set this to whatever you want
  12. # X.1.0.0 X.0.0.0
  13. # ` - - | - - ^
  14.  
  15. function check_ip_range() {
  16. local c q
  17. if (( $1 >= IP_COUNT_THRESHOLD )); then
  18. c=$(echo $1 | perl -ne '$x=reverse $_;$x=~s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;print scalar reverse $x;')
  19. printf 'Do you really want to print out %s IP addresses?\n' $c;
  20. read -p 'Continue Printing: [Y]es [N]o: ' q;
  21. q=`tr a-z A-Z <<< $q`
  22. if test "x${q:0:1}" = "xN" || test "${q:0:1}X" = "nX"; then
  23. echo 'Ciao...' >&2;
  24. exit 0;
  25. fi
  26. fi
  27. }
  28. function check_octet_range() {
  29. local octet
  30. for octet
  31. do
  32. if (( octet > 255 ));then
  33. printf 'ipls: invalid IPv4 address!\n' >&2
  34. printf 'Octet out of range=> %#02x [%d] (octets must be <= 0xFF)\n' $octet $octet >&2
  35. exit 3;
  36. fi
  37. done
  38. }
  39.  
  40. ####### main()
  41.  
  42. [ $# -eq 2 ] || { printf 'Usage: ipls [start-ip] [end-ip]\n' >&2; exit 1; }
  43.  
  44. read s e <<< `printf '%s\n%s\n' "$1" "$2" | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n`
  45.  
  46. read o p q r <<< `tr . ' ' <<< "$s"`
  47. read i j k l <<< `tr . ' ' <<< "$e"`
  48.  
  49. check_octet_range $o $p $q $r $i $j $k $l
  50.  
  51. n=`echo "(($i-$o)*255^3)+(($j-$p)*255^2)+(($k-$q)*255^1)+(($l-$r)*255^0)" | bc`
  52.  
  53. (( n < 0 )) && let "n = 255 + $n"
  54.  
  55. check_ip_range $n
  56.  
  57.  
  58. o1=$o o2=$p o3=$q o4=$r # octets
  59.  
  60. t=0
  61.  
  62. while (( t <= n ));
  63. do
  64. printf '%s.%s.%s.%s\n' $o1 $o2 $o3 $o4
  65. ((o4++));
  66. ((t++))
  67. if (( o4 > 255 ));
  68. then
  69. o4=0;
  70. ((o3++));
  71. if (( o3 >= 255 ));
  72. then
  73. o3=0;
  74. ((o2++));
  75. if (( o2 >= 255 ));
  76. then
  77. o2=0;
  78. ((o1++));
  79. if (( o1 >= 255 ));then
  80. printf 'ipls: first octet %d greater than 255!\n' $o1 >&2;
  81. exit 2;
  82. fi
  83. fi
  84. fi
  85. fi
  86. done
  87.  
  88. case "$e" in "$o1.$o2.$o3.$o4" ) ((t++));echo $e;; esac;
  89.  
  90. #echo "Total addresses: $t" >&2; # uncomment if you want a total printed to STDERR
  91.  
  92. exit 0

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Jacky888 on October 13, 2018

the 8 best how to use tv antenna reviewtvantennas

You need to login to post a comment.