Advertisement
peetaur

ip to binary

Oct 29th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.57 KB | None | 0 0
  1. tobinary() {
  2.     ip=$1
  3.     ip1=$(echo "$ip" | cut -f1 -d.)
  4.     ip2=$(echo "$ip" | cut -f2 -d.)
  5.     ip3=$(echo "$ip" | cut -f3 -d.)
  6.     ip4=$(echo "$ip" | cut -f4 -d.)
  7.    
  8.     b1=$(echo "obase=2; $ip1" | bc)
  9.     b2=$(echo "obase=2; $ip2" | bc)
  10.     b3=$(echo "obase=2; $ip3" | bc)
  11.     b4=$(echo "obase=2; $ip4" | bc)
  12.    
  13.     printf "%0.8d.%0.8d.%0.8d.%0.8d\n" $b1 $b2 $b3 $b4
  14. }
  15.  
  16. $ tobinary 34.76.28.55
  17. 00100010.01001100.00011100.00110111
  18. $ tobinary 34.76.51.131
  19. 00100010.01001100.00110011.10000011
  20. $ tobinary 255.255.192.0
  21. 11111111.11111111.11000000.00000000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement