[关闭]
@Dukebf 2017-07-12T00:13:52.000000Z 字数 3624 阅读 1383

python-nmap 使用

python-nmap python


目录

安装方法

查看官网

安装方法一:

  1. # python -m pip install python-namp

安装方法二:
下载源码,setup安装:

  1. # hg clone https://bitbucket.org/xael/python-nmap
  2. # tar xvzf python-nmap-0.6.1.tar.gz
  3. # cd python-nmap-0.6.1
  4. # python setup.py install

官网使用例子

  1. >>> import nmap
  2. >>> nm = nmap.PortScanner()
  3. >>> nm.scan('127.0.0.1', '22-443')
  4. >>> nm.command_line()
  5. 'nmap -oX - -p 22-443 -sV 127.0.0.1'
  6. >>> nm.scaninfo()
  7. {'tcp': {'services': '22-443', 'method': 'connect'}}
  8. >>> nm.all_hosts()
  9. ['127.0.0.1']
  10. >>> nm['127.0.0.1'].hostname()
  11. 'localhost'
  12. >>> nm['127.0.0.1'].state()
  13. 'up'
  14. >>> nm['127.0.0.1'].all_protocols()
  15. ['tcp']
  16. >>> nm['127.0.0.1']['tcp'].keys()
  17. [80, 25, 443, 22, 111]
  18. >>> nm['127.0.0.1'].has_tcp(22)
  19. True
  20. >>> nm['127.0.0.1'].has_tcp(23)
  21. False
  22. >>> nm['127.0.0.1']['tcp'][22]
  23. {'state': 'open', 'reason': 'syn-ack', 'name': 'ssh'}
  24. >>> nm['127.0.0.1'].tcp(22)
  25. {'state': 'open', 'reason': 'syn-ack', 'name': 'ssh'}
  26. >>> nm['127.0.0.1']['tcp'][22]['state']
  27. 'open'
  28. >>> for host in nm.all_hosts():
  29. >>> print('----------------------------------------------------')
  30. >>> print('Host : %s (%s)' % (host, nm[host].hostname()))
  31. >>> print('State : %s' % nm[host].state())
  32. >>> for proto in nm[host].all_protocols():
  33. >>> print('----------')
  34. >>> print('Protocol : %s' % proto)
  35. >>>
  36. >>> lport = nm[host][proto].keys()
  37. >>> lport.sort()
  38. >>> for port in lport:
  39. >>> print ('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
  40. ----------------------------------------------------
  41. Host : 127.0.0.1 (localhost)
  42. State : up
  43. ----------
  44. Protocol : tcp
  45. port : 22 state : open
  46. port : 25 state : open
  47. port : 80 state : open
  48. port : 111 state : open
  49. port : 443 state : open
  50. >>> print(nm.csv())
  51. host;protocol;port;name;state;product;extrainfo;reason;version;conf
  52. 127.0.0.1;tcp;22;ssh;open;OpenSSH;protocol 2.0;syn-ack;5.9p1 Debian 5ubuntu1;10
  53. 127.0.0.1;tcp;25;smtp;open;Exim smtpd;;syn-ack;4.76;10
  54. 127.0.0.1;tcp;53;domain;open;dnsmasq;;syn-ack;2.59;10
  55. 127.0.0.1;tcp;80;http;open;Apache httpd;(Ubuntu);syn-ack;2.2.22;10
  56. 127.0.0.1;tcp;111;rpcbind;open;;;syn-ack;;10
  57. 127.0.0.1;tcp;139;netbios-ssn;open;Samba smbd;workgroup: WORKGROUP;syn-ack;3.X;10
  58. 127.0.0.1;tcp;443;;open;;;syn-ack;;
  59. >>> nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -PE -PA21,23,80,3389')
  60. >>> hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
  61. >>> for host, status in hosts_list:
  62. >>> print('{0}:{1}'.host)
  63. 192.168.1.0:down
  64. 192.168.1.1:up
  65. 192.168.1.10:down
  66. 192.168.1.100:down
  67. 192.168.1.101:down
  68. 192.168.1.102:down
  69. 192.168.1.103:down
  70. 192.168.1.104:down
  71. 192.168.1.105:down
  72. [...]
  73. >>> nma = nmap.PortScannerAsync()
  74. >>> def callback_result(host, scan_result):
  75. >>> print '------------------'
  76. >>> print host, scan_result
  77. >>>
  78. >>> nma.scan(hosts='192.168.1.0/30', arguments='-sP', callback=callback_result)
  79. >>> while nma.still_scanning():
  80. >>> print("Waiting >>>")
  81. >>> nma.wait(2) # you can do whatever you want but I choose to wait after the end of the scan
  82. >>>
  83. 192.168.1.1 {'nmap': {'scanstats': {'uphosts': '1', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '0', 'totalhosts': '1', 'elapsed': '0.43'}, 'scaninfo': {}, 'command_line': 'nmap -oX - -sP 192.168.1.1'}, 'scan': {'192.168.1.1': {'status': {'state': 'up', 'reason': 'arp-response'}, 'hostname': 'neufbox'}}}
  84. ------------------
  85. 192.168.1.2 {'nmap': {'scanstats': {'uphosts': '0', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '1', 'totalhosts': '1', 'elapsed': '0.29'}, 'scaninfo': {}, 'command_line': 'nmap -oX - -sP 192.168.1.2'}, 'scan': {'192.168.1.2': {'status': {'state': 'down', 'reason': 'no-response'}, 'hostname': ''}}}
  86. ------------------
  87. 192.168.1.3 {'nmap': {'scanstats': {'uphosts': '0', 'timestr': 'Mon Jun 7 11:31:11 2010', 'downhosts': '1', 'totalhosts': '1', 'elapsed': '0.29'}, 'scaninfo': {}, 'command_line': 'nmap -oX - -sP 192.168.1.3'}, 'scan': {'192.168.1.3': {'status': {'state': 'down', 'reason': 'no-response'}, 'hostname': ''}}}
  88. >>> nm = nmap.PortScannerYield()
  89. >>> for progressive_result in nm.scan('127.0.0.1/24', '22-25'):
  90. >>> print(progressive\_result)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注