Skip to content

Instantly share code, notes, and snippets.

@yk-st
Last active November 25, 2020 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yk-st/2a22b865ff8a6a6870a9de57269242f7 to your computer and use it in GitHub Desktop.
Save yk-st/2a22b865ff8a6a6870a9de57269242f7 to your computer and use it in GitHub Desktop.
list all private in CIDR
#!/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
@yk-st
Copy link
Author

yk-st commented May 31, 2020

need nmap install
sudo yum install nmap

@yk-st
Copy link
Author

yk-st commented May 31, 2020

#!/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