#!/bin/bash
# Script finds nano3G femtocell on network according to its open ports.

if [ "$1" = "" ]; then
	echo "[ERROR] no interface (e.g. eth0) specified to search for nano3G"
	exit 1
fi

host_ip="$(ip address | grep $1 | grep inet \
	| cut -d 't' -f2 | cut -d '/' -f1)"

subnet="$(echo $host_ip | cut -d '.' -f1,2,3).1/24"

echo "[INFO] seachring nano3G on $1..."


hosts=$(nmap "${subnet}" | grep "scan report" | cut -d ' ' -f5)
sleep 10 # documentum port (10002) closes for 10 s after port scan

femtocell=""

for host in $hosts; do
	if [ "$(nmap -Pn "$host" | grep tcp | tr '\n' ' ' \
		| grep 22 | grep 8089 | grep 8090 | grep 10002)" != "" ]; then
			femtocell=$host
			echo "[INFO] Found nano3G on $femtocell" 
			break
	fi
done

if [ "$femtocell" = "" ]; then
	echo "[ERROR] No femto cell found !!!"
	exit 1
fi
