Python Socket

hostname by ip address

import socket
socket.gethostbyaddr("8.8.8.8")
('google-public-dns-a.google.com', [], ['8.8.8.8'])

* ip2hostname.py 표준입력에서 IP 주소를 읽어 호스트 이름을 출력해줌

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys 
import socket
 
for line in sys.stdin:
    ip = line.strip()
    try:
        print(ip + ' ' + socket.gethostbyaddr(ip)[0])
    except:
        print(ip + ' [unknown host]')
echo '192.168.123.24' |  ip2hostname.py