Linux如何查看端口

1、lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000

# lsof -i:8000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
lwfs 22065 root 6u IPv4 4395053 0t0 TCP *:irdmi (LISTEN)

可以看到8000端口已经被轻量级文件系统转发服务lwfs占用

2、netstat -tunlp |grep 端口号,用于查看指定的端口号的进程情况,如查看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: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 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 22065/lwfs

说明一下几个参数的含义:

 -t (tcp) 仅显示tcp相关选项
                                 -u (udp)仅显示udp相关选项
                                 -n 拒绝显示别名,能显示数字的全部转化为数字
                                 -l 仅列出在Listen(监听)的服务状态
                                 -p 显示建立相关链接的程序名
 

附加一个python端口占用监测的程序,该程序可以监测指定IP的端口是否被占用。

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 输入IP和端口号,扫描判断端口是否占用
10 """
11 try:
12 if port >=65535:
13 print u'端口扫描结束'
14 s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
15 result=s.connect_ex((ip, port))
16 if result==0:
17 lock.acquire()
18 print ip,u':',port,u'端口已占用'
19 lock.release()
20 except:
21 print u'端口扫描异常'
22
23 def ip_scan(ip):
24 """
25 输入IP,扫描IP的0-65534端口情况
26 """
27 try:
28 print u'开始扫描 %s' % ip
29 start_time=time.time()
30 for i in range(0,65534):
31 thread.start_new_thread(socket_port,(ip, int(i)))
32 print u'扫描端口完成,总共用时:%.2f' %(time.time()-start_time)
33 # raw_input("Press Enter to Exit")
34 except:
35 print u'扫描ip出错'
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)

该程序执行结果如下:

# python scan_port.py
Input the ip you want to scan: 20.0.208.112
开始扫描 20.0.208.112
20.0.208.112 : 111 端口已占用
20.0.208.112 : 22 端口已占用
20.0.208.112 : 8000 端口已占用
20.0.208.112 : 15996 端口已占用
20.0.208.112 : 41734 端口已占用
扫描端口完成,总共用时:9.38

https://www.cnblogs.com/的更多相关文章

  1. 访问路径:https://i.cnblogs.com/posts?categoryid=925678

    https://i.cnblogs.com/posts?categoryid=925678

  2. URL https://i.cnblogs.com/EditPosts.aspx?opt=1

    URL url = new URL("https://i.cnblogs.com");URL url1 = new URL(url, "EditPosts.aspx?op ...

  3. 随笔二-https://www.cnblogs.com/shang1680/p/9657994.html

    作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...

  4. 211806189杨昊辰 https://www.cnblogs.com/honey1433223/

    211806189杨昊辰 https://www.cnblogs.com/honey1433223/

  5. https://www.cnblogs.com/h2zZhou/p/5440271.html

    https://www.cnblogs.com/h2zZhou/p/5440271.html

  6. https://www.cnblogs.com/soundcode/p/4174410.html

    https://www.cnblogs.com/soundcode/p/4174410.html 1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. ...

  7. https://www.cnblogs.com/yudanqu/p/9467803.html

    https://www.cnblogs.com/yudanqu/p/9467803.html

  8. 转发自:一像素 十大经典排序算法(动图演示)原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删

    原链接:https://www.cnblogs.com/onepixel/articles/7674659.html     个人收藏所用   侵删 0.算法概述 0.1 算法分类 十种常见排序算法可 ...

  9. @无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 通篇引用

    无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 关于Async与Await的FAQ 关于Async与Await的 ...

  10. 211806385 黄存慧 https://www.cnblogs.com/huangch/ https://github.com/HuangCh327

    211806385 黄存慧 https://www.cnblogs.com/huangch/ https://github.com/HuangCh327

随机推荐

  1. Luogu P1290 欧几里得的游戏/UVA10368 Euclid's Game

    Luogu P1290 欧几里得的游戏/UVA10368 Euclid's Game 对于博弈论的题目没接触过多少,而这道又是比较经典的SG博弈,所以就只能自己来推关系-- 假设我们有两个数$m,n$ ...

  2. jquery基础知识2

    1.js和jquery对象的转换 js==>jquery对象 $(js对象) jquery==>js jq对象[index] jq对象.get(index) <!DOCTYPE ht ...

  3. 多继承以及MRO顺序

    class A: def test(self): print("A --- test方法") def demo(self): print("A --- demo方法&qu ...

  4. BZOJ2155(?) R集合 (卡特兰数)

    Orz Freopen大佬 CODE fac = [0]*1005 def C(n, m): return fac[n] // fac[m] // fac[n-m] n = int(input()) ...

  5. How To Install Docker On Ubuntu 18.04

    Docker is an increasingly popular software package that creates a container for application developm ...

  6. learning scala view collection

    The view method will create a non-strict version of the collection which means that the elements of ...

  7. [Luogu] 运输问题 -- 00

    https://www.luogu.org/problemnew/show/4015 #include <bits/stdc++.h> #define gc getchar() using ...

  8. 斐波那契数列 矩阵乘法优化DP

    斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007​\),\(n\le 10^{18}​\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...

  9. infiniband RDMA对比

  10. NodeJS基础知识

    console.log方法: console.log("This is a test string"); node app.js 使用重定向标准输出流: node app.js 1 ...