Bash 小技巧 !!

标题中的’!!’并不是感叹词,而是要提到的主角’!!’

  • ln -s ~/tools    /usr/bin/tools
  • Permission denied

遇到过这种情况?

一般人会…

  • sudo ln -s ~/tools    /usr/bin/tools

更简单的做法: 

  • sudo !!

忘记初始化kerberos也一样 

  • ssh 10.3.x.xx #no permission
  • kinit && !!

=?gb2312?B?zfXMqQ?= 什么意思? — 编码小知识普及

你的邮件有没有收到类似这样发件人发来的邮件?收到的时候是否有崩溃的感觉??

其实很简单,我来告诉你这个什么意思吧,

首先这是一个字符串,并且按照’?’分割成5部分

‘=’ :一个转码过的字符串的开始
‘gb2312’:字符串原来的编码为gb2312
‘B’:现在的编码 ‘B-encoding’,也就是base64的意思
‘zfXMqQ’:真正的值,也就是编码过的字符串
‘=’:结束
怎么解码呢?把’zfXMqQ’这个base64编码的值转成gb2312,你就可以在gb*的系统中看了,如果你的系统是utf-8的,还需要把刚才的结果再转成utf-8。

也就是:

echo “zfXMqQ” | base64 -d | iconv -f gb2312 -t utf-8
如果你手头正好有台*nix,并且安装了base64和iconv这两个工具运行一下吧,是不是有点意思? ^_^

因为我一直用mutt看邮件,最近要写一个脚本,扫描一下所有邮件中的联系人,整理一个通讯录,所以研究了上面的内容。

用kindle 3看漫画

kindle看书的好处简直太多了,轻便易携带,不伤眼,可以调整字体大小,丰富的第三方云推送服务。现在在地铁上已经不习惯看纸制书。

但看漫画有些不方便,因为网上很多漫画都是rar格式,而且两页连在一起,很多漫画还是从右往左看的。

后来发现可以用工具ImageMagick轻松解决这些问题。

convert $i -crop 2×1@ +repage +adjoin $i

写了一个工具

可以输入漫画目录和zip/rar包,
支持左右两种方向阅读的漫画,
支持打包成zip
如果你用的是*uix, 并且安装了ImageMagick就可以体验一下了

传送门 (https://github.com/wangtai/Kindle-Comic/blob/master/kindle_comic.bash)

==========

如果放到ipad中可以看彩色的

人人转发浏览器插件

javascript:(function() { var scripts = document.getElementsByTagName(‘script’); var i = 0; for (i; i < scripts.length; i++) { if ('http://widget.renren.com/js/forward.js&#039; == scripts[i].src) { return false; } } var head = document.getElementsByTagName('head')[0]; var config = document.createElement('script'); config.type = 'text/javascript'; config.textContent = 'window.__fwdCfg = {"api_key" : "1822613f8b894453b268eaf733315da0","img_minWidth": 260,"img_minHeight": 260,"text_minCount":15};'; var renren = document.createElement('script'); renren.type = 'text/javascript'; renren.src = "http://widget.renren.com/js/forward.js&quot;; renren.async = "true"; head.appendChild(config); head.appendChild(renren); })()

alias 菜单=’cd’

经常在中文输入法和英文中切换,有时候在使用终端的时候经常出错,例如将 cd打成’菜单’,所以还要删除掉很麻烦。今天在.bashrc中加了这个别名定义,^_^以后不怕打错了

alias 菜单=’cd’

ie 302 post cookie

今天遇到了一个倒霉问题。在ie下302 post set-cookie,cookie丢失。只有在ie会出现这个问题。ff & chrome会兼容掉这个错误,所以没有bug。

问题产生的原因:

The HTTP 1.0 standard per http://www.w3.org/Protocols/rfc2109/rfc2109 in section “4.3.5 Sending Cookies in Unverifiable Transactions” says that cookies should not be set from unverifiable transactions. It states specifically that “Unverifiable transactions typically arise when a user agent automatically requests inlined or embedded entities or when it resolves redirection (3xx) responses from an origin server.” So, “Set-Cookie” is not to be acted upon by browsers / user agents for 3xx redirects. Nothing in HTTP 1.1 changes this part of HTTP.
观察Http Header可以直观的了解到问题,关键部分已经标出:

here is FF header:
POST /app/account/process_login HTTP/1.1
Host: http://www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.example.com/app/account/login
Content-Type: application/x-www-form-urlencoded
Content-Length: 40
back=&username=testuser&password=1234567
HTTP/1.x 302 Found
Server: Apache/1.3.36 (Unix) mod_auth_tkt/2.0.0rc2 mod_perl/1.29
Set-Cookie: auth_tkt=YTg3OWYyZjVlYmQ5ZTllM2Q3NWExNzdmMTRhMjI3ZGI0NzBlYzIzYnRlc3R1c2VyITI%3D; path=/;
Location: /app
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/plain
———————————————————-
http://www.example.com/app
GET /app HTTP/1.1
Host: http://www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.example.com/app/account/login
Cookie: auth_tkt=YTg3OWYyZjVlYmQ5ZTllM2Q3NWExNzdmMTRhMjI3ZGI0NzBlYzIzYnRlc3R1c2VyITI%3D
HTTP/1.x 200 OK
Server: Apache/1.3.36 (Unix) mod_auth_tkt/2.0.0rc2 mod_perl/1.29
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
———————————————————-

here is IE header
POST /app/account/process_login HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
Referer: http://www.example.com/app/account/login
Accept-Language: en-us,en;q=0.5
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
Host: http://www.example.com
Content-Length: 40
Connection: Keep-Alive
Cache-Control: no-cache
back=&username=testuser&password=1234567
HTTP/1.1 302 Found
Server: Apache/1.3.36 (Unix) mod_auth_tkt/2.0.0rc2 mod_perl/1.29
Set-Cookie: auth_tkt=NmY4ZDFiZDZhODgxZTk0ZTkwNDIyZjU5MmRmNGMwY2Q0NzBlYjk1MXRlc3R1c2VyITI%3D; path=/; expires=Fri, 12-Oct-2007 01:01:21 GMT
Location: /app
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/plain
GET /app HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
Referer: http://www.example.com/app/account/login
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
Host: http://www.example.com
Connection: Keep-Alive
Cache-Control: no-cache
HTTP/1.1 200 OK
Date: Fri, 12 Oct 2007 00:01:21 GMT
Server: Apache/1.3.36 (Unix) mod_auth_tkt/2.0.0rc2 mod_perl/1.29
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
啥也没有

简单的压力测试工具 ab

ab Apache HTTP server benchmarking tool,可以用作简单的http压力测试工具,在有些时候在没有运维组帮助的条件下我们可以完成一些压力测试。虽然使用简单但是功能却不简单,应该是很强大。

它的功能包括:

1. 单线程多次请求

2. 并发多次请求

3. 用户认证信息请求

4. 带Cookie信息请求

5. 设置发送数据大小

6. 支持 SSL/TLS 协议

7. 自定义header

8. PUT file

9. 报表

10. csv报表

… …

太多了,强大到所有类型的http协议压力测试都可以实现。感兴趣的同学可以看一下man ab。但使用起来却出奇的简单。 比如:

# 请求http://www.renren.com/home 2000次

ab -n 2000 http://www.renren.com/home

output:

This is ApacheBench, Version 2.3

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking http://www.renren.com (be patient)

Completed 200 requests

Completed 400 requests

Completed 600 requests

Completed 800 requests

Completed 1000 requests

Completed 1200 requests

Completed 1400 requests

Completed 1600 requests

Completed 1800 requests

Completed 2000 requests

Finished 2000 requests

Server Software: nginx/1.1.7

Server Hostname: http://www.renren.com

Server Port: 80

Document Path: /home

Document Length: 134 bytes

Concurrency Level: 1

Time taken for tests: 41.018 seconds

Complete requests: 2000

Failed requests: 0

Write errors: 0

Non-2xx responses: 2000

Total transferred: 724000 bytes

HTML transferred: 268000 bytes

Requests per second: 48.76 [#/sec] (mean)

Time per request: 20.509 [ms] (mean)

Time per request: 20.509 [ms] (mean, across all concurrent requests)

Transfer rate: 17.24 [Kbytes/sec] received

Connection Times (ms)

min mean[+/-sd] median max

Connect: 1 3 67.2 2 3007

Processing: 8 17 71.5 11 2270

Waiting: 8 17 71.5 11 2270

Total: 10 20 98.0 13 3017

Percentage of the requests served within a certain time (ms)

50% 13

66% 13

75% 14

80% 14

90% 15

95% 17

98% 45 #百分之98的请求时间小于45ms ,还不错

99% 195

100% 3017 (longest request)

上面的内容很多,Requests per second: 48.76 [#/sec] (mean) ; Time per request: 20.509 [ms] (mean)这是我们关心的。表示每秒可以请求48次,平均每次请求20ms。以上内容是在我本地做的测试。

当然,还有其他用法如下:

# 20个线程,请求总数2000次

ab -n 2000 -c 20 http://www.renren.com/home

#测试需要cookie的请求

ab -n 2000 -c 20 -C t=ticket http://www.renren.com/home