Blog

  • linux连接数统计查看

    在日常工作中经常要查看连接数,以下是几个例子

    先安装

    yum install net-tools -y

    统计80端口连接数

    netstat -na | grep -i "80" | wc -l

    统计已连接上的,状态为“established’的连接数

    netstat -na | grep ESTABLISHED | wc -l

    ESTABLISHED来源IP访问量前5名

    netstat -na | grep ESTABLISHED | awk '{print$5}' | awk -F : '{print$1}' | sort | uniq -c | sort -k 1 -n -r | head -n 5

    正在SYN来源IP访问量前5名

    netstat -na | grep SYN | awk '{print$5}' | awk -F : '{print$1}' | sort | uniq -c | sort -k 1 -n -r | head -n 5

    将不同的tcp连接状态打印出来

    netstat -n | awk '/^tcp/ {++S[$NF]} END {for(key in S) print key,"\t",S[key]}'

     

    TIME_WAIT 257 等待所有分组死掉
    SYN_SENT 2 应用已经开始,打开一个连接
    FIN_WAIT1 49 应用说它已经完成
    FIN_WAIT2 60 另一边已同意释放
    ESTABLISHED 509 正常数据传输状态/当前并发连接数
    SYN_RECV 5 一个连接请求已经到达,等待确认
    CLOSING 1 无连接是活动的或正在进行
    LAST_ACK 37 等待所有分组死掉

    netstat 查看 unix socket 使用

    netstat -ntlpx

    参考:http://www.letuknowit.com/post/31.html

  • linux write命令给其他用户发消息

    使用w或`last | grep stil`可以查看到哪些用户正在登陆

    11:46:19 up 82 days, 5:58, 2 users, load average: 0.00, 0.01, 0.05
    USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    jpuyy pts/0 192.168.1.2 11:45 3.00s 0.53s 0.00s w
    jpuyy pts/3 192.168.1.3 11:45 19.00s 0.57s 0.57s -bash

    现在在pts/0向pts/3的jpuyy用户写消息

    jpuyy@ip-172-31-11-178:~$ sudo write jpuyy pts/3

    [sudo] password for jpuyy:

    write: warning: write will appear from jpuyy
    foo
    bar

    当发送命令后,对方会收到一条

    Message from yourname@yourhost on yourtty at hh:mm …
    foo
    bar

    方便在没有其他通讯的时候发消息,测试下来不支持中文。

  • windows查看内存

    简要说明

    C:\Users\Administrator>wmic memorychip list brief

    Capacity DeviceLocator MemoryType Name Tag Total Width
    8589934592 DIMM_A1 0 Physical Memory Physical Memory 0 72
    8589934592 DIMM_A2 0 Physical Memory Physical Memory 1 72

    详细说明(可查内存频率)

    C:\Users\Administrator>wmic memorychip list

  • vim折叠

    展开所有折叠

    zi

     

  • zip-unzip命令

    yum install zip unzip

    ubuntu使用unzip解压会出现乱码,加上-O参数就不会有乱码了,后面可接编码号如 gb2312 CP936  GBK GB18030

    unzip -O gb2312 材料.zip

    不解压查看zip文档的内容

    unzip -v file1.zip

     

    解压到指定目录下(如果没有会新建目录)

    unzip terraform_0.12.29_darwin_amd64.zip -d terraform_0.12.29
    

    http://zeuscn.net/archives/2012/12/25/ubuntu-linux-zip-command.html

  • ip2long与long2ip

    在php中可以知道 ip2long 函数将ip转为整型,long2ip将整型转为ip,从而方便计算。

    ip分为四段,将第一段*256^3,第二段*256^2,第三段*256^1,第四段*256^0,最后相加,这样就计算出了一个唯一的值。如果都是最大值,算出来的值是

    >>> 255*256*256*256 + 255*256*256 + 255*256 + 255
    4294967295

    我们可以发现mysql中int型的取值范围是4个字节,十进制为

    0  到  4294967295

    所以所有的ipv4地址用mysql的int恰好可以完整记录

    python中互相转换的方法如下

    import socket 
    import struct
    
    def ip2long(ipstr): 
        return struct.unpack("!I", socket.inet_aton(ipstr))[0]
    
    def long2ip(ip): 
        return socket.inet_ntoa(struct.pack("!I", ip))
    

    http://hily.me/blog/2009/03/python-ip2long-long2ip/