Linux wishes you to check the port occupancy.
Editor to share with you linux I wish you to check the port occupancy situation, I hope you have learned a lot after reading this article, let's discuss it together!
How does Linux view ports?
Linux to check port occupancy
1. Lsof-I: the port number is used to view the occupancy of a port, for example, to check the usage of port 8000, and lsof-i 8000.
# lsof-i:8000COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMElwfs 22065 root 6u IPv4 4395053 0t0 TCP *: irdmi (LISTEN)
You can see that port 8000 has been occupied by the lightweight file system forwarding service lwfs
2. Netstat-tunlp | grep port number, which is used to view the progress of the specified port number. For example, for port 8000, netstat-tunlp | grep 8000
# netstat-tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0 0 only servers 111 0.0.0 0 LISTEN 4814/rpcbind tcp 0 0 0.0.0 0 0.0.0.0 * LISTEN 25492/qemu-kvm tcp 0 00.0.0.0vel6996 0.0.0.0 LISTEN 22065/lwfs tcp 0 192.168.122.1 Vol 53 0.0.0.0Vol * LISTEN 38296/dnsmasq tcp 0 0 0.0.0.0 LISTEN 5013/cupsd tcp 22 0.0.0.0 LISTEN 5013/cupsd tcp 0 0 127.0.0.1 LISTEN 44868/lwfs tcp 25 0.0.0.0 LISTEN 5962/master tcp 00 0.0.0.015 8666 0.0.0.0 LISTEN 44868/lwfs tcp 00 0.0.0.0000 0.0.0.0 grep 8000tcp * LISTEN 22065/lwfs# netstat-tunlp | grep 8000tcp 00 0.0.0.0 grep 8000tcp 8000 0.0.0.0 LISTEN 22065/lwfs
Explain the meaning of several parameters:
-t (tcp) shows only tcp related options-u (udp) shows only udp related options-n refuses to show aliases Can display all the numbers converted to numbers-l lists only the service status in Listen (monitoring)-p displays the name of the program that establishes the relevant link
Attach a python port occupancy monitoring program, which can monitor whether the port of the specified IP is occupied.
#! / usr/bin/env python#-*-coding:utf-8-*-import socket, time, threadsocket.setdefaulttimeout (3) # set the default timeout def socket_port (ip, port): "" enter IP and port number Scan to determine whether the port is occupied by "" try: if port > = 65535: print u' Port scan ends' s=socket.socket (socket.AF_INET, socket.SOCK_STREAM) result=s.connect_ex ((ip, port)) if result==0: lock.acquire () print ip,u':',port U' Port occupied 'lock.release () except: print u' Port scan exception' def ip_scan (ip): "" enter IP Scan the 0-65534 port of IP "" try: print u' start scanning% s'% ip start_time=time.time () for i in range (0meme 65534): thread.start_new_thread (socket_port, (ip, int (I)) print u' scan port completed Total time:% .2f'% (time.time ()-start_time) # raw_input ("Press Enter to Exit") except: print u' scan ip error'if _ name__=='__main__': url=raw_input ('Input the ip you want to scan:') lock=thread.allocate_lock () ip_scan (url)
The execution result of the program is as follows:
# python scan_port.pyInput the ip you want to scan: 20.0.208.112 start scanning 20.0.208.11220.0.208.112: 111Port 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 scanning port completed, total time: 9.38 finished reading this article I believe you have a certain understanding of the linux to check the port occupancy, want to know more about it, welcome to follow the industry information channel, thank you for your reading!