0

I'm writing a little Python script(Python 2.7.13) to do ARP scanning. 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:

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:

'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? 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...

Community
  • 1
  • 1
  • 3
    Possible duplicate of [Finding local IP addresses using Python's stdlib](http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib) – jordanm May 07 '17 at 05:48
  • @jordanm thanks, but that's not what I wanted... But doing what this other post said I just get '127.0.1.1' ... – Andre Korol May 07 '17 at 05:52
  • `ip.split()[1]` – t.m.adam May 07 '17 at 05:53
  • Thank you @t.m.adam !! That simple solution just works fine! I was thinking of really just using split(), but I thought there would be a simpler solution just usig shell script. But your answer really gets the job done! – Andre Korol May 07 '17 at 05:58
  • You 're welcome . You dont need to use bash commands at all , you can do it all in python, i would post an answer but i'm on windows at the time . Also , i think `ifconfig` will be replaced with `ip` , you may want to use that – t.m.adam May 07 '17 at 06:12
  • That's true @t.m.adam ! I should stop trying to use Python alongside Bash... I read that too, now we gotta use `ip addr show` for example... – Andre Korol May 07 '17 at 06:17
  • If you want to get an ip associated with an interface, you can try `netiface` module – kuro May 07 '17 at 06:18
  • If this is not just a training exercise, you do know that kali comes with `arp-scan` that you can just directly call from bash like: `arp-scan -I eth0 --localnet` etc. – Olli K May 07 '17 at 16:30
  • @jordanm is correct to point out that this should, ideally, be done natively in Python rather than parsing the output of an external command. BTW, it’s worth being aware that on GNU/Linux systems, `ifconfig` is part of the deprecated (and no longer maintained) `net-tools` package. The developers instead recommend the use of the `ip` command which has been available since Linux 2.2. – Anthony Geoghegan May 08 '17 at 15:35

3 Answers3

0

or you can use below command to get the ip address:

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

Community
  • 1
  • 1
Z.Liu
  • 403
  • 2
  • 9
0

The bug is that cut -d ':' splits on colons, not on whitespace. But using external utilities to parse the output from ifconfig is crazy anyway. 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).

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. A pure Python solution which avoids ifconfig is probably going to be more portable.

On OSX the first character before inet is a tab, but not on Ubuntu (so I'm guessing that will hold on Kali too). I had to change the code to accommodate. See the edit history for an example of a portability issue.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Maybe see also http://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess – tripleee May 08 '17 at 04:35
0

Using the netifaces library, this boils down to:

>>> 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.

To filter only the ipv4 addresses, change the if conditional to:

if iftype in (netifaces.AF_INET,):
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284