In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use python to write Mini Program to detect linux port occupation, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
Today, I'm going to use python to write a port probe Mini Program to detect whether some specific service ports are occupied. I suddenly find that I don't know how to query for port occupancy in linux.
1. Lsof-I: the port number is used to check the usage of a port, for example, to check the usage of port 8000, lsof-ilsof 800 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, such as port 8000. Netstat-tunlp | grep 800 ordinary netstat-tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 00 0.0.0.0grep 111 0.0.0.0grep * 5908 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 22 0.0.0.0 LISTEN 5013/cupsd * LISTEN 5278/sshd tcp 0 0 127.0.1 Tcp 0 0 127.0.0.1 LISTEN 44868/lwfs tcp 25 0.0.0.0 LISTEN 5962/master tcp 0 0 0.0.0.0 LISTEN 44868/lwfs tcp 0 0 0.0.0.0: 8000 0.0.0.0 grep 8000tcp * LISTEN 22065/lwfs# netstat-tunlp | grep 8000tcp 00 0.0.0.015 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 numbers converted to numbers-l lists only the service status in Listen (monitoring)-p shows the name of the program that established 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 on how to write Mini Program with python to detect linux port occupancy is shared here, I hope the above can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.