<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    so true

    心懷未來,開創未來!
    隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
    數據加載中……

    how to determine the established time of one socket

    You can try the following:
    1. get the PID (say $pid) of the program by adding the -p option to netstat.
    2. identify the proper line in the /proc/net/tcp file by looking at the local_address and/or rem_address fields (note that they are in hex format, specifically the IP address is expressed in little-endian byte order), also make sure that the st is 01 (for ESTABLISHED);
    3. note the associated inode field (say $inode);
    4. search for that inode among the file descriptors in /proc/$pid/fd and finally query the file access time of the symbolic link: find /proc/$pid/fd -lname "socket:\[$inode\]" -printf %t
    function suptime() {
        local addr=${1:?Specify the remote IPv4 address}
        local port=${2:?Specify the remote port number}
        # convert the provided address to hex format
        local hex_addr=$(python -c "import socket, struct; print(hex(struct.unpack('<L', socket.inet_aton('$addr'))[0])[2:10].upper().zfill(8))")
        local hex_port=$(python -c "print(hex($port)[2:].upper().zfill(4))")
        # get the PID of the owner process
        local pid=$(netstat -ntp 2>/dev/null | awk '$6 == "ESTABLISHED" && $5 == "'$addr:$port'"{sub("/.*", "", $7); print $7}')
        [ -z "$pid" ] && { echo 'Address does not match' 2>&1; return 1; }
        # get the inode of the socket
        local inode=$(awk '$4 == "01" && $3 == "'$hex_addr:$hex_port'" {print $10}' /proc/net/tcp)
        [ -z "$inode" ] && { echo 'Cannot lookup the socket' 2>&1; return 1; }
        # query the inode status change time
        local timestamp=$(find /proc/$pid/fd -lname "socket:\[$inode\]" -printf %T@)
        [ -z "$timestamp" ] && { echo 'Cannot fetch the timestamp' 2>&1; return 1; }
        # compute the time difference
        LANG=C printf '%s (%.2fs ago)\n' "$(date -d @$timestamp)" $(bc <<<"$(date +%s.%N) - $timestamp")
    }

    #the function used to convert ip between hex and decimal style
    ip_cvt() {
        local addr=${1:?Please specify <ip:port> or <ip> <port> as the args}
        if [ $# -eq 2 ]; then
            addr=$1:$2
        fi
        local ip=${addr%%:*}
        local port=${addr##*:}
        if [ "$ip" == "${ip##*.}" ]; then
            echo $(python -c "import socket, struct; print(socket.inet_ntoa(struct.pack('<L', int('$ip',16))))"):$(printf "%d\n" "0x"$port)
        else
            echo $(python -c "import socket, struct; print(hex(struct.unpack('<L', socket.inet_aton('$ip'))[0])[2:10].upper().zfill(8))"):$(python -c "print(hex($port)[2:].upper().zfill(4))")
        fi
    }

    $ head /proc/net/tcp
      sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode       
    the meaning of st field is:
      0 /* (Invalid)                */ TCP_CLOSE,
      1 /* TCP_ESTABLISHED  */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
      2 /* TCP_SYN_SENT     */ TCP_CLOSE,
      3 /* TCP_SYN_RECV     */ TCP_FIN_WAIT1 | TCP_ACTION_FIN,
      4 /* TCP_FIN_WAIT1    */ TCP_FIN_WAIT1,
      5 /* TCP_FIN_WAIT2    */ TCP_FIN_WAIT2,
      6 /* TCP_TIME_WAIT    */ TCP_CLOSE,
      7 /* TCP_CLOSE                */ TCP_CLOSE,
      8 /* TCP_CLOSE_WAIT   */ TCP_LAST_ACK  | TCP_ACTION_FIN,
      9 /* TCP_LAST_ACK     */ TCP_LAST_ACK,
      A /* TCP_LISTEN               */ TCP_CLOSE,
      B /* TCP_CLOSING      */ TCP_CLOSING,

    posted on 2016-06-15 11:59 so true 閱讀(249) 評論(0)  編輯  收藏 所屬分類: Linux

    主站蜘蛛池模板: 18禁成年无码免费网站无遮挡| 亚洲精品黄色视频在线观看免费资源 | 99视频有精品视频免费观看| 国产AV无码专区亚洲AV手机麻豆| 美女黄色免费网站| 亚洲成年看片在线观看| 免费无码又爽又黄又刺激网站| 免费a在线观看播放| 黄色网址在线免费观看| 亚洲国产午夜中文字幕精品黄网站| 特黄特色大片免费| 自拍偷自拍亚洲精品情侣| 黄页免费在线观看| 亚洲毛片一级带毛片基地| 国产免费不卡v片在线观看| 亚洲精品无码专区在线播放| 免费a级黄色毛片| 久久久精品视频免费观看| 久久精品国产精品亚洲色婷婷| 16女性下面扒开无遮挡免费| 亚洲成a人片在线看| 天堂亚洲免费视频| 任你躁在线精品免费| 亚洲毛片一级带毛片基地| 国产又大又黑又粗免费视频| 搜日本一区二区三区免费高清视频 | 国产精品亚洲lv粉色| 亚洲av无码天堂一区二区三区| 成人网站免费看黄A站视频| 亚洲经典在线观看| 国产午夜影视大全免费观看| 中文字幕手机在线免费看电影| 亚洲精品亚洲人成在线观看麻豆| 在线播放高清国语自产拍免费 | 国产亚洲成人在线播放va| 91精品免费高清在线| 亚洲av午夜国产精品无码中文字| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 中文字幕免费在线看线人动作大片 | 亚洲综合小说另类图片动图| 亚洲精品国精品久久99热|