zoukankan      html  css  js  c++  java
  • linux下列出所有连接到你的Server的IP地址

    linux下列出所有连接到你的Server的IP地址

    最近要做一个检查所有连接到主机的IP的脚本,google到一篇老外写的文章

    《List all IP addresses connected to your Server》

    试了一下脚本结果好像可以。因为对linux了解很少,所以对这脚本各种参数和命令不太明白,正好这篇文章对脚本的描述很详细,正好一边翻译一边理解了。

    下列是一个Unix命令,用来列出所有连接到主机80端口的IP地址。

    netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head
    

    输出 - 全部按IP连接数从高到低排列

    97 114.198.236.100
    56 67.166.157.194
    44 170.248.43.76
    38 141.0.9.20
    37 49.248.0.2
    37 153.100.131.12
    31 223.62.169.73
    30 65.248.100.253
    29 203.112.82.128
    29 182.19.66.187
    

    注:
    This command is useful to detect if your server is under attack, and null route those IPs. Read this null route attacker IP story.

    下面来把这个有点长的命令打散:

    1. netstat -tn 2>/dev/null

    用 netstat 列出所有网络连接,包括进和出。

    1. -n -只显示数字(IP地址和端口),不解析成名字。
    2. -t -只显示TCP连接。

    输出

    #Examples - 7 connections
    tcp        0      0 64.91.*.*:80            114.198.236.100:12763       TIME_WAIT
    tcp        0      0 64.91.*.*:80            175.136.226.244:51950       TIME_WAIT
    tcp        0      0 64.91.*.*:80            175.136.226.244:51951       TIME_WAIT
    tcp        0      0 64.91.*.*:23            202.127.210.2:14517         TIME_WAIT
    tcp        0      0 64.91.*.*:80            149.238.193.121:65268       TIME_WAIT
    tcp        0      0 64.91.*.*:80            114.198.236.100:44088       ESTABLISHED
    tcp        0      0 64.91.*.*:80            175.136.226.244:51952       TIME_WAIT
    

    2>/dev/null
    重定位所有不必要的输出流到/dev/null——一个专门用来获取并清空所有输出流的地方。(译注:2是标准错误输出,这段命令用来隐藏所有错误信息。)

    2. grep :80

    只显示连接到主机80端口的IP地址。

    tcp        0      0 64.91.*.*:80            114.198.236.100:12763       TIME_WAIT
    tcp        0      0 64.91.*.*:80            175.136.226.244:51950       TIME_WAIT
    tcp        0      0 64.91.*.*:80            175.136.226.244:51951       TIME_WAIT
    tcp        0      0 64.91.*.*:80            149.238.193.121:65268       TIME_WAIT
    tcp        0      0 64.91.*.*:80            114.198.236.100:44088       ESTABLISHED
    tcp        0      0 64.91.*.*:80            175.136.226.244:51952       TIME_WAIT
    

    3. awk ‘{print $5}’

    使用 awk 来只显示第5列的内容。

    114.198.236.100:12763
    175.136.226.244:51950
    175.136.226.244:51951
    149.238.193.121:65268
    114.198.236.100:44088
    175.136.226.244:51952
    

    4. cut -d: -f1

    使用 cut 来抽取内容。

    1. -d – 在-d选项后面紧挨着的文字将作为分隔符,默认是tab。
    2. -f – 指定显示用分隔符进行分割后的列
    114.198.236.100
    175.136.226.244
    175.136.226.244
    149.238.193.121
    114.198.236.100
    175.136.226.244
    

    5. sort | uniq -c | sort -nr

    对列表进行排序,分组然后再次倒序排序。

    sort

    114.198.236.100
    114.198.236.100
    149.238.193.121
    175.136.226.244
    175.136.226.244
    175.136.226.244
    

    uniq -c – 分组。

    2 114.198.236.100
    1 149.238.193.121
    3 175.136.226.244
    

    sort -nr – 按数字倒序排列(大数在前)。

    3 175.136.226.244
    2 114.198.236.100
    1 149.238.193.121
    

    完成。

    6. head

    这是个可选项,用来显示前10个结果。

  • 相关阅读:
    将Nginx添加到windows服务中
    springboot使用redis管理session
    GIT常用命令
    阻止360、谷歌浏览器表单自动填充
    谈谈对Spring IOC的理解
    同一个Nginx服务器同一端口配置多个代理服务
    LeetCode 653. Two Sum IV
    109. Convert Sorted List to Binary Search Tree(根据有序链表构造平衡的二叉查找树)
    108. Convert Sorted Array to Binary Search Tree(从有序数组中构造平衡的BST)
    LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树求两点LCA)
  • 原文地址:https://www.cnblogs.com/movingcity/p/6428515.html
Copyright © 2011-2022 走看看