The socket module implements a port scanner
# -*- coding: utf-8 -*-
# @Author : Lan126
import optparse
from socket import *
from threading import *
screenLock = Semaphore(value=1)
def connScan(tgtHost, tgtPort):
try:
connSkt = socket(AF_INET, SOCK_STREAM)
connSkt.connect((tgtHost, tgtPort))
connSkt.send(b"Hello!Baby!!!\r\n")
results = connSkt.recv(100)
screenLock.acquire()
print("[+]%d/tcp open" % tgtPort)
print("[+] " + results.decode("utf-8"))
//得到的result为bytes转成str要用utf-8解码
except Exception as e:
screenLock.acquire()
print("[-]%d/tcp closed " % tgtPort)
print("\n"+str(e))
finally:
screenLock.release()
connSkt.close()
def portScan(tgtHost, tgtPorts):
try:
tgtIp = gethostbyname(tgtHost)
except:
print("[-] Cannot resolve '%s': Unknown host" % tgtHost)
return
try:
tgtName = gethostbyaddr(tgtIp)
print("\n[+] Scan Results for: " + tgtName[0])
except:
print("\n[+] Scan Results for: " + tgtIp)
setdefaulttimeout(1)
for tgtPort in tgtPorts:
t = Thread(target=connScan, args=(tgtHost, int(tgtPort)))
t.start()
def main():
parser = optparse.OptionParser("usage%prog" + "-H <target host> -p <target port>")
parser.add_option("-H", dest="tgtHost", type="string", help="specify target host")
parser.add_option("-p", dest="tgtPort", type="string", help="specify target port[s] separated by comma")
options, args = parser.parse_args()
tgtHost = options.tgtHost
tgtPorts = str(options.tgtPort).split(",")
if (tgtHost is None) | (tgtPorts is None):
print(parser.usage)
exit(0)
portScan(tgtHost, tgtPorts)
if __name__ == "__main__":
main()
result graph
ideas
__optargs__ has nothing to say. This time the script mainly uses the socket module
. We first create a TCP/IP socket, which is the basis for writing applications for network communication.
connSkt = socket(AF_INET, SOCK_STREAM)
connSkt.connect((tgtHost, tgtPort))
Use AF_INET family, SOCK_STREAM type socket to instantiate a socket object
because the AF_INET family is selected, the parameter of the connect function here is a tuple when connecting to the remote end, the first is the host name or ip address, and the second is the port number It is an integer
and then uses the revc function to accept data of a certain size. During the above operation, if an exception occurs, it is determined that the port is closed.
detail
The gethostbyname function returns an Ipv4 string. If the ipv4 address is passed in, the
gethostbyaddr function returns a triple, (hostname, aliaslist, ipaddrlist) , and the hostname is bound to the hostname of the incoming parameter in
order to achieve normal printing. To operate, you must use the semaphore mechanism and use screenLock.acquire() to perform the locking operation. If the semaphore is locked, the process continues to execute, and other processes wait for the semaphore to be released in the finally block.
The value of Semaphore(value=1) is The size of the built-in counter
nmap port scan
Since the nmap module cannot be used on the windows platform, the centos operating system is replaced here.
result
ideas
It is basically the same as the above implementation, but here is a more advanced package of the above, but here you have to pass the ip address and port number, and the host name will be gg
python3 encoding and strings
To explain in detail
, the main thing is to know that what is transmitted or received to the network or disk is a bytes object. When converting to str, you need to decode from str to bytes and use encode.