Shell常用脚本
- Shell
- 2024-03-27
- 697热度
- 0评论
TCP各状态连接数统计
用法如下:
$ sh socket_statistics.sh
==socket
LISTEN=19,CLOSE_WAIT=3,ESTAB=1084,TIME_WAIT=7840
完整代码如下:
#!/bin/bash
export PATH=/home/nagent/libexec/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# ESTAB
# SYN-SENT
# SYN-RECV
# FIN-WAIT-1
# FIN-WAIT-2
# TIME-WAIT
# UNCONN
# CLOSE-WAIT
# LAST-ACK
# LISTEN
# CLOSING
# UNKNOWN
echo ==socket
ss -t -a -n|awk '{print $1}'|grep -v State|awk '{++state[$NF]} END {for(key in state) print key,"=",state[key]}'|tr '\n' ','|sed -e 's/,$//g'|sed -e 's/-/_/g'|sed -e 's/ //g'
echo
获取UDP状态
完整代码如下:
$ cat check_udp_stat.sh
#!/bin/bash
packets_received_last=`grep "packets received" /tmp/udp_stat.tmp|awk '{print $1}'`
packets_to_unknown_port_received_last=`grep "packets to unknown port received" /tmp/udp_stat.tmp|awk '{print $1}'`
packet_receive_errors_last=`grep "packet receive errors" /tmp/udp_stat.tmp|awk '{print $1}'`
packets_sent_last=`grep "packets sent" /tmp/udp_stat.tmp|awk '{print $1}'`
RcvbufErrors_last=`grep "RcvbufErrors" /tmp/udp_stat.tmp|awk -F':' '{print $2}' `
InCsumErrors_last=`grep "InCsumErrors" /tmp/udp_stat.tmp|awk -F':' '{print $NF}'`
InErrors_last=`grep "Udp: " /tmp/udp_stat.tmp |awk '{print $4}'`
SndbufErrors_last=`grep "Udp: " /tmp/udp_stat.tmp |awk '{print $7}'`
netstat -su |grep -w -A 6 Udp >/tmp/udp_stat.tmp
cat /proc/net/snmp | grep -w Udp|grep -v InDatagrams >>/tmp/udp_stat.tmp
packets_received_now=`grep "packets received" /tmp/udp_stat.tmp|awk '{print $1}'`
packets_to_unknown_port_received_now=`grep "packets to unknown port received" /tmp/udp_stat.tmp|awk '{print $1}'`
packet_receive_errors_now=`grep "packet receive errors" /tmp/udp_stat.tmp|awk '{print $1}'`
packets_sent_now=`grep "packets sent" /tmp/udp_stat.tmp|awk '{print $1}'`
RcvbufErrors_now=`grep "RcvbufErrors" /tmp/udp_stat.tmp|awk -F':' '{print $2}' `
InCsumErrors_now=`grep "InCsumErrors" /tmp/udp_stat.tmp|awk -F':' '{print $NF}'`
InErrors_now=`grep "Udp: " /tmp/udp_stat.tmp |awk '{print $4}'`
SndbufErrors_now=`grep "Udp: " /tmp/udp_stat.tmp |awk '{print $7}'`
packets_received=`echo $packets_received_now-$packets_received_last|bc`
packets_to_unknown_port_received=`echo $packets_to_unknown_port_received_now-$packets_to_unknown_port_received_last|bc`
packet_receive_errors=`echo $packet_receive_errors_now-$packet_receive_errors_last|bc`
packets_sent=`echo $packets_sent_now-$packets_sent_last|bc`
RcvbufErrors=`echo $RcvbufErrors_now-$RcvbufErrors_last|bc`
InCsumErrors=`echo $InCsumErrors_now-$InCsumErrors_last|bc`
InErrors=`echo $InErrors_now-$InErrors_last|bc`
SndbufErrors=`echo $SndbufErrors_now-$SndbufErrors_last|bc`
echo "==udp_stat"
echo "packets_received"=$packets_received,"packets_to_unknown_port_received"=$packets_to_unknown_port_received,"packet_receive_errors"=$packet_receive_errors,packets_sent=$packets_sent,RcvbufErrors=$RcvbufErrors,InCsumErrors=$InCsumErrors,InErrors=$InErrors,SndbufErrors=$SndbufErrors
获取Top快照
完整代码如下:
#!/bin/bash
#content=`export COLUMNS=200;top -bcn 1`
content=`export COLUMNS=200;top -bcn 2 -d 1 | perl -00 -ne 'print if $.>2'`
head=`echo -e "$content" | head -n 7 | sed 'H;$!d;g;s/\n/\\\n/g'`
body=`echo -e "$content" | sed '1,7'd | sort -rn -k9 -k10 | head -n 20 | sed 'H;$!d;g;s/\n/\\\n/g'`
echo -n "{\"top_snapshot\":[{\"no\":\"1\",\"content\":\""
echo -n "$head"
echo -n "$body"
echo -n "\"}]}"