remote OS detection in python

泪湿孤枕 提交于 2019-12-11 06:16:31

问题


I want to implement an OS detection using python (like nmap), I find python-nmap-0.3.4.tar.gz library, but it didn't provide Operating system in response! How can I change it to achieve my goal.

EDIT: in the site sample:

>>> import nmap
>>> nm = nmap.PortScanner()
>>> nm.scan('127.0.0.1', '22-443')
>>> print(nm.csv())
host;protocol;port;name;state;product;extrainfo;reason;version;conf
127.0.0.1;tcp;22;ssh;open;OpenSSH;protocol 2.0;syn-ack;5.9p1 Debian 5ubuntu1;10
127.0.0.1;tcp;25;smtp;open;Exim smtpd;;syn-ack;4.76;10
127.0.0.1;tcp;53;domain;open;dnsmasq;;syn-ack;2.59;10
127.0.0.1;tcp;80;http;open;Apache httpd;(Ubuntu);syn-ack;2.2.22;10
127.0.0.1;tcp;111;rpcbind;open;;;syn-ack;;10
127.0.0.1;tcp;139;netbios-ssn;open;Samba smbd;workgroup: WORKGROUP;syn-ack;3.X;10
127.0.0.1;tcp;443;;open;;;syn-ack;;

it find OS , but when I run my own it didn't show any os. is there any function to find remote OS?


回答1:


Try with -O option

nm.scan('scanme.nmap.org', arguments='-O')

The problem is the information will not appear if you do

print(nm.csv())

So you have to do a loop

nm.scan("127.0.0.1", arguments="-O")
if 'osclass' in nm['127.0.0.1']:
    for osclass in nm['127.0.0.1']['osclass']:
        print('OsClass.type : {0}'.format(osclass['type']))
        print('OsClass.vendor : {0}'.format(osclass['vendor']))
        print('OsClass.osfamily : {0}'.format(osclass['osfamily']))
        print('OsClass.osgen : {0}'.format(osclass['osgen']))
        print('OsClass.accuracy : {0}'.format(osclass['accuracy']))
        print('')

More info https://bitbucket.org/xael/python-nmap/src/391178ab25a20d7b5edbca51e187f93dc1c16ad2/example.py?at=default&fileviewer=file-view-default

PS: you need python 3.x, i don't think that it's works with Python 2.x

EDIT Of course, you can access directly by

print nm['127.0.0.1']['osclass']

Or

print nm['127.0.0.1']['osclass']['vendor']



回答2:


If you only need the OS name then you can do this:

import nmap
nm = nmap.PortScanner()
machine = nm.scan('<hostIP>', arguments='-O')
print(machine['scan']['<hostIP>']['osmatch'][0]['osclass'][0]['osfamily'])

This will provide OS Name [Ex: 'Linux']



来源:https://stackoverflow.com/questions/27158046/remote-os-detection-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!