How Linux looks at ports
1, Lsof-i: Port number is used to view the occupancy of a port, such as viewing 8000 ports usage, lsof-i:8000
# lsof-i:8000COMMAND PID USER FD TYPE DEVICE size/OFF NODE namelwfs 22065 Root 6u 4395053 0t0 TCP *:irdmi (LISTEN)
You can see that port 8000 has been LWFS by the lightweight file system forwarding service
2, NETSTAT-TUNLP |grep port number, used to view the specified port number of the process, such as View 8000 port, NETSTAT-TUNLP |grep 8000
# netstat-TUNLP Active Internet connections (only servers) Proto recv-q send-q Local address Foreign Address State Pid/Program Name TCP000.0.0.0:1110.0.0.0:* LISTEN4814/Rpcbind TCP000.0.0.0:59080.0.0.0:* LISTEN25492/qemu-KVM TCP000.0.0.0:69960.0.0.0:* LISTEN22065/Lwfs TCP00192.168.122.1:530.0.0.0:* LISTEN38296/DNSMASQ TCP000.0.0.0:220.0.0.0:* LISTEN5278/Sshd TCP00127.0.0.1:6310.0.0.0:* LISTEN5013/CUPSD TCP00127.0.0.1:25 0.0.0.0:* LISTEN 5962/master TCP 0 0 0.0. 0.0:8666 0.0. 0.0:* LISTEN 44868/lwfs TCP 0 0 0.0.0.0:8000 0.0. 0.0:* LISTEN 22065/lwfs
8000TCP 0 0.0. 0.0:0.0.
Explain the meaning of several parameters:
-t (TCP) displays only TCP-related options -U (UDP) shows only UDP-related options -N refuses to display aliases, showing all numbers converted to digital -l only lists service statuses in listen (listening) -p shows the name of the program that established the associated link
Attach a Python port occupancy monitoring program that can monitor whether the port of the specified IP is occupied.
1#!/usr/bin/env Python 2#-*-Coding:utf-8-*-3 4ImportSocket, time, thread 5 socket.setdefaulttimeout (3)#Set default Timeout time 6 7DefSocket_port (IP, port): 8"""9 Enter the IP and port number, scan to determine whether the port occupies 10"" "11Try: 12If Port >=65535: 13Print U‘End of port scan' S=Socket.socket (socket.af_inet, socket. SOCK_STREAM) result=S.CONNECT_EX ((IP, port)) 16If result==0:17Lock.acquire (18)Print Ip,u‘:', Port,u‘Port is occupied' 19Lock.release (20)Except: 21Print U‘Port Scan exception' 22 23DefIp_scan (IP): 24"""25 input IP, scan IP 0-65534 port condition 26"" "27Try: 28Print U‘Start Scan%s‘ %IP start_time=Time.time (30)For IIn range (0,65534): 31Thread.start_new_thread (socket_port, (IP, int (i))) 32Print U‘Scan port complete, total time:%.2f "% (Time.time ()-start_time) 33 # Raw_input ("press Enter to Exit") except: print u ' : Url=raw_input ( ' input the IP want to scan: " ) all Lock=thread.allocate_lock () ip_scan (URL)
The results of the program execution are as follows:
# python scan_port.pyinput the IP want to scan:20.0.208.112 start scanning 20.0.208.11220.0.208.112:111 Port has occupied 20.0. 208.112:22 Port occupied 20.0.208.112:8000 Port occupied 20.0.208.112:15996 Port occupied 20.0.208.112:41734 port occupied scan Port completed, total time : 9.38
Linux View port Usage