Tuesday, 22 October 2013

Linux - Port Scan Command

Linux - Port Scan Command

Yesterday I was tasked to get open and closed ports on a specific IP address.

I found a website that I started from http://www.catonmat.net/blog/tcp-port-scanner-in-bash/

Once I was there I modified his one procedure and came up with mine.


scan() {
  if [[ -z $1 || -z $2 ]]; then
    echo "Usage: $0 <host> <port, ports, or port-range>"
    return
  fi

  local host=$1
  local ports=()
  local endS=0
  local exS=''
  case $2 in
    *-*)
      IFS=- read start end <<< "$2"

      if ((end - start > 10)); then
        endS=$start
        while [ $endS -lt  $(($end-10)) ]; do
          endS=$(($endS+10));
          (scan $host $(($endS-10))-$endS) &
        done
        wait
        echo "done $(date -d "today" +"%Y%m%d%H%M")"
        return
      fi

      for ((port=start; port <= end; port++)); do
        ports+=($port)
      done
      ;;
    *,*)
      IFS=, read -ra ports <<< "$2"
      ;;
    *)
      ports+=($2)
      ;;
  esac

  for port in "${ports[@]}"; do
    timeout 1 bash -c  "echo >/dev/tcp/$host/$port" &&
      echo -e "<$(date -d "today" +"%Y%m%d%H%M")>\t<$host>\tport $port is open" >> ~/Documents/@hack/log-$host.log ||
        echo -e "<$(date -d "today" +"%Y%m%d%H%M")>\t<$host>\tport $port NOT open" >> ~/Documents/@hack/log-$host.log
  done
}

Example use:
scan www.bjorn.co.za 80


#bearMan

No comments:

Post a Comment