Skip to content

Instantly share code, notes, and snippets.

@hoangdh
Last active March 7, 2020 15:27
Show Gist options
  • Save hoangdh/a3153a2d0cfd207edd24b04960d6d663 to your computer and use it in GitHub Desktop.
Save hoangdh/a3153a2d0cfd207edd24b04960d6d663 to your computer and use it in GitHub Desktop.
Check port 25 opened.
#!/bin/bash
file='list.txt'
port='25'
while getopts "p:f:" arg;
do
case $arg in
p)
port=$OPTARG
;;
f)
file=$OPTARG
;;
*)
echo "Unknown parameter."
;;
esac
done
if [ -f $file ]
then
if [ -z $port ]
then
cat $file | sort -u | while read -r ip; do nc -vz -w 3 $ip 25 >> /tmp/25-result.txt 2>&1; done
else
cat $file | sort -u | while read -r ip; do nc -vz -w 3 $ip $port >> /tmp/25-result.txt 2>&1; done
fi
echo "---Port $port opened:---"
cat /tmp/25-result.txt | grep -w 'succeeded' | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | sort -ur
rm -rf /tmp/25-result.txt
else
echo -e "$file: File not found.\nUsage: $0 -p <Port Number|Default: 25> -f <File-IP-Listing>\nExample: $0 -p 80 -f list1.txt"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment