Linux迷+Python粉 - 网络https://blog.pythonwood.com/2018-11-27T16:00:00+08:00网络检测ping脚本——搬瓦工CN2机房PING测试2018-11-25T20:00:00+08:002018-11-27T16:00:00+08:00pythonwoodtag:blog.pythonwood.com,2018-11-25:/2018/11/网络检测ping脚本——搬瓦工CN2机房PING测试/<h2 id="vps">项目起源:搬瓦工vps快过期<a class="headerlink" href="#vps" title="Permanent link">¶</a></h2>
<p>为决定是否upgrade到cn2机房,需要先验证<span class="caps">CN2</span>机房网络是不是更好。一直没有一个适合自己的ping工具,自己写一个。</p>
<h3 id="openwrt5ping">部署在openwrt路由,定时任务每5分钟启动ping一次<a class="headerlink" href="#openwrt5ping" title="Permanent link">¶</a></h3>
<p>脚本兼容新旧版本ping(输出内容不同),使用sh标准达到最大linux兼容。已兼容openwrt。</p>
<div class="highlight"><pre><span></span># ping 50次,不超过90s,丢包率每字符表示2%,延迟每字符表示10ms
*/5 …</pre></div><h2 id="vps">项目起源:搬瓦工vps快过期<a class="headerlink" href="#vps" title="Permanent link">¶</a></h2>
<p>为决定是否upgrade到cn2机房,需要先验证<span class="caps">CN2</span>机房网络是不是更好。一直没有一个适合自己的ping工具,自己写一个。</p>
<h3 id="openwrt5ping">部署在openwrt路由,定时任务每5分钟启动ping一次<a class="headerlink" href="#openwrt5ping" title="Permanent link">¶</a></h3>
<p>脚本兼容新旧版本ping(输出内容不同),使用sh标准达到最大linux兼容。已兼容openwrt。</p>
<div class="highlight"><pre><span></span># ping 50次,不超过90s,丢包率每字符表示2%,延迟每字符表示10ms
*/5 * * * * /path/to/20181119_host_ping.sh 23.252.103.101 50 90 2 10 >>/tmp/ping.DC3.txt 2>/dev/null
*/5 * * * * /path/to/20181119_host_ping.sh 65.49.131.102 50 90 2 10 >>/tmp/ping.DC9.txt 2>/dev/null
*/5 * * * * /path/to/20181119_host_ping.sh vps.old 50 90 2 10 >>/tmp/ping.vps.old.txt 2>/dev/null
*/5 * * * * /path/to/20181119_host_ping.sh vps.new 50 90 2 10 >>/tmp/ping.vps.new.txt 2>/dev/null
</pre></div>
<h2 id="host">最终版本的脚本,单host版<a class="headerlink" href="#host" title="Permanent link">¶</a></h2>
<h3 id="_1">脚本使用与输出字段意义<a class="headerlink" href="#_1" title="Permanent link">¶</a></h3>
<div class="highlight"><pre><span></span>$ ./20181119_host_ping.sh
请参考文档使用:
./20181119_host_ping.sh <host<span class="p">|</span><span class="m">163</span>.com> <pkgcn<span class="p">|</span><span class="m">10</span>> <maxtime<span class="p">|</span><span class="m">30</span>> <lost_step<span class="p">|</span><span class="m">2</span>> <rt_step<span class="p">|</span><span class="m">5</span>>
./20181119_host_ping.sh <span class="m">163</span>.com <span class="m">10</span> <span class="m">30</span> <span class="m">1</span> <span class="m">5</span> <span class="o">(</span>测10次ping,不超过30s,丢包率每字符表示1%,延迟每字符表示5ms<span class="o">)</span>
时间 出包 入包 丢包 min avg max <span class="p">|</span>图形化丢包率,最长25字符 <span class="p">|</span>图形化延迟avg,最多50字符
<span class="m">2018</span>-11-20_10:08:55 <span class="m">3</span> <span class="m">3</span> <span class="m">2</span>% <span class="m">166</span> <span class="m">167</span> <span class="m">169</span> <span class="p">|</span>xx <span class="p">|</span>****************
</pre></div>
<h2 id="_2">比较结果<a class="headerlink" href="#_2" title="Permanent link">¶</a></h2>
<p>搬瓦工<span class="caps">CN2</span> <span class="caps">GIA</span>质量非常稳定,土豪随意;对于移动用户访问,<span class="caps">CN2</span>比一般机房提升不大。</p>
<p>一天之中2-10点网络最好,晚上6点至凌晨1点最差</p>
<div class="highlight"><pre><span></span>root@Gargoyle:/tmp# tail ping.DC9.txt ping.DC3.txt ping.vps.old.txt -n 50
==> ping.DC9.txt <==
16:00:01 50 50 0% 163 166 170 | |****************
16:05:01 51 50 1% 163 177 301 | |*****************
16:10:01 50 50 0% 162 166 170 | |****************
16:15:01 50 50 0% 164 166 171 | |****************
16:20:01 50 50 0% 164 166 169 | |****************
16:25:01 50 50 0% 162 166 171 | |****************
16:30:02 50 50 0% 164 166 171 | |****************
16:35:01 50 50 0% 163 167 174 | |****************
16:40:02 50 50 0% 164 167 172 | |****************
16:45:01 50 50 0% 164 168 173 | |****************
16:50:01 50 50 0% 163 166 171 | |****************
16:55:01 50 50 0% 163 166 170 | |****************
17:00:01 51 50 1% 163 166 174 | |****************
17:05:01 50 50 0% 164 167 178 | |****************
17:10:01 50 50 0% 163 166 171 | |****************
17:15:01 50 50 0% 163 166 170 | |****************
17:20:01 50 50 0% 163 166 169 | |****************
17:25:01 50 50 0% 163 166 169 | |****************
17:30:01 50 50 0% 163 166 171 | |****************
17:35:01 50 50 0% 163 166 170 | |****************
17:40:01 50 50 0% 164 167 172 | |****************
17:45:01 50 50 0% 164 167 169 | |****************
17:50:01 50 50 0% 164 166 169 | |****************
17:55:01 50 50 0% 162 166 171 | |****************
18:00:01 50 50 0% 164 166 170 | |****************
18:05:01 50 50 0% 164 167 170 | |****************
18:10:01 50 50 0% 163 165 168 | |****************
18:15:01 50 50 0% 164 166 170 | |****************
18:20:01 50 50 0% 164 166 170 | |****************
18:25:01 50 50 0% 164 166 169 | |****************
18:30:01 50 50 0% 164 166 169 | |****************
18:35:01 50 50 0% 164 166 171 | |****************
18:40:01 50 50 0% 164 167 171 | |****************
18:45:01 50 50 0% 163 166 170 | |****************
18:50:01 50 50 0% 164 166 171 | |****************
18:55:01 50 50 0% 164 166 171 | |****************
19:00:01 50 50 0% 164 167 188 | |****************
19:05:01 50 50 0% 164 168 188 | |****************
19:10:01 50 50 0% 163 166 171 | |****************
19:15:01 50 50 0% 164 169 174 | |****************
19:20:01 50 50 0% 163 168 183 | |****************
19:25:01 50 50 0% 164 167 180 | |****************
19:30:01 50 50 0% 164 168 187 | |****************
19:35:01 50 50 0% 163 169 193 | |****************
19:40:01 50 50 0% 162 168 196 | |****************
19:45:01 50 50 0% 163 166 169 | |****************
19:50:01 50 50 0% 163 167 169 | |****************
19:55:01 50 50 0% 163 166 170 | |****************
20:00:01 50 50 0% 163 167 192 | |****************
20:05:01 50 50 0% 162 166 171 | |****************
==> ping.DC3.txt <==
16:00:01 59 50 15% 234 283 333 |xxxxxxx |****************************
16:05:01 56 50 10% 174 241 318 |xxxxx |************************
16:10:01 52 50 3% 188 246 264 |x |************************
16:15:01 51 50 1% 179 229 255 | |**********************
16:20:02 50 50 0% 189 245 261 | |************************
16:25:01 51 50 1% 188 246 267 | |************************
16:30:02 52 50 3% 187 245 265 |x |************************
16:35:02 50 50 0% 212 249 262 | |************************
16:40:02 52 50 3% 181 249 264 |x |************************
16:45:01 53 50 5% 191 233 261 |xx |***********************
16:50:01 53 50 5% 188 249 263 |xx |************************
16:55:01 50 50 0% 174 241 263 | |************************
17:00:01 50 50 0% 208 248 263 | |************************
17:05:01 50 50 0% 204 242 262 | |************************
17:10:01 54 50 7% 188 216 252 |xxx |*********************
17:15:01 51 50 1% 218 252 263 | |*************************
17:20:01 51 50 1% 205 249 265 | |************************
17:25:01 51 50 1% 196 243 261 | |************************
17:30:01 52 50 3% 197 249 265 |x |************************
17:35:01 52 50 3% 224 249 262 |x |************************
17:40:01 54 50 7% 213 251 266 |xxx |*************************
17:45:01 50 50 0% 221 247 265 | |************************
17:50:01 52 50 3% 206 246 260 |x |************************
17:55:01 53 50 5% 180 241 265 |xx |************************
18:00:01 50 50 0% 207 243 262 | |************************
18:05:01 52 50 3% 198 243 263 |x |************************
18:10:01 52 50 3% 197 252 264 |x |*************************
18:15:01 51 50 1% 220 247 263 | |************************
18:20:01 51 50 1% 205 245 263 | |************************
18:25:01 50 50 0% 178 221 247 | |**********************
18:30:01 51 50 1% 178 237 262 | |***********************
18:35:01 51 50 1% 161 187 261 | |******************
18:40:01 51 50 1% 206 241 261 | |************************
18:45:01 51 50 1% 165 188 209 | |******************
18:50:01 50 50 0% 209 247 264 | |************************
18:55:01 50 50 0% 196 236 261 | |***********************
19:00:01 52 50 3% 167 200 222 |x |********************
19:05:01 52 50 3% 170 200 238 |x |********************
19:10:01 50 50 0% 225 248 263 | |************************
19:15:01 51 50 1% 204 246 263 | |************************
19:20:01 50 50 0% 226 248 263 | |************************
19:25:01 51 50 1% 204 249 264 | |************************
19:30:01 50 50 0% 212 253 265 | |*************************
19:35:01 52 50 3% 206 245 265 |x |************************
19:40:01 50 50 0% 206 241 286 | |************************
19:45:01 50 50 0% 180 236 262 | |***********************
19:50:01 50 50 0% 197 241 264 | |************************
19:55:01 51 50 1% 213 255 265 | |*************************
20:00:01 54 50 7% 204 254 264 |xxx |*************************
20:05:01 53 50 5% 216 250 265 |xx |*************************
==> ping.vps.old.txt <==
16:00:01 67 50 25% 240 285 338 |xxxxxxxxxxxx |****************************
16:05:01 61 50 18% 183 247 325 |xxxxxxxxx |************************
16:10:01 51 50 1% 219 254 270 | |*************************
16:15:02 55 50 9% 182 236 262 |xxxx |***********************
16:20:01 51 50 1% 197 258 524 | |*************************
16:25:01 50 50 0% 193 276 1166 | |***************************
16:30:02 52 50 3% 191 250 269 |x |*************************
16:35:02 51 50 1% 231 256 270 | |*************************
16:40:02 50 50 0% 190 254 269 | |*************************
16:45:01 51 50 1% 194 239 266 | |***********************
16:50:01 50 50 0% 191 256 270 | |*************************
16:55:01 50 50 0% 179 247 266 | |************************
17:00:01 51 50 1% 220 254 269 | |*************************
17:05:01 50 50 0% 211 248 267 | |************************
17:10:01 51 50 1% 194 222 256 | |**********************
17:15:01 51 50 1% 213 258 271 | |*************************
17:20:01 52 50 3% 209 255 268 |x |*************************
17:25:01 50 50 0% 199 248 268 | |************************
17:30:01 50 50 0% 198 255 270 | |*************************
17:35:01 51 50 1% 231 255 270 | |*************************
17:40:01 52 50 3% 220 257 270 |x |*************************
17:45:01 51 50 1% 225 252 267 | |*************************
17:50:01 50 50 0% 208 251 268 | |*************************
17:55:01 51 50 1% 186 248 269 | |************************
18:00:01 51 50 1% 211 248 272 | |************************
18:05:01 52 50 3% 207 249 267 |x |************************
18:10:01 52 50 3% 199 258 269 |x |*************************
18:15:01 50 50 0% 223 252 269 | |*************************
18:20:01 50 50 0% 210 250 267 | |*************************
18:25:01 51 50 1% 184 227 254 | |**********************
18:30:01 50 50 0% 187 244 267 | |************************
18:35:01 51 50 1% 169 194 268 | |*******************
18:40:01 51 50 1% 212 247 266 | |************************
18:45:01 50 50 0% 170 193 213 | |*******************
18:50:01 51 50 1% 216 252 269 | |*************************
18:55:01 50 50 0% 201 242 267 | |************************
19:00:01 50 50 0% 170 206 227 | |********************
19:05:01 51 50 1% 175 208 261 | |********************
19:10:01 50 50 0% 230 253 267 | |*************************
19:15:01 53 50 5% 210 253 268 |xx |*************************
19:20:01 50 50 0% 230 254 267 | |*************************
19:25:01 50 50 0% 211 254 268 | |*************************
19:30:01 50 50 0% 221 259 269 | |*************************
19:35:01 51 50 1% 213 252 269 | |*************************
19:40:01 50 50 0% 220 248 268 | |************************
19:45:01 50 50 0% 187 241 267 | |************************
19:50:01 50 50 0% 202 247 270 | |************************
19:55:01 52 50 3% 216 261 268 |x |**************************
20:00:01 51 50 1% 212 258 269 | |*************************
20:05:01 52 50 3% 228 257 270 |x |*************************
</pre></div>
<h3 id="2018-11-27">2018-11-27后注:<a class="headerlink" href="#2018-11-27" title="Permanent link">¶</a></h3>
<p>作为搬瓦工多年老用户,随着去年以来ssh操作vps卡顿日渐严重,差点就因此粉转黑。 不过对我来说,还是搬瓦工性价比高,操作熟悉。</p>
<p>非常遗憾,错过史上最大促销(<span class="caps">CPU</span>:2 核 内存:2048 <span class="caps">MB</span> 硬盘:40 <span class="caps">GB</span> <span class="caps">SSD</span> 流量:1 <span class="caps">TB</span> <span class="caps">CN2</span>网络)相当于打3折。 25号因为黑五优惠,买了普通cn2机房vps。</p>
<p>让人满意的是,ping结果事实证明,新vps比老vps的网络环境好了小一倍。 老旧vps都安装了nginx,使用http播放mp4视频,新vps体验完胜老vps。 就算去掉部分优化的影响:新vps系统是ubuntu18.04并开启了<span class="caps">BBR</span>拥塞算法。</p>
<p><strong>最终结果:cn2机房vps完胜老vps</strong></p>
<div class="highlight"><pre><span></span>$ tail /tmp/ping.vps* -n <span class="m">40</span> <span class="p">|</span> sed <span class="s1">'s/2018-11-27_//'</span> <span class="p">|</span> sed <span class="s1">'/^16:/d'</span>
<span class="o">==</span>> /tmp/ping.vpsold.txt <<span class="o">==</span>
<span class="m">13</span>:30:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">180</span> <span class="m">221</span> <span class="m">267</span> <span class="p">|</span> <span class="p">|</span>**********************
<span class="m">13</span>:35:01 <span class="m">51</span> <span class="m">50</span> <span class="m">1</span>% <span class="m">216</span> <span class="m">271</span> <span class="m">518</span> <span class="p">|</span> <span class="p">|</span>***************************
<span class="m">13</span>:40:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">222</span> <span class="m">233</span> <span class="m">247</span> <span class="p">|</span> <span class="p">|</span>***********************
<span class="m">13</span>:45:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">195</span> <span class="m">236</span> <span class="m">269</span> <span class="p">|</span> <span class="p">|</span>***********************
<span class="m">13</span>:50:01 <span class="m">55</span> <span class="m">50</span> <span class="m">9</span>% <span class="m">235</span> <span class="m">246</span> <span class="m">265</span> <span class="p">|</span>xxxx <span class="p">|</span>************************
<span class="m">13</span>:55:01 <span class="m">56</span> <span class="m">50</span> <span class="m">10</span>% <span class="m">261</span> <span class="m">281</span> <span class="m">311</span> <span class="p">|</span>xxxxx <span class="p">|</span>****************************
<span class="m">14</span>:00:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">166</span> <span class="m">225</span> <span class="m">248</span> <span class="p">|</span> <span class="p">|</span>**********************
<span class="m">14</span>:05:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">168</span> <span class="m">197</span> <span class="m">230</span> <span class="p">|</span> <span class="p">|</span>*******************
<span class="m">14</span>:10:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">162</span> <span class="m">181</span> <span class="m">217</span> <span class="p">|</span> <span class="p">|</span>******************
<span class="m">14</span>:15:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">196</span> <span class="m">231</span> <span class="m">280</span> <span class="p">|</span> <span class="p">|</span>***********************
<span class="m">14</span>:20:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">212</span> <span class="m">239</span> <span class="m">265</span> <span class="p">|</span> <span class="p">|</span>***********************
<span class="m">14</span>:25:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">195</span> <span class="m">246</span> <span class="m">288</span> <span class="p">|</span> <span class="p">|</span>************************
<span class="m">14</span>:30:02 <span class="m">51</span> <span class="m">50</span> <span class="m">1</span>% <span class="m">192</span> <span class="m">233</span> <span class="m">263</span> <span class="p">|</span> <span class="p">|</span>***********************
<span class="m">14</span>:35:02 <span class="m">52</span> <span class="m">50</span> <span class="m">3</span>% <span class="m">220</span> <span class="m">264</span> <span class="m">292</span> <span class="p">|</span>x <span class="p">|</span>**************************
<span class="m">14</span>:40:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">178</span> <span class="m">211</span> <span class="m">259</span> <span class="p">|</span> <span class="p">|</span>*********************
<span class="m">14</span>:45:02 <span class="m">51</span> <span class="m">50</span> <span class="m">1</span>% <span class="m">172</span> <span class="m">204</span> <span class="m">234</span> <span class="p">|</span> <span class="p">|</span>********************
<span class="m">14</span>:50:01 <span class="m">51</span> <span class="m">50</span> <span class="m">1</span>% <span class="m">245</span> <span class="m">281</span> <span class="m">321</span> <span class="p">|</span> <span class="p">|</span>****************************
<span class="m">14</span>:55:01 <span class="m">53</span> <span class="m">50</span> <span class="m">5</span>% <span class="m">231</span> <span class="m">296</span> <span class="m">337</span> <span class="p">|</span>xx <span class="p">|</span>*****************************
<span class="m">15</span>:00:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">231</span> <span class="m">263</span> <span class="m">290</span> <span class="p">|</span> <span class="p">|</span>**************************
<span class="m">15</span>:05:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">229</span> <span class="m">258</span> <span class="m">282</span> <span class="p">|</span> <span class="p">|</span>*************************
<span class="m">15</span>:10:01 <span class="m">51</span> <span class="m">50</span> <span class="m">1</span>% <span class="m">195</span> <span class="m">241</span> <span class="m">277</span> <span class="p">|</span> <span class="p">|</span>************************
<span class="m">15</span>:15:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">213</span> <span class="m">263</span> <span class="m">320</span> <span class="p">|</span> <span class="p">|</span>**************************
<span class="m">15</span>:20:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">182</span> <span class="m">208</span> <span class="m">225</span> <span class="p">|</span> <span class="p">|</span>********************
<span class="m">15</span>:25:01 <span class="m">51</span> <span class="m">50</span> <span class="m">1</span>% <span class="m">227</span> <span class="m">268</span> <span class="m">313</span> <span class="p">|</span> <span class="p">|</span>**************************
<span class="m">15</span>:30:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">218</span> <span class="m">288</span> <span class="m">340</span> <span class="p">|</span> <span class="p">|</span>****************************
<span class="m">15</span>:35:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">187</span> <span class="m">225</span> <span class="m">339</span> <span class="p">|</span> <span class="p">|</span>**********************
<span class="m">15</span>:40:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">224</span> <span class="m">263</span> <span class="m">297</span> <span class="p">|</span> <span class="p">|</span>**************************
<span class="m">15</span>:45:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">235</span> <span class="m">259</span> <span class="m">288</span> <span class="p">|</span> <span class="p">|</span>*************************
<span class="m">15</span>:50:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">195</span> <span class="m">249</span> <span class="m">274</span> <span class="p">|</span> <span class="p">|</span>************************
<span class="m">15</span>:55:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">220</span> <span class="m">264</span> <span class="m">302</span> <span class="p">|</span> <span class="p">|</span>**************************
<span class="o">==</span>> /tmp/ping.vpsold.txt <<span class="o">==</span>
<span class="m">13</span>:30:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">156</span> <span class="m">159</span> <span class="m">164</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">13</span>:35:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">13</span>:40:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">156</span> <span class="m">160</span> <span class="m">164</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">13</span>:45:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">13</span>:50:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">159</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">13</span>:55:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">155</span> <span class="m">159</span> <span class="m">163</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">14</span>:00:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">14</span>:05:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">164</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">14</span>:10:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">164</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">14</span>:15:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">159</span> <span class="m">163</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">14</span>:20:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">163</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">14</span>:25:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">163</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">14</span>:30:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">156</span> <span class="m">159</span> <span class="m">163</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">14</span>:35:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">14</span>:40:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">159</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">14</span>:45:02 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">159</span> <span class="m">163</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">14</span>:50:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">163</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">14</span>:55:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">164</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">15</span>:00:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">159</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">15</span>:05:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">15</span>:10:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">159</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">15</span>:15:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">156</span> <span class="m">160</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">15</span>:20:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">156</span> <span class="m">159</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">15</span>:25:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">164</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">15</span>:30:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">166</span> <span class="m">192</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">15</span>:35:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">160</span> <span class="m">190</span> <span class="m">201</span> <span class="p">|</span> <span class="p">|</span>*******************
<span class="m">15</span>:40:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">160</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>****************
<span class="m">15</span>:45:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">155</span> <span class="m">159</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">15</span>:50:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">157</span> <span class="m">159</span> <span class="m">162</span> <span class="p">|</span> <span class="p">|</span>***************
<span class="m">15</span>:55:01 <span class="m">50</span> <span class="m">50</span> <span class="m">0</span>% <span class="m">156</span> <span class="m">160</span> <span class="m">176</span> <span class="p">|</span> <span class="p">|</span>****************
</pre></div>ssh命令:隧道代理+本地端口转发+远程端口转发2016-02-15T11:29:00+08:002017-11-27T22:12:00+08:00pythonwoodtag:blog.pythonwood.com,2016-02-15:/2016/02/ssh命令:隧道代理+本地端口转发+远程端口转发/<h3 id="0">0、前言<a class="headerlink" href="#0" title="Permanent link">¶</a></h3>
<p>nc是一个在网络连接两端的好工具,同时也是也个临时的端口转发的好工具。(永久的端口转发用什么?用iptables)</p>
<p>ssh也是这方面的好工具,好处是加密可靠可复用在一端操作即可,代价是要有登录帐号。</p>
<p>我们知道,<span class="caps">SSH</span> 会自动加密和解密所有 <span class="caps">SSH</span> 客户端与服务端之间的网络数据。但是,<span class="caps">SSH</span> 还同时提供了一个非常有用的功能,这就是端口转发。它能够将其他 <span class="caps">TCP …</span></p><h3 id="0">0、前言<a class="headerlink" href="#0" title="Permanent link">¶</a></h3>
<p>nc是一个在网络连接两端的好工具,同时也是也个临时的端口转发的好工具。(永久的端口转发用什么?用iptables)</p>
<p>ssh也是这方面的好工具,好处是加密可靠可复用在一端操作即可,代价是要有登录帐号。</p>
<p>我们知道,<span class="caps">SSH</span> 会自动加密和解密所有 <span class="caps">SSH</span> 客户端与服务端之间的网络数据。但是,<span class="caps">SSH</span> 还同时提供了一个非常有用的功能,这就是端口转发。它能够将其他 <span class="caps">TCP</span> 端口的网络数据通过 <span class="caps">SSH</span> 链接来转发,并且自动提供了相应的加密及解密服务。</p>
<h3 id="1">1、隧道带理<a class="headerlink" href="#1" title="Permanent link">¶</a></h3>
<p>典型应用:翻越高墙</p>
<p>需要条件:一个国外vps,一个不需要登录(安全)的帐号,一个命令。</p>
<p>vps新建帐号:</p>
<div class="highlight"><pre><span></span>useradd -g nobody -s /sbin/nologin gfw && echo gfw_Passw0rd | passwd --stdin gfw
</pre></div>
<p>本地ssh连接:</p>
<div class="highlight"><pre><span></span>ssh -NfD 6666 gfw@vps -p 2222
</pre></div>
<p>可选转换:使用privoxy把socks5代理变成http代理供svn,pip,gem,curl等工具使用。</p>
<h3 id="2">2、本地端口转发<a class="headerlink" href="#2" title="Permanent link">¶</a></h3>
<p><img alt="tu2" src="https://blog.pythonwood.com/uploads/2016/本地端口转发.jpg" title="2"></p>
<p>我们可以将远程机器(LdapClientHost)上的应用直接配置到本机的 7001 端口上(而不是 <span class="caps">LDAP</span> 服务器的 389 端口上)。在 LdapClientHost 上执行如下命令即可建立一个 <span class="caps">SSH</span> 的本地端口转发,例如:</p>
<div class="highlight"><pre><span></span>ssh -L 7001:localhost:389 LdapServerHost
</pre></div>
<p>之后的数据流将会是下面这个样子:</p>
<p>我们在 LdapClientHost 上的应用将数据发送到本机的 7001 端口上,
而本机的 <span class="caps">SSH</span> Client 会将 7001 端口收到的数据加密并转发到 LdapServertHost 的 <span class="caps">SSH</span> Server 上。
<span class="caps">SSH</span> Server 会解密收到的数据并将之转发到监听的 <span class="caps">LDAP</span> 389 端口上,
最后再将从 <span class="caps">LDAP</span> 返回的数据原路返回以完成整个流程。</p>
<h3 id="3">3、远程端口转发<a class="headerlink" href="#3" title="Permanent link">¶</a></h3>
<p><img alt="tu3" src="https://blog.pythonwood.com/uploads/2016/远程端口转发.jpg" title="3"></p>
<p>我们在 LdapClientHost 上的应用将数据发送到本机的 7001 端口上,而本机的 <span class="caps">SSH</span> Server 会将 7001 端口收到的数据加密并转发到 LdapServertHost 的 <span class="caps">SSH</span> Client 上。</p>
<p>在 <span class="caps">LDAP</span> 服务器(LdapServertHost)端执行如下命令:</p>
<div class="highlight"><pre><span></span>ssh -R 7001:localhost:389 LdapClientHost
</pre></div>
<h4 id="_1">区别:<a class="headerlink" href="#_1" title="Permanent link">¶</a></h4>
<p><span class="caps">SSH</span> 端口转发自然需要 <span class="caps">SSH</span> 连接,而 <span class="caps">SSH</span> 连接是有方向的,从 <span class="caps">SSH</span> Client 到 <span class="caps">SSH</span> Server 。而我们的应用也是有方向的,比如需要连接 <span class="caps">LDAP</span> Server 时,<span class="caps">LDAP</span> Server 自然就是 Server 端,我们应用连接的方向也是从应用的 Client 端连接到应用的 Server 端。如果这两个连接的方向一致,那我们就说它是本地转发。而如果两个方向不一致,我们就说它是远程转发。</p>
<h3 id="4autosshsupervisior">4、使用autossh防网络抖动+supervisior进程守护。<a class="headerlink" href="#4autosshsupervisior" title="Permanent link">¶</a></h3>
<p>autossh解决的问题:远程端口转发一旦端口,很难再次建立。</p>
<div class="highlight"><pre><span></span>autossh -M 1932 -NR 1922:localhost:1122 user@vps -p 1122
</pre></div>
<p>ps可以看到实质是这样的:</p>
<div class="highlight"><pre><span></span>ssh -L 1932:127.0.0.1:1932 -R 1932:127.0.0.1:1933 -NR 1922:localhost:1122 -p 1122 user@vps
</pre></div>
<p>分析:使用回路,将本地1933端口,(远程转发)映射到远端1932,(本地转发)到本机的1932。形成回路。</p>
<p>supervisior解决的问题:autossh的进程守护</p>
<p>因此,使用supervisior守护autossh,autossh守护ssh。达到自动启动和守护端口转发的目的。</p>
<h3 id="5">5、综合使用<a class="headerlink" href="#5" title="Permanent link">¶</a></h3>
<p>案例一、如何2222端口被封,如果绕过封死2222端口的防火墙直接ssh到内网机器。(就是说限某几个端口是有局限的)</p>
<p>1、登录最重要的机器把2222端口映射到12222端口:</p>
<div class="highlight"><pre><span></span>ssh -gfNL 12222:0.0.0.0:2222 localhost -p2222
</pre></div>
<p>2、使用该机器做隧道代理访问其他内网机器:</p>
<div class="highlight"><pre><span></span>ssh -NfD 10000 user@host -p12222
</pre></div>
<p>3、ssh绕道访问其他内网机器:</p>
<div class="highlight"><pre><span></span>ssh -o "ProxyCommand=nc -x localhost:10000 %h %p" user@host -p2222
</pre></div>
<p>巧妙结合的ssh和nc,都是linux自带工具,没有依赖。</p>
<p>案例二、借助远程vps让两台不能直接相通的机器相互能访问。</p>
<p>有主机vps和主机A、B。A、B无法直连,通过“中介”搭桥相连。(两台机器都能主动ssh到vps就能完成。)</p>
<p>A要ssh到B(B要ssh到A是同理):</p>
<p>1、主机B用ssh远程转发自己的2222端口到vps的127.0.0.1:12222</p>
<div class="highlight"><pre><span></span>ssh -NfR 12222:127.0.0.1:2222 user@vps -p2222
</pre></div>
<p>2、主机A用ssh本地转发vps的127.0.0.1:12222到本地的127.0.0.1:12222</p>
<div class="highlight"><pre><span></span>ssh -NfL 12222:127.0.0.1:12222 user@vps -p2222
</pre></div>
<p>3、主机A登录主机B</p>
<div class="highlight"><pre><span></span>ssh user@localhost -p12222
</pre></div>
<p>参考:</p>
<p>实战 <span class="caps">SSH</span> 端口转发</p>
<p><a href="https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/">https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/</a></p>
<p>使用supervisor托管shadowsocks</p>
<p><a href="https://blog.phpgao.com/supervisor_shadowsocks.html">https://blog.phpgao.com/supervisor_shadowsocks.html</a></p>
<p><span class="caps">SSH</span>反向连接及Autossh</p>
<p><a href="http://www.cnblogs.com/eshizhan/archive/2012/07/16/2592902.html">http://www.cnblogs.com/eshizhan/archive/2012/07/16/2592902.html</a></p>不能小看的nc——实践TCP协议第四层的软件(传输层)2015-12-20T21:20:00+08:002017-11-30T22:12:00+08:00pythonwoodtag:blog.pythonwood.com,2015-12-20:/2015/12/不能小看的nc——实践TCP协议第四层的软件(传输层)/<p>nc命令全名为netcat,顾名思义就是通过<span class="caps">TCP</span>或<span class="caps">UDP</span>从网络读写数据。</p>
<p>很多事情不一定非得抓包,nc也能发挥巨大作用。</p>
<h3 id="1">1、传输文件<a class="headerlink" href="#1" title="Permanent link">¶</a></h3>
<h4 id="_1">使用<>重定向符(只适用单文件,不推荐、失败时 “>” 产生空文件)<a class="headerlink" href="#_1" title="Permanent link">¶</a></h4>
<div class="highlight"><pre><span></span>nc -l 8888 < demo …</pre></div><p>nc命令全名为netcat,顾名思义就是通过<span class="caps">TCP</span>或<span class="caps">UDP</span>从网络读写数据。</p>
<p>很多事情不一定非得抓包,nc也能发挥巨大作用。</p>
<h3 id="1">1、传输文件<a class="headerlink" href="#1" title="Permanent link">¶</a></h3>
<h4 id="_1">使用<>重定向符(只适用单文件,不推荐、失败时 “>” 产生空文件)<a class="headerlink" href="#_1" title="Permanent link">¶</a></h4>
<div class="highlight"><pre><span></span>nc -l 8888 < demo.txt # 在本机8888端口侦听TCP连接,将收到的数据写入文件
nc ip 8888 > demo.txt # 文件接收端:将文件内容通过网络"cat"到远端
</pre></div>
<h4 id="tar">使用tar传输文件(推荐,好处是保留了原目录结构和权限)<a class="headerlink" href="#tar" title="Permanent link">¶</a></h4>
<div class="highlight"><pre><span></span>tar cz demo1.txt demo_dir/ | nc -l 8888 # 监听8888端口,有连接时开始tar打包并"cat"到远端
nc ip 8888 | tar zx # 连接、接收数据、解压一步到位。
</pre></div>
<h3 id="2">2、建立网络管道<a class="headerlink" href="#2" title="Permanent link">¶</a></h3>
<div class="highlight"><pre><span></span>nc -l 8888 # 接收消息
echo msg | nc ip 8888 # 发送消息
</pre></div>
<h3 id="3">3、迁移生产机房数据到测试机房(运维电脑建管道)<a class="headerlink" href="#3" title="Permanent link">¶</a></h3>
<div class="highlight"><pre><span></span>tar cz demo1.txt demo_dir/ | nc -l 8888 # 生产机房ipA
nc -l 8888 | tar zx # 测试机房ipB
nc ipA 8888 | nc ipB 8888 # 运维个人电脑,连通两台机器的8888端口。
</pre></div>
<h3 id="4-lbgudp">4、测试网络连通 (排查测试机房的lbg转发udp问题)<a class="headerlink" href="#4-lbgudp" title="Permanent link">¶</a></h3>
<div class="highlight"><pre><span></span>curl 调试http,即7层非常高效。但如何调试4层网络呢?答案就是nc
</pre></div>
<p>测试机房lbg做了公网udp服务的映射,但测试同学发现程序出错,怀疑网络问题,找运维同学排查。</p>
<div class="highlight"><pre><span></span>nc -ul 9999 # 服务ipS 监听9999端口 该端口映射到公网 ipVS:portVS
nc -zuv ipS 9999 # 内网udp连通成功 显示Connection to ipS 9999 port [udp/*] succeeded!
nc -zuv ipVS portVS # 公网udp连通失败 显示Connection to ipVS portVS port [udp/*] fail!
一、上如何检查tcp?以上的参数u去掉,默认就是tcp。
二、进一步写出nagios插件,检查udp服务端口(以部署在PP的udp消息推送监控中)
</pre></div>
<h3 id="5telnetmc">5、代替telnet,测试mc,浮云等等。<a class="headerlink" href="#5telnetmc" title="Permanent link">¶</a></h3>
<div class="highlight"><pre><span></span>echo -e "stats\r\n" | nc ipS portMC # 非交换式查看mc的状态。
echo -e "INFO\r\n" | nc ipS portREDIS # 非交换式查看redis的状态。
</pre></div>
<!--非交换的set、get检查浮云ds # 已部署到PP的浮云监控。-->
<h3 id="6nchttpshellman-nc">6、使用nc发邮件,发送http请求,反弹shell,端口转发等等,请man nc<a class="headerlink" href="#6nchttpshellman-nc" title="Permanent link">¶</a></h3>
<div class="highlight"><pre><span></span>echo -e "GET / HTTP/1.0\r\n" | nc uc.cn 80
</pre></div>