Last active
November 25, 2020 12:47
-
-
Save yk-st/2a22b865ff8a6a6870a9de57269242f7 to your computer and use it in GitHub Desktop.
list all private in CIDR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
isMaster=$(cat /emr/instance-controller/lib/info/instance.json | jq .isMaster) | |
if ! "${isMaster}" ; then | |
echo "slave server" | |
exit 0 | |
fi | |
# consumed ip addres by others including self | |
aws ec2 describe-network-interfaces | jq -r .[][] | grep "PrivateIpAddress" | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d':' -f 2 | tr -d \" | tr -d , | tr -d " " | sort | uniq > /tmp/ip.txt | |
cidr=$(ip r | awk '{print $ 1}' | grep '/') | |
# reserved IP | |
ipcalc -b $cidr | cut -d'=' -f 2 >> /tmp/ip.txt | |
nmap -sL $cidr | awk '/Nmap scan report/{print $NF}' | tr -d '()' | sort -V | head -n 4 >> /tmp/ip.txt | |
nmap -sL $cidr | awk '/Nmap scan report/{print $NF}' | tr -d '()' | sort -V | tail -n 1 >> /tmp/ip.txt | |
# list adresses | |
nmap -sL $cidr | awk '/Nmap scan report/{print $NF}' | tr -d "()" > /tmp/ip_range.txt | |
while read line | |
do | |
if grep -x ${line} /tmp/ip.txt > /dev/null ; then | |
echo ${line} | |
else | |
echo "OK" | |
echo -n "${line}" > /tmp/decided.txt | |
break | |
fi | |
done < /tmp/ip_range.txt |
#!/bin/bash
#aws ec2 describe-network-interfaces | jq -r .[][].PrivateIpAddress > /tmp/ip.txt
cidr=$(ip r | awk '{print $ 1}' | grep '/')
reserved IP
ipcalc -b $cidr | cut -d'=' -f 2 > /tmp/ip.txt
nmap -sL $cidr | awk '/Nmap scan report/{print $NF}' | tr -d '()' | sort -V | head -n 4 >> /tmp/ip.txt
list adresses
nmap -sL $cidr | awk '/Nmap scan report/{print $NF}' | tr -d "()" > /tmp/ip_range.txt
while read line
do
if grep -x ${line} /tmp/ip.txt > /dev/null ; then
echo ${line}
else
echo "OK"
echo -n "export ASSOATION_IP=${line}" >> ~/.bash_profile
break
fi
done < /tmp/ip_range.txt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
need nmap install
sudo yum install nmap