ImportNmap#Import nmap.py ModuleNM = Nmap. Portscanner ()#instantiation of Nmap. Portscanner ObjectNm.scan ('127.0.0.1','22-443')#Scan 127.0.0.1, port number from 22 to 443Nm.command_line ()#gets the command line that currently performs the scan: Nmap-ox-P 22-443 127.0.0.1Nm.scaninfo ()#get Nmap Scan information {' TCP ': {' services ': ' 22-443 ', ' method ': ' Connect '}}Nm.all_hosts ()#get all the hosts that have been scannednm['127.0.0.1'].hostname ()#gets the host name of a host 127.0.0.1, typically a user recordnm['127.0.0.1'].hostnames ()#Gets the host list of host 127.0.0.1, returning a dictionary type#[{' Name ': ' hostname1 ', ' type ': ' PTR '}, {' name ': ' hostname2 ', ' type ': ' User '}]nm['127.0.0.1'].state ()#get the status of host 127.0.0.1 (up|down|unknown|skipped)nm['127.0.0.1'].all_protocols ()#gets the executed protocol [' TCP ', ' UDP '] contains (IP|TCP|UDP|SCTP)nm['127.0.0.1']['TCP'].keys ()#get all the port numbers for the TCP protocolnm['127.0.0.1'].ALL_TCP ()#get all port numbers for the TCP protocol (sort by port number size)nm['127.0.0.1'].ALL_UDP ()#get all the port numbers for the UDP protocol (sort by port number size)nm['127.0.0.1'].ALL_SCTP ()#get all port numbers for the SCTP protocol (sort by port number size)nm['127.0.0.1'].has_tcp (22)#host 127.0.0.1 Whether there is any information about Port 22nm['127.0.0.1']['TCP'][22]#Get host 127.0.0.1 information about Port 22nm['127.0.0.1'].tcp (22)#Get host 127.0.0.1 information about Port 22nm['127.0.0.1']['TCP'][22][' State']#get the status of host 22 ports (open)
Basic usage of Nmap's library under Python