Last active
December 12, 2017 14:49
-
-
Save takakabe/fcaed5849ac2b4e674b2d482be13af2d to your computer and use it in GitHub Desktop.
ShellScript
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 | |
# IPアドレスを2進数に変換する | |
# 実行例 | |
# $ ./address.sh 172.24.140.1/26 | |
# Netmask: 11111111 11111111 11111111 11000000 | |
# Address: 10101100 00011000 10001100 00000000 | |
address=${1} | |
octet1=`echo "${address}" | awk -F"." '{print $1}'` | |
octet2=`echo "${address}" | awk -F"." '{print $2}'` | |
octet3=`echo "${address}" | awk -F"." '{print $3}'` | |
octet4=`echo "${address}" | awk -F"." '{print $4}'` | |
netmask=`echo "${address}" | awk -F"/" '{print $2}'` | |
octet1_bin=`echo "obase=2; ibase=10;" ${octet1}| bc` | |
octet1_bin=`printf %08d ${octet1_bin}` | |
octet2_bin=`echo "obase=2; ibase=10;" ${octet2}| bc` | |
octet2_bin=`printf %08d ${octet2_bin}` | |
octet3_bin=`echo "obase=2; ibase=10;" ${octet3}| bc` | |
octet3_bin=`printf %08d ${octet3_bin}` | |
octet4_bin=`echo "obase=2; ibase=10;" ${octet4}| bc` | |
octet4_bin=`printf %08d ${octet4_bin}` | |
for i in `seq 1 32` | |
do | |
if [ ${i} -le ${netmask} ] | |
then | |
netmask_bin+="1" | |
else | |
netmask_bin+="0" | |
fi | |
if [ $(( i % 8 )) = 0 ] | |
then | |
netmask_bin+=" " | |
fi | |
done | |
echo "Netmask: ${netmask_bin}" | |
echo "Address: ${octet1_bin} ${octet2_bin} ${octet3_bin} ${octet4_bin}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment