-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. 파일(ex:hosts.txt)에 담긴 호스트 주소나 도메인을 IP 주소로 변환하고 그 값을 화면에 출력한다. 2. IP 주소를 패킷 바이너리 포맷(ex:7dd1de8e)으로 변환한다.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# host_to_ip.py | ||
import socket | ||
from sys import argv | ||
from binascii import hexlify | ||
|
||
script, file_name = argv | ||
""" | ||
파일(ex:hosts.txt)에 담긴 호스트 주소나 도메인을 IP 주소로 변환하고 그 값을 화면에 출력한다. | ||
""" | ||
remote_host_ip_list = [] | ||
|
||
def get_remote_machine_info(): | ||
""" | ||
file_name은 절대 경로를 사용할 수 있다. ex) '/home/python/hosts.txt' | ||
""" | ||
file = open(file_name, encoding='utf-8') | ||
lines = file.readlines() | ||
for line in lines: | ||
remote_host = line.rstrip('\n') | ||
try: | ||
remote_host_ip_list.append(socket.gethostbyname(remote_host)) | ||
#print("Ip address of %s: %s" % (remote_host, socket.gethostbyname(remote_host))) | ||
except OSError as err: | ||
print("%s: %s" % (remote_host, err)) | ||
|
||
""" | ||
get_remote_machine_info에서 얻은 IP 주소를 패킷 바이너리 포맷(ex:7dd1de8e)으로 변환한다. | ||
""" | ||
def convert_ip4_address(): | ||
get_remote_machine_info() | ||
for ip_addr in remote_host_ip_list: | ||
packed_ip_addr = socket.inet_aton(ip_addr) | ||
unpacked_ip_addr = socket.inet_ntoa(packed_ip_addr) | ||
print("IP Address: %s => Packed: %s, Unpacked: %s" % (ip_addr, hexlify(packed_ip_addr), unpacked_ip_addr)) | ||
|
||
if __name__ == '__main__': | ||
convert_ip4_address() |