0

I'm trying to use the output of nmap command in linux (shell output):

sudo nmap -sn 192.168.1.0/24
------
Nmap scan report for 192.168.1.98
Host is up (0.094s latency).
MAC Address: B8:27:EB:CE:0A:9F (Raspberry Pi Foundation)

In a python script via subprocess:

import subprocess
p = subprocess.Popen(["nmap", "-sn", "192.168.1.0/24"], stdout=subprocess.PIPE)
output, err = p.communicate()
print ("*** Running nmap -sn 192.168.1.0/24 ***\n", output)

Which works pretty well except from the fact that I NEED the MAC line that shell output has and subprocess doesn't.

subprocess output:

\nNmap scan report for 192.168.1.98\nHost is up (0.015s latency).\n

I'm working on an idea of getting IP via MAC/Name and I can't see how to do it without that line...

3
  • 1
    Are you running the script as root? Commented Jul 9, 2020 at 14:46
  • I don't think it's writing to stderr. Have you tried prefixing with sudo? Although, using a package is a better approach. Commented Jul 9, 2020 at 14:48
  • Thanks @CMinusMinus! That solved it.... Commented Jul 9, 2020 at 15:19

1 Answer 1

1

You don't need to run nmap as a subprocess in python, You can just install the nmap library and import it.

pip install python-nmap

Then write your code:

import json
import nmap

np = nmap.PortScanner()

target = '192.168.1.0/24'

# Scan the subnet 
results = np.scan(hosts=target, arguments='-sn')

# Clean the data nmap returns
results = results['scan']
output = {}
for result in results:
    output[result] = {}
    # Add the MAC addr to the IP
    try:
        output[result]['mac']       = results[result]['addresses']['mac']
    except:
        output[result]['mac']       = 'No MAC address avalible'
    # Add the vendor to the IP
    try:
        output[result]['vendor']    = list(results[result]['vendor'].values())[0]
    except:
        output[result]['vendor']    = 'No vendor info avalible'

print(json.dumps(output,indent=2))

When you run your code you have to run it as sudo or else you wont get the MAC addresses.

Output should look like this

{
  "192.168.1.1": {
    "mac": "16:91:82:xx:xx:xx",
    "vendor": "No vendor info avalible"
  },
  "192.168.1.10": {
    "mac": "44:39:C4:xx:xx:xx",
    "vendor": "Universal Global Scientific Industrial"
  },
  "192.168.1.50": {
    "mac": "No MAC address avalible",
    "vendor": "No vendor info avalible"
  }
}

I hope it was helpful :-)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Mama. I was messing around with "python-nmap" version but it didn't solve my problem (Or I didn't figure how to solve it). I've used this version with this method nmap = nmap3.NmapHostDiscovery() results = nmap.nmap_no_portscan("192.168.1.0/24") The output works, but it does not gives me MAC addres or names, just IP and status.
This is the output:{'state': 'up', 'reason': 'conn-refused', 'reason_ttl': '0', 'addr': '192.168.1.98', 'addrtype': 'ipv4'} I need MAC and name in order to see IP Owners... maybe it's about conn-refused?
If I run my script as "sudo" I get this error: [sudo] password for daniziz: Traceback (most recent call last): File "nmap_rb.py", line 2, in <module> import nmap ModuleNotFoundError: No module named 'nmap'
Ok, I've done it with sudo pip install python-nmap since if I don't install it with sudo privileges I cant run .py script with sudo. Im going to play with JSON format in order to get my vendors, but this is better way than subprocess option.
Here is my solution: for result in results: try: # View to list and append vendor = list(results[result]["vendor"].values())[0] vendors_check.append(vendor) except IndexError as E: print("Element scan error, not adding to list and continuing...")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.