Advertisement
yakar

Subnet Ping with Bash

May 10th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.23 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Hocam subnet hesaplamasinda hata var ama vakit yetmedi malesef :(
  4. # Baslangic ip ve toplam ip blogunu hesaplayamadim, uzerinde biraz daha ugrasmam lazim
  5. # fping ile daha kolay yapilabiliyor aslinda ama dediginiz gibi zor yolu tercih ettim biraz..
  6.  
  7. if [[ $# -eq 0 ]] ; then
  8.     echo "[*] Kullanimi: $0 <interface>"
  9.     exit 1
  10. fi
  11.  
  12. IF=$1
  13. IP=$(ifconfig $IF | grep Mask | cut -d ':' -f2 | cut -d " " -f1)
  14. Mask=$(ifconfig $IF | grep Mask | cut -d ':' -f4 | cut -d " " -f1)
  15. IFS=.
  16. IPArray=($IP)
  17. MaskArray=($Mask)
  18. NetArray=()
  19. Start=0
  20. Max=$(( 255 * 255 * 255 * 255 ))
  21.  
  22. for ara in "${!IPArray[@]}";
  23. do
  24.     NetArray[$ara]=$(( ${IPArray[$ara]} & ${MaskArray[$ara]} ))
  25.     Start=$(( $Start + (${NetArray[$ara]} << (3-$ara)*8) ))
  26. done
  27.  
  28. IFS=
  29.  
  30. echo "IP Adresiniz    : $IP"
  31. echo "Subnet Maskiniz : $Mask"
  32. echo "Network         : ${NetArray[@]}"
  33.  
  34. for ((IPs=$Start; IPs <= $Max; IPs++))
  35. do
  36.     IP=$(( IPs >> 24 ))
  37.     IP="$IP.$(( (IPs >> 16) & 255 ))"
  38.     IP="$IP.$(( (IPs >> 8) & 255 ))"
  39.     IP="$IP.$(( IPs & 255 ))"
  40.     $(ping -c 1 -w 1 $IP >& /dev/null)
  41.     if [[ $? -eq 0 ]]; then
  42.         echo "[+] $IP aktif."
  43.         AKTIF=$AKTIF+1
  44.     else
  45.         PASIF=$PASIF+1
  46.         echo "[-] $IP pasif."
  47.     fi
  48. done
  49.  
  50. echo "\n[*] Tarama tamamlandi:"
  51. echo "[-] $AKFIT aktif cihaz"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement