OPS script: Python for bulk IP port scanning
Focus on network operation and maintenance, only send practical dry goods
Scan QR code to pay attention to the public
Today do not want to update, send an inventory, share a small piece of Python code to everyone, can achieve a simple batch port scanning, nonsense said, first on the code:
===========================================================
#-*-Coding:utf-8-*-
Import socket
Import time
Import xlrd
Import threading
Hostfile = Xlrd.open_workbook (' port_scan.xlsx ') #调用扫描目标表格文件
table_list = Hostfile.sheet_by_index (0) #读取表格中的第一个sheet页
Hang = Table_list.nrows
Lie = Table_list.ncols
Print ("Total hosts:%d"% (hang-1))
def scan (deviceid,hostname,hostip,port):
Try
s = socket.socket (socket.af_inet, socket. SOCK_STREAM)
S.settimeout (5) #超时时间
S.connect ((Hostip,port))
Print ('%d_%s%s%s OPEN '% (deviceid,hostname,hostip,port))
Except
Print ('%d_%s%s%s CLOSE '% (deviceid,hostname,hostip,port))
S.close ()
For DeviceID in range (1, hang):
hostname = Table_list.cell (DeviceID, 0). Value
HostIP = Table_list.cell (DeviceID, 1). Value
PORT = Int (Table_list.cell (DeviceID, 2). Value)
Threading. Thread (target=scan,args= (Deviceid,hostname,hostip,port)). Start ()
Time.sleep (0.2) #间隔时间可以自己调
Time.sleep (10)
Input (' Enter Quit: ')
===========================================================
Pre-run preparation work:
1, set the scan target, save as. xlsx execl table, in the following format:
Business Name |
IP Address |
Port number |
Mailbox |
1.1.1.1 |
25 |
Web |
2.2.2.2 |
80 |
Remote Desktop |
3.3.3.3 |
3389 |
2, install Python 3
3. External library required for installation: XLRD (table Read)
Install command: Pip install xlrd
Pro-Test effective, need to scan the host and port in the table to fill the line, applicable to a number of different host and port batch scanning, not applicable to a single IP 1-65535 port scan.
Focus on network operation and maintenance, only send practical dry goods
Scan QR code to pay attention to the public
OPS script: Python for bulk IP port scanning