Linux Port number Check

Source: Internet
Author: User

  

Today to use Python to write a port detection applet to detect some specific service port is not occupied, suddenly found that they do not know how to query the Linux port is occupied, God, quickly learn.

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  IPv4 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 TCP 0 0 0.0.0.0:111 0.0.0.0:* LI STEN 4814/rpcbind TCP 0 0 0.0.0.0:5908 0.0.0.0:* LISTEN 2549          2/QEMU-KVM TCP 0 0 0.0.0.0:6996 0.0.0.0:* LISTEN 22065/lwfs       TCP 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 38296/dnsmasq TCP 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5278/sshd TCP 0 0 127.0.                0.1:631 0.0.0.0:* LISTEN 5013/cupsd TCP 0 0 127.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 2206         5/lwfs
# NETSTAT-TUNLP | grep 8000tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      22065/lwfs          

Explain the meaning of several parameters:

-T (TCP) displays only TCP-related options-U (UDP) only shows UDP-related options-n rejects display aliases, can display all the digits of the To the number-l list only the service status in listen (listening)-p displays the program name that establishes 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 4 import socket, time, thread 5 socket.setdefaulttimeout (3) #设置默认超 Time 6 7 def socket_port (IP, Port): 8 "" "9 input IP and port number, scan to determine whether the port occupies ten" "" Try:12 if Port > =65535:13 print U ' port scan end ' S=socket.socket (socket.af_inet, socket.             Sock_stream) result=s.connect_ex ((IP, port)) if Result==0:17 Lock.acquire () 18 Print Ip,u ': ', Port,u ' Port occupied ' lock.release () except:21 print U ' port scan exception ', ' Def Ip_scan (IP): 24 "" "25 input IP, scan IP 0-65534 port condition" "" try:28 print U ' Start scan%s '% IP start_tim         E=time.time () for I in Range (0,65534): Thread.start_new_thread (socket_port, (IP, int (i))) 32         Print u ' Scan port complete, total time:%.2f '% (Time.time ()-start_time) # raw_input ("Press Enter to Exit") except:35   Print u ' scan IP error ' if __name__== ' __main__ ': 38  Url=raw_input (' Input the IP want to scan: ') lock=thread.allocate_lock () + ip_scan (URL) 

Linux Port number Check

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.