也就是可能会出现A机房的网络subnet:192.168.1.0/24, B 机房的网络subnet:192.168.100.0/24 但是他们属于同一个vlan,要求如果容器在A机房的物理机拉起,分到的是192.168.1.0/24中的IP,B机房的容器分到的IP是:192.168.100.0/24
\{n,m} Matches n to m of the preceding atom, as many as possible \{n} Matches n of the preceding atom \{n,} Matches at least n of the preceding atom, as many as possible \{,m} Matches 0 to m of the preceding atom, as many as possible \{} Matches 0 or more of the preceding atom, as many as possible (like *) */\{-* \{-n,m} matches n to m of the preceding atom, as few as possible \{-n} matches n of the preceding atom \{-n,} matches at least n of the preceding atom, as few as possible \{-,m} matches 0 to m of the preceding atom, as few as possible \{-} matches 0 or more of the preceding atom, as few as possibles
Shell的工作方式,大多数入门用户会觉得枯燥难学,而所谓的经典教材也离不开《Advanced Bash-Scripting》、《Bash Guide for Beginners》,但类似本文这样的一些“雕虫小技”因为难登大雅之堂绝不会收录进去。这情况如果象国外一些unix用户比较多的地方会有很好改善,即使是新手,偶尔看看别人的操作都能“偷师”一手,我编译本系列文章其实也就希望稍微改善一下这个状况。
这也许是最有趣的一条技巧了,David Leadbeater 创建了一个 DNS 服务器,通过它当你查询一个 TXT 记录类型时,会返回一条来自于 Wikipedia 的简短的词条文字,这是他的介绍。 这里有一个样例,来查询 “hacker” 的含义:
1``2``3``4``5``6``7``8
$ **dig** +short txt hacker.wp.dg.cx`` ``"Hacker may refer to: Hacker (computer security), someone involved``in computer security/insecurity, Hacker (programmer subculture), a``programmer subculture originating in the US academia in the 1960s,``which is nowadays mainly notable for the free software/” “open``source movement, Hacker (hobbyist), an enthusiastic home computer``hobbyist http://a.vu/w:Hacker"
这里使用了 dig 命令,这是标准的用来查询 DNS 的系统管理工具,+short 参数是让其仅仅返回文字响应,txt 则是指定查询 TXT 记录类型。 更简单的做法是你可以为这个技巧创建一个函数:
1``2``3``4``5
wiki**()** **{** **dig** +short txt $1.wp.dg.cx; **}**``*#**然后试试吧:*``wiki hacker`` ``"Hacker may refer to: Hacker (computer security), …"
<rmem_alloc> the memory allocated for receiving packet
<rcv_buf> the total memory can be allocated for receiving packet
<wmem_alloc> the memory used for sending packet (which has been sent to layer 3)
<snd_buf> the total memory can be allocated for sending packet
<fwd_alloc> the memory allocated by the socket as cache, but not used for receiving/sending packet yet. If need memory to send/receive packet, the memory in this cache will be used before allocate additional memory.
<wmem_queued> The memory allocated for sending packet (which has not been sent to layer 3)
<ropt_mem> The memory used for storing socket option, e.g., the key for TCP MD5 signature
<back_log> The memory used for the sk backlog queue. On a process context, if the process is receiving packet, and a new packet is received, it will be put into the sk backlog queue, so it can be received by the process immediately
<sock_drop> the number of packets dropped before they are de- multiplexed into the socket
The entire print format of ss -m is given in the source:
如上图,tb指可分配的发送buffer大小,不够还可以动态调整(应用没有写死的话),w[The memory allocated for sending packet (which has not been sent to layer 3)]已经预分配好了的size,t[the memory used for sending packet (which has been sent to layer 3)] , 似乎 w总是等于大于t?
rcv_space is the high water mark of the rate of the local application reading from the receive buffer during any RTT. This is used internally within the kernel to adjust sk_rcvbuf.
# ss -ant dst :80 or src :1723
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 3 *:1723 *:*
TIME-WAIT 0 0 172.31.23.95:37269 111.161.68.235:80
TIME-WAIT 0 0 172.31.23.95:37263 111.161.68.235:80
TIME-WAIT 0 0 172.31.23.95:37267
or:
ss -ant dport = :80 or sport = :1723
地址筛选,目标地址是111.161.68.235的连接
ss -ant dst 111.161.68.235
端口大小筛选,源端口大于1024的端口:
ss sport gt 1024
How Do I Compare Local and/or Remote Port To A Number? Use the following syntax:
## Compares remote port to a number ##
ss dport OP PORT
## Compares local port to a number ##
sport OP PORT
Where OP can be one of the following:
<= or le : Less than or equal to port
>= or ge : Greater than or equal to port
== or eq : Equal to port
!= or ne : Not equal to port
< or gt : Less than to port
> or lt : Greater than to port
Note: le, gt, eq, ne etc. are use in unix shell and are accepted as well.
###################################################################################
### Do not forget to escape special characters when typing them in command line ###
###################################################################################
ss sport = :http
ss dport = :http
ss dport \> :1024
ss sport \> :1024
ss sport \< :32000
ss sport eq :22
ss dport != :22
ss state connected sport = :http
ss \( sport = :http or sport = :https \)
ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24
ss 查看 timer 状态
ss -atonp
按连接状态过滤
Display All Established HTTP Connections
ss -o state established '( dport = :http or sport = :http )'
List all the TCP sockets in state -FIN-WAIT-1 for our httpd to network 202.54.1/24 and look at their timers: ss -o state fin-wait-1 ‘( sport = :http or sport = :https )’ dst 202.54.1/24
Filter Sockets Using TCP States
ss -4 state FILTER-NAME-HERE
Where FILTER-NAME-HERE can be any one of the following,
established
syn-sent
syn-recv
fin-wait-1
fin-wait-2
time-wait
closed
close-wait
last-ack
listen
closing
all : All of the above states
connected : All the states except for listen and closed
synchronized : All the connected states except for syn-sent
bucket : Show states, which are maintained as minisockets, i.e. time-wait and syn-recv.
big : Opposite to bucket state.
Diag列的说明 Indicator Meaning >| The sender window (i.e. the window advertised by the remote endpoint) is 0. No data can be sent to the peer. >|< The receiver window (i.e. the window advertised by the local endpoint) is 0. No data can be received from the peer. > ># There are unacknowledged packets and the last ACK was received more than one second ago. This may be an indication that there are network problems or that the peer crashed.
1. give packets from eth0 a delay of 2ms bash$ tc qdisc add dev eth0 root netem delay 2ms 2.change the delay to 300ms bash$ tc qdisc change dev eth0 root netem delay 3ms
3.display eth0 delay setting bash$ tc qdisc show dev eth0 4.stop the delay bash$ tc qdisc del dev eth0 root #corrupt The following rule corrupts 5% of the packets by introducing single bit error at a random offset in the packet: tc qdisc change dev eth0 root netem corrupt 5%
# 指定 eth0 网卡,来源 ip 是 10.0.1.1,目的端口是 3306 的访问延迟 20ms,上下浮动 2ms tc qdisc add dev eth0 root handle 1: prio bands 4 tc qdisc add dev eth0 parent 1:4 handle 40: netem delay 5ms 30ms tc filter add dev eth0 parent 1: protocol ip prio 4 basic match "cmp(u16 at 2 layer transport eq 80) and cmp(u8 at 16 layer network eq 100) and cmp(u8 at 17 layer network eq 100) and cmp(u8 at 18 layer network eq 146) and cmp(u8 at 19 layer network eq 3)" flowid 1:4
tc filter add dev eth0 parent 1: protocol ip prio 4 basic match "cmp(u8 at 16 layer network eq 100) and cmp(u8 at 17 layer network eq 100) and cmp(u8 at 18 layer network eq 61) and cmp(u8 at 19 layer network eq 88)" flowid 1:4
# 删除过滤 sudo tc filter del dev eth0 parent 1: prio 4 basic sudo tc qdisc del dev eth0 root
0 layer 代表 sport 2 layer 代表 dport
指定端口34001上,延时5ms
1 2 3
tc qdisc add dev eth0 root handle 1: prio tc qdisc add dev eth0 parent 1:3 handle 30: netem delay 5ms tc filter add dev eth0 protocol ip parent 1:0 u32 match ip sport 34001 0xffff flowid 1:3
rate表示令牌的产生速率, sustained maximum rate latency表示数据包在队列中的最长等待时间, packets with higher latency get dropped burst参数表示 maximum allowed burst: burst means the maximum amount of bytes that tokens can be available for instantaneously. 如果数据包的到达速率与令牌的产生速率一致,即200kbit,则数据不会排队,令牌也不会剩余 如果数据包的到达速率小于令牌的产生速率,则令牌会有一定的剩余。 如果后续某一会数据包的到达速率超过了令牌的产生速率,则可以一次性的消耗一定量的令牌。 burst就是用于限制这“一次性”消耗的令牌的数量的,以字节数为单位。
tbf: use the token buffer filter to manipulate traffic rates
限制10MB,排队等待超过100ms就触发丢包,只限制了出去的流量,没有限制进来的流量:
1 2 3 4 5
tc qdisc ls dev eth0 // 查看eth0上的队列规则 sudo tc qdisc add dev eth0 root tbf rate 80mbit burst 1mbit latency 100ms
# 5. 添加过滤规则,将不同 IP 的流量导向不同的类 tc filter add dev eth0 protocol ip parent 1: prio 1 u32 match ip dst 10.0.3.228/32 flowid 1:10 tc filter add dev eth0 protocol ip parent 1: prio 1 u32 match ip dst 10.0.3.229/32 flowid 1:20 tc filter add dev eth0 protocol ip parent 1: prio 1 u32 match ip dst 10.0.3.230/32 flowid 1:30 tc filter add dev eth0 protocol ip parent 1: prio 1 u32 match ip dst 10.0.3.231/32 flowid 1:40
# =================== 查看规则 ====================== tc filter show dev ${DEVICE_NAME} tc class show dev ${DEVICE_NAME} tc qdisc show dev ${DEVICE_NAME}
#====================== 清理 ====================== tc filter delete dev ${DEVICE_NAME} parent 1:0 protocol ip pref 10 tc qdisc del dev ${DEVICE_NAME} parent 1:2 netem tc class del dev ${DEVICE_NAME} parent 1:0 classid 1:2 tc class del dev ${DEVICE_NAME} parent 1:0 classid 1:1 tc qdisc del dev ${DEVICE_NAME} root handle 1
A trivial test module, this module always returns pong' on successful contact. It does not make sense in playbooks, but it is useful from /usr/bin/udp’