5

Python脚本批量解析主机名和IP地址

 2 years ago
source link: https://chegva.com/3792.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python脚本批量解析主机名和IP地址

  • 生产实践

    用于中控机上批量解析主机名和IP地址

  • 脚本内容:      

生产中在中控机上时常需要查看服务器IP或通过IP查主机名,如果有多个ip或主机名时不太方便,此python脚本使用socket模块可以批量解析主机名和ip,支持读取文件。脚本内容如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
############################################################
# $Name:         hc.py
# $Version:      v1.0
# $Function:     Convert Host Name to IP Address and vice versa
# $Author:       Zhihe An
# $organization: chegva.com
# $Create Date:  2020-06-03
############################################################
import os
import socket
import sys
port = int(22)
# 使用提示
def Usage():
print '''\033[;32mUsage: %s [ip or hostname] [-f file] [-h] [--help]
Convert Host Name to IP Address and vice versa
created by [email protected]
\033[0m''' % (os.path.basename(sys.argv[0]))
sys.exit()
# 将IP地址解析成主机名
def ip_to_host(ip):
try:
result = socket.gethostbyaddr(ip)
print result[0+ '\t' + ip
except (socket.gaierror, socket.herror):
print '\033[;33m IP   : %s Not found\033[0m' % ip
# 将主机名解析成IP地址
def host_to_ip(host):
try:
#result = socket.getaddrinfo(host,port)
result = socket.getaddrinfo(host,None)
print host + '\t' + result[0][-1][0]
except (socket.gaierror, socket.herror):
print '\033[;33m HOST : %s Not found\033[0m' % host
# 验证IP是否合法
def valid_ip(ip):
try:
socket.inet_aton(ip)
return 'True'
except:
return 'False'
# 指定文件批量操作
def load_file():
try:
file = sys.argv[2]
with open(file,'r') as f:
linesText = f.readlines();
linesText.sort();
for line in linesText:
line = line.strip('\n').strip()
if line:
if valid_ip(line) == 'True':
ip_to_host(line)
elif valid_ip(line) == 'False':
host_to_ip(line)
except:
print '\033[;31m Empty file or specify is not a file!\033[0m'
# 批量解析IP地址或主机名
def batch_resolve(all_list):
all_list.sort();
for line in all_list:
if valid_ip(line) == 'True':
ip_to_host(line)
elif valid_ip(line) == 'False':
host_to_ip(line)
def main(argv):
# 输入参数检查
try:
if len(sys.argv[0]) == 1 or sys.argv[1== "-h" or sys.argv[1== "--help":
Usage()
except IndexError:
Usage()
argv_all = argv[1:]
argv_sum = len(argv[1:])
if argv_sum == 2:
argv = argv[1]
if argv == '-f':
load_file()
else:
batch_resolve(argv_all)
else:
batch_resolve(argv_all)
if __name__ == '__main__':
main(sys.argv)

◎查看效果

anzhihe@theone-op:~$ hc
Usage: hc [ip or hostname] [-f file] [-h] [--help]

Convert Host Name to IP Address and vice versa
created by [email protected]

anzhihe@theone-op:~$ hc chegva-op-simulation00.bj01 10.89.32.63
chegva-op-index00.sh01	10.89.32.63
chegva-op-simulation00.bj01	100.90.146.39

anzhihe@theone-op:~$ hc -f tmp
chegva-op-index00.sh01	10.89.32.63
chegva-op-index01.sh01	100.70.106.19
chegva-op-simulation00.bj01	100.90.146.39
chegva-op-simulation01.bj01	100.90.142.40

anzhihe@theone-op:~$ hc -f xx
 Empty file or specify is not a file!
anzhihe@theone-op:~$ hc xx
 HOST : xx Not found
anzhihe@theone-op:~$ hc 11
 IP   : 11 Not found
anzhihe安志合个人博客,版权所有丨 如未注明,均为原创 丨转载请注明转自:https://chegva.com/3792.html | ☆★★每天进步一点点,加油!★★☆

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK