LoginSignup
1
1

More than 5 years have passed since last update.

python > IPアドレスの4つ目の数値を取得する実装

Last updated at Posted at 2015-11-24
動作環境
CentOS 6.5

http://qiita.com/7of9/items/2224c2e12bc521f02763
にてIPアドレスを取得できた。

ここからやりたいのは4つ目の数値を取得すること。

以下を使う。
http://qiita.com/7of9/items/1f049a82c169106040f2

151125c.py
import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

def IPADR_getLastDigits(ipadr):
    ipadrs = ipadr.split(".")
    return ipadrs[3]

ipadr = get_ip_address('eth0')  # '192.168.xxx.xxx'
print ipadr

last = IPADR_getLastDigits(ipadr)
print last
結果
[toLearn]$ python 151125c.py 
192.168.10.8
8
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1