简体   繁体   English

在Python中使用Bash仅获取IP地址

[英]Get only the IP address using Bash inside Python

I'm writing a little Python script(Python 2.7.13) to do ARP scanning. 我正在编写一个小的Python脚本(Python 2.7.13)来进行ARP扫描。 The problem is I don't know too much of Shell Scripting yet... I'm running on Kali Linux and this is what I'm using so far: 问题是我还不太了解Shell Scripting ...我在Kali Linux上运行,这是我到目前为止使用的:

import subprocess
interface = "wlan0"
ip = subprocess.check_output("ifconfig " + interface + " | grep 'inet'| cut -d':' -f2", shell = True).strip()    

By doing that I'm still getting this as ip: 通过这样做,我仍然得到这个作为ip:

'inet 192.168.0.43  netmask 255.255.255.0  broadcast 192.168.0.255\n14c\n14c'

What do I need to add to my ip assignment to get 192.168.0.43 as my ip variable? 我需要添加到我的IP分配中以获得192.168.0.43作为我的IP变量,我需要做什么? I already looked up on the web but I just couldn't find a solution to this on Linux... I only found really good solutions for this on the Mac OS X( Parse ifconfig to get only my IP address using Bash ), but I'd like for this code to run on my Kali Linux installation first, than I can port it to Mac OS X later I also already tried using this answer( Finding local IP addresses using Python's stdlib ) but I was looking for a way of manipulating the result of ifconfig, just by using cut or awk... 我已经在网上查找了,但是在Linux上找不到解决方案……我只在Mac OS X上找到了一个非常好的解决方案(将ifconfig解析为仅使用Bash获取我的IP地址 ),但是我希望此代码首先在我的Kali Linux安装上运行,然后再将其移植到Mac OS X,我也已经尝试使用此答案( 使用Python的stdlib查找本地IP地址 ),但是我正在寻找一种方法只需使用cut或awk即可操纵ifconfig的结果...

or you can use below command to get the ip address: 或者您可以使用以下命令获取IP地址:

ip = subprocess.check_output("ifconfig " + interface + " | awk '/inet / {print $2}', shell=True)

and there are some other answers in here. 还有其他一些答案。 how-can-i-get-the-ip-address-of-eth0-in-python 如何-可以-I-得到最IP地址-的-eth0的功能于蟒蛇

The bug is that cut -d ':' splits on colons, not on whitespace. 错误是cut -d ':'在冒号而不是空格上分割。 But using external utilities to parse the output from ifconfig is crazy anyway. 但是,使用外部实用程序来解析ifconfig的输出还是很疯狂的。 Python does it quite well natively, and probably both faster and much easier to read and understand if you are not familiar with the standard shell utilities (and perhaps even if you are). Python在本机上做得很好,而且如果您不熟悉标准的Shell实用程序(甚至您可能不熟悉),可能会更快,更容易阅读和理解。

ifconfig = subprocess.check_output(['ifconfig', interface])  # no shell=True
for line in ifconfig.split('\n'):
    fields = line.split()
    if fields[0] == 'inet':
        ip = fields[1]  # no need to strip() if you discard the tail
        break

This is still not entirely portable because the details of ifconfig will look different on different platforms (though this particular detail is probably okay) and the interface names will look different on different platforms. 这仍然不是完全可移植的,因为ifconfig的细节在不同的平台上看起来会有所不同(尽管这个特定的细节可能还可以),并且接口名称在不同的平台上看起来会有所不同。 A pure Python solution which avoids ifconfig is probably going to be more portable. 避免ifconfig纯Python解决方案可能会更具可移植性。

On OSX the first character before inet is a tab, but not on Ubuntu (so I'm guessing that will hold on Kali too). 在OSX上, inet之前的第一个字符是制表符,但在Ubuntu上则不是(因此,我猜它也将在Kali上适用)。 I had to change the code to accommodate. 我不得不更改代码以适应。 See the edit history for an example of a portability issue. 有关可移植性问题的示例,请参见编辑历史记录。

Using the netifaces library, this boils down to: 使用netifaces库,可以归结为:

>>> for iface in netifaces.interfaces():
...   for iftype, data in netifaces.ifaddresses(iface).items():
...      if iftype in (netifaces.AF_INET, netifaces.AF_INET6):
...        for info in data:
...           print(info['addr'])
...
127.0.0.1
::1
172.27.127.53
fe80::807a:c726:c91:6269%enp3s0
192.168.1.12
fe80::e69:7e0:4b7:abc2%wlxc4e984122c4e
192.168.0.1
fe80::42:57ff:fef6:3fb2%docker0
192.168.48.1
192.168.32.1
fe80::cb3:17ff:fe80:481e%veth8dd1c5a
fe80::48c:96ff:fea9:e2eb%vethff4fd4a
fe80::5ca8:68ff:feb1:8231%vethc52d992
fe80::406c:40ff:fe2f:4c7e%vethf1c1221

This accounts for both ipv6 and ipv4 addresses, and also deals with interface that have multiple addresses and a machine having multiple interfaces. 这既解决了ipv6和ipv4地址,又处理了具有多个地址的接口和具有多个接口的机器。

To filter only the ipv4 addresses, change the if conditional to: 要仅过滤ipv4地址,请将if条件更改为:

if iftype in (netifaces.AF_INET,):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM