Last active
May 6, 2023 22:59
-
-
Save rnek0/2152fd058edd7a97af2a4b1688761937 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/bash | |
# Convert IPv4 to bin | |
if [ $# != 1 ]; then | |
echo -e "\n [!] This script needs an IPv4 address"; | |
echo | |
exit 0 | |
fi | |
blue_color=$(printf '\033[34m') | |
red_color=$(printf '\033[31m') | |
reset_color=$(printf '\033[m') | |
return_value="" | |
max=255 | |
min=0 | |
ipv4="${1}" | |
function start_banner(){ | |
cat <<End-of-banner | |
${blue_color} | |
======================================================================== | |
${red_color}IPv4 to dec${blue_color} | |
======================================================================== | |
${reset_color} | |
End-of-banner | |
} | |
function stop_banner(){ | |
cat <<End-of-banner | |
${blue_color} | |
======================================================================== | |
rnek0 from https://web.lunarviews.net | |
${reset_color} | |
End-of-banner | |
exit 0 | |
} | |
function check_ip(){ | |
valid_ip=$(echo ${ipv4} | grep -E '^[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}$'); | |
if [ ! -n "$valid_ip" ]; then | |
echo -e "\n ${red_color}[!]${reset_color} This IPv4 address ( ${ipv4} ) isn't valid. \n ... Please check it and try again."; | |
echo | |
stop_banner | |
fi | |
} | |
function convert_to_dec(){ | |
IFS='.' read -a array <<< "${1}" | |
for numByte in "${array[@]}"; do | |
if [ ${numByte} -ge ${min} ] && [ ${numByte} -le ${max} ]; then | |
return_value=${return_value}"$(echo "obase=2; ${numByte}" | bc | numfmt --format=%08f)." | |
else | |
echo -e "\n ${red_color}[!]${reset_color} Decimal bytes are between values 0 and 255 \n ... Check your IPv4 address: ( ${1} )" | |
echo | |
stop_banner | |
fi | |
done | |
return_value="${return_value:0:-1}" | |
echo -e "\n [+] IPv4 address : ${ipv4}" | |
echo -e " Binary value : ${blue_color}${return_value}${reset_color}\n" | |
} | |
start_banner | |
check_ip | |
convert_to_dec "${ipv4}" | |
stop_banner | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment