flipje

ping hosts in subnet

Aug 18th, 2011
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.06 KB | None | 0 0
  1. #!/bin/bash
  2. # +-----------------------------------------------------------------------------------------+
  3. # | nmap host check - scans subnet on ip's                                                  |
  4. # |                                                                                         |
  5. # |                                                                                         |
  6. # |                                                                                         |
  7. # | Augustus 2011 flip hess [email protected]                                             |
  8. # +-----------------------------------------------------------------------------------------+
  9.  
  10. # Global variables:
  11.  
  12. PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  13. SCRIPT_PATH="${0}"
  14. ARGS="${#}"
  15.  
  16. # Functions:
  17.  
  18.   # exit function
  19.   function die()
  20.   {
  21.     echo "${1}"
  22.     exit 1
  23.   }
  24.  
  25.   # Shows usage function.
  26.   function fShowUsage()
  27.   {
  28.     echo "Usage: ${SCRIPT_PATH}"
  29.     return 0
  30.   }
  31.  
  32.   # check for.......
  33.   function fCheck()
  34.   {
  35.     # script depends on:
  36.     [ -x $(which nmap) ] || die "This script depends on nmap"
  37.  
  38.     # user must be root:
  39.     [ $(whoami) = root ] || die "User must be root!"
  40.  
  41.     # check for arguments:
  42.     [ ${ARGS} = 0 ] || { fShowUsage; exit 1; }
  43.  
  44.     echo ""
  45.  
  46.     return 0
  47.   }
  48.  
  49.   # The main function.
  50.   function fMain()
  51.   {
  52.    # determine subnet
  53.    NET="$( ifconfig | grep 'Bcast:' | cut -d ":" -f 2 | cut -d "." -f 1,2,3 )"
  54.    
  55.    # ping all subnet hosts /24
  56.    HOSTS="$( nmap ${NET}.0/24 -sP | grep 'report' | sed -e "s/.*${NET}.//" -e 's/)//g' )"
  57.    
  58.    # for loopje met mooie resultaatjes:
  59.    for HOST in ${HOSTS}
  60.    do
  61.      NAME="$( host ${NET}.${HOST} | grep 'domain' | awk '{ print $NF }' | cut -d '.' -f 1,2,3 )"
  62.      echo "${NET}.${HOST}.:${NAME}"
  63.      ONLINE[`expr ${#ONLINE[@]} + 1`]=${HOST}
  64.    done
  65.    echo
  66.    echo "Computers online: ${#ONLINE[*]}"
  67.  
  68.    return 0
  69.  
  70.   }
  71.  
  72.  # check environment:
  73.   fCheck
  74.  
  75.  # Start the program:
  76.   fMain "${@}"
  77.  
  78.  # Exit with previous return code:
  79.   exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment