#!/bin/bash
if ! type nc >/dev/null 2>&1; then
echo "init env"
echo 'nc no install ,will insall'
sudo apt install -y netcat-traditional
sudo update-alternatives --config nc # choice 2
fi
ret_ip=`ip a | grep inet | awk -F ' ' '{print $2}'`
gateway=24
for ip in $ret_ip; do
tmp_ip=`echo $ip | awk -F '/' '{print $1}'`
tmp_gateway=`echo $ip | awk -F '/' '{print $2}'`
if test $[tmp_gateway] -eq $[gateway]
then
echo "local ip is: $tmp_ip"
echo "local gateway is : $tmp_gateway"
break
fi
done
echo
port=80
if [ "$1" ]; then
port=$1
fi
echo "start scan,port is: $port"
lan_ip=`echo $tmp_ip | awk -F '.' '{print $1 "." $2 "." $3 "." }'`
for (( i = 1; i < 255; i++ )); do
{
nc -zw2 ${lan_ip}$i $port
if [ $? -eq 0 ]; then
echo "find host:${lan_ip}$i"
fi
} &
done
wait
echo "end"
运行:shell文件名称 端口号
效果:扫描出局域网中指定端口号的 IP地址,比如扫描22端口,这样可以连接ssh,扫描5555可以连接adb