Today to use Python to write a port exploration of the small program to detect some specific service port is not occupied, suddenly found out from the fact that you do not know how to query the Linux port is occupied, God, please study quickly.
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 TC P *: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:* listen 4814/rpcbind tcp 0 0 0.0.0.0:5908 0.0.0.0:* listen 25492/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 22065/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) Show only TCP-related options -u (UDP) shows only UDP-related options -n reject display aliases to show all numbers converted to digital -l list only the service statuses in Listen (listening) -p Show the program name to establish the related link
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import socket, time, thread 5 socket.setdefaulttimeout (3) #设置默认超时时间 6 7 def socket_port (ip, port): 8 "" 9 input IP and port number, scan to determine if port is occupied 10 "" " 11 try: 12 if port >=65535: 13 print u ' port scan end ' 14 s= Socket.socket (Socket.af_inet, socket. Sock_stream) 15 result=s.connect_ex ((Ip, port)) 16 if result==0: 17&nbsP; lock.acquire () 18 print ip,u ': ', Port,u ' port is occupied ' 19 lock.release () 20 except: 21 print u ' Port Scan Exception ' 22 23 def ip_scan (IP): 24 "" " 25 input IP, scan IP 0-65534 port condition 26 "" 27 try: 28 print u ' Start scanning %s ' % ip 29 start_time=time.time () 30 for i in range (0,65534): 31 &nbSp; thread.start_new_thread (Socket_port, (Ip, int (i))) 32 print u ' Scan port complete, total time:%.2f ' % (Time.time ()-start_time) 33 # raw_input ("Press enter to exit") 34 except: 35 print u ' Scan IP error ' 36 37 if __name__== ' __main__ ': 38 Url=raw_input (' input the ip you want to scan: ') 39 lock=thread.allocate_lock () 40 ip_scan (URL)
The results of the implementation of the procedure 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 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 ended, altogether time: 9.38
Original link: http://www.magedu.com/71838.html
This article is from the "Marco Linux Training" blog, so be sure to keep this source http://mageedu.blog.51cto.com/4265610/1929081
How to query for port usage in Linux