-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathconnhost
More file actions
executable file
·74 lines (61 loc) · 1.66 KB
/
Copy pathconnhost
File metadata and controls
executable file
·74 lines (61 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# coding=utf8
import socket
import sys
port = int(22)
def Usage():
print
print '''\033[;32m resolve hostname or ip
Usage: %s hostname or ip
%s -f file
%s by 919953500@qq.com
\033[0m''' % (sys.argv[0],sys.argv[0],sys.argv[0])
sys.exit()
try:
if len(sys.argv[0]) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
Usage()
except IndexError:
Usage()
# 将IP地址解析成主机名
def ip_to_host(ip):
try:
result = socket.gethostbyaddr(ip)
print result[0] + ' | ' + 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 result[0][-1][0] + ' | ' + host
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'
# -f 指定文件批量操作
argv = sys.argv[1]
if argv == '-f':
try:
file = sys.argv[2]
f = open(file,'r')
for line in f.readlines():
line = line.strip().strip('\n')
if valid_ip(line) == 'True':
ip_to_host(line)
elif valid_ip(line) == 'False':
host_to_ip(line)
f.close()
except:
print '\033[;31m Please specify a file\033[0m'
elif valid_ip(argv) == 'True':
ip_to_host(argv)
elif valid_ip(argv) == 'False':
host_to_ip(argv)
else:
Usage()