在ruby中将ip地址转换为32位整数

我试图找到一种方法Ruby地址转换为32位整数,用于木偶模板.

这就是我在bash中进行转换的方式.

root@ubuntu-server2:~# cat test.sh 
#!/bin/bash

#eth0 address is 10.0.2.15
privip=`ifconfig eth0 | grep "inet addr:" | cut -d : -f 2 | cut -d " " -f 1` ;

echo "Private IP: ${privip}" ;

# Turn it into unsigned 32-bit integer

ipiter=3 ;

for ipoctet in `echo ${privip} | tr . " "` ;
    do
    ipint=$(( ipint + ( ipoctet * 256 ** ipiter-- ) )) ;
    done ;

echo "Private IP int32: ${ipint}" ;

.

root@ubuntu-server2:~# bash test.sh 
Private IP: 10.0.2.15
Private IP int32: 167772687

任何帮助将不胜感激.

解决方法

require 'ipaddr'
ip = IPAddr.new "10.0.2.15"
ip.to_i                      # 167772687

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...