[英]Obtaining IP from command 'host' executed inside python
I have this function to show me the 1st ip of a domain:我有这个功能来显示域的第一个 ip:
def get_ip_address(url):
command="host "+url
process=os.popen(command)
results=str(process.read())
marker=results.find('has address')+12
print results[marker:].splitlines()[0]
return results[marker:].splitlines()[0]
But this only shows me the first ip.但这仅显示我的第一个 ip。 I'd like to show only the ip's.
我只想显示ip。 The marker is for not having "has address" as shown below (imagine I input "reddit.com":
标记是因为没有“有地址”,如下所示(假设我输入了“reddit.com”:
['151.101.65.140', 'reddit.com has address 151.101.129.140', 'reddit.com has address 151.101.193.140', 'reddit.com has address 151.101.1.140', 'reddit.com mail is handled by 1 aspmx.l.google.com.', 'reddit.com mail is handled by 10 aspmx2.googlemail.com.', 'reddit.com mail is handled by 10 aspmx3.googlemail.com.', 'reddit.com mail is handled by 5 alt1.aspmx.l.google.com.', 'reddit.com mail is handled by 5 alt2.aspmx.l.google.com.']
I want to show only the ips, not reddit.com has address
nor once the ip's end, mail is handled
etc.我只想显示 ips,而不是
reddit.com has address
也没有一旦 ip 结束, mail is handled
等。
I tried with我试过
def get_ip_address(url):
command="host "+url
process=os.popen(command)
results=str(process.read())
marker=results.find('has address')+12
i=0
arrayIps=[]
while "has address" in results[marker:].splitlines()[i]:
print results[marker:].splitlines()[i]
arrayIps.append(results[marker:].splitlines()[i])
print("array")
print arrayIps[i]
i=i+1
return arrayIps
But it is not working!但它不起作用! Not even returning anything useful!
甚至没有返回任何有用的东西!
What I am expecting is an array with (in this case):我期待的是一个数组(在这种情况下):
'151.101.65.140', '151.101.129.140', '151.101.193.140', '151.101.1.140'
See it shows multiple hosts as you required .Your output can be generated using a map
function看到它根据您的需要显示多个主机。您的输出可以使用
map
功能生成
In [132]: socket.getaddrinfo("reddit.com", 80, proto=socket.IPPROTO_TCP)
Out[132]:
[(<AddressFamily.AF_INET: 2>,
<SocketKind.SOCK_STREAM: 1>,
6,
'',
('151.101.65.140', 80)),
(<AddressFamily.AF_INET: 2>,
<SocketKind.SOCK_STREAM: 1>,
6,
'',
('151.101.1.140', 80)),
(<AddressFamily.AF_INET: 2>,
<SocketKind.SOCK_STREAM: 1>,
6,
'',
('151.101.129.140', 80)),
(<AddressFamily.AF_INET: 2>,
<SocketKind.SOCK_STREAM: 1>,
6,
'',
('151.101.193.140', 80))]
In [134]: list(map(lambda x:x[4][0],socket.getaddrinfo("reddit.com", 80, proto=socket.IPPROTO_TCP)))
Out[134]: ['151.101.129.140', '151.101.193.140', '151.101.65.140', '151.101.1.140']
You should try the dig command instead of host and apply a proper regexp aswell:您应该尝试使用 dig 命令而不是 host 并应用适当的正则表达式:
from subprocess import run, PIPE
def get_answer(url):
response = run(["dig", "+noall", "+answer", "+short","{url}".format(url=url)], stdout=PIPE, universal_newlines=True)
return response.stdout
Notes:笔记:
EDIT编辑
As mentioned in commentaries, you can rid off the regex part using +short option in dig.正如评论中提到的,您可以在 dig 中使用 +short 选项去掉正则表达式部分。
Try splitting the string IPs at their spaces using .split()
and then taking the last item of it [-1]
and then splitting the last item (which should be an IP address) at the periods .split('.')
.尝试使用
.split()
在空格处拆分字符串 IP,然后取其中的最后一项[-1]
,然后在句点.split('.')
处拆分最后一项(应该是 IP 地址.split('.')
。 Join the strings returned from that using ''.join(iterable)
where iterable are the value from .split('.')
.使用
''.join(iterable)
加入从那个返回的字符串,其中 iterable 是.split('.')
。 Check if the joined string is a numeric value using .isnumeric()
and if that is True, print and return the last item.使用
.isnumeric()
检查连接的字符串是否为数值,如果为 True,则打印并返回最后一项。
def get_ip_address(url):
command="host "+url
process=os.popen(command)
results=str(process.read())
marker=results.find('has address')+12
ip_list=[]
for ip in results[marker:].splitlines()[0]:
if ''.join(ip.split()[-1].split('.')).isnumeric():
ip_list.append(ip.split()[-1])
print ip_list
return ip_list
The bad news is that I can't seem to get results=str(process.read())
to be set to anything.坏消息是我似乎无法将
results=str(process.read())
设置为任何内容。 It currently just returns an empty string, but hopefully, you'll have more luck :D它目前只返回一个空字符串,但希望你会有更多的运气:D
(Edit: As said in your comments, socket.getaddrinfo()
works with flying colours. Not sure of it on Python 2.7, but it works here in 3.7.1) (编辑:正如您在评论中所说,
socket.getaddrinfo()
可以很好地工作。不确定它在 Python 2.7 上,但它在 3.7.1 中工作)
(Edit2: Here's your code in 3.7.1. Should be similar in 2.7) (Edit2:这是您在 3.7.1 中的代码。在 2.7 中应该类似)
import socket
ip_list = []
for info in socket.getaddrinfo('reddit.com', 80):
ip_list.append(info[-1][0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.