我是shell脚本控

最近两个月都沉迷于shell脚本中,这从博客更新就看得出来。

开始是延时播入vpn的脚本,它解决了开机时网络不通的情况下播入vpn的问题。理论上用/etc/network/interfaces配置文件也应可以完成,但我设置了if-up还是有问题,所以就转而考虑shell脚本。

第二个shell脚本是用于中文内码转换的。平时下载得到txt文件大多是gbk编码的,但使用统一的utf8必将更加方便。iconv可以完成这个工作,但它每次只能对一个文件进行处理,显然增加了我的工作量。于是我利用脚本,对通配符进行循环并配合上tellenc对输入文件的内码进行猜测。现在的问题是tellenc的结果不一定很准确,这是只根据前10行进行猜测造成的。可能我会在这方面加入更多硬编码的干预,或者写一个更强大的tellenc来准确的得到编码。

然后就是Google离开中国时写下的一个监控工具。也就是不断的通过域名解析,ping,首页分析,反向dns等方法来观察gfw什么时候,用什么方式对Google下手。这个脚本经过几次强化已经比较完善,稍微修改一下就可以对其他的站点进行同样的监控。

还有一个脚本是用于满足我下列需要:在Apache的.htaccess文件中添加和更新若干Allow From XXX.XXX.XXX.XXX,其中XXX.XXX.XXX.XXX应该是指定的动态域名的ip。在写这个脚本的过程中,我学习到了sed的复杂用法。需要面对的是Apache配置文件的注释风格——只有行首的#被认为是注释的开始。因此我需要在每一个Allow From的上一行加上一个动态域名的md5用于识别,sed将替换掉这个md5的下一行,sed -e “/$md5/N” -e “/$md5/c# $domain : $md5\nAllow From $ip”

还有一个小作品和这个主机分享计划的发起者Michael(bemike.org)有那么一点关系。和他在msn上的聊天中我猜测他有那么一个工具能够截屏并上传到图片分享网站。这很是让人羡慕Mac软件的优秀。于是我写了一个shell脚本来完成同样的事情:scrot用来截取屏幕,yfrog的python api将图片上传,notify-send将url在屏幕上显示,xclip用于将url复制到剪切板。最后,我还用Compiz把PrtScn,Alt+PrtScn都绑定到了这个脚本上。

最终章用于满足我下载的需求——登录到路由器上检查是否有其他电脑接入了网络。然后根据这一点来控制打开和关闭amuled。既不影响上网网速,又最大程度的利用网络,如果路由器能够对流量就进行统计就更好了。

小礼物:bash tips,如果有疑问的话请请先看这个视频

预告:shell脚本暂时告一段落,下面Python脚本和Go语言即将登台演出。

on February 1st, 2010 | No Comments »

Google is leaving… Here’s a shell script to monitor that.

Google正要离开中国。

计划今天晚上去来福士16楼为Google献上一束鲜花。

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

Flowers for Google

此外,还特意写了一个bash脚本来记录下这一切。以一个程序员的方法来纪念。

#!/bin/bash
export PATH="/bin:/usr/bin"

LOG=~/google.log
TMP=/tmp/google.index.html
FMT="+%Y-%m-%d %H:%M:%S"
HOST=www.g.cn
NS=202.96.209.5

while [ 1 ]
do
  date "$FMT" | tee -a $LOG
  ips=`dig @$NS $HOST | sed -n -r 's/^.*IN\s+A\s+(.*)$/\1/p'`
  if [ -n "$ips" ]
  then
    for ip in $ips
    do
      wget -O $TMP $ip > /dev/null 2> /dev/null
      google=`cat $TMP | sed -n -r "s/^.*Google.*$/OK/p" \
        | sed -n -r '1p'`
      if [ -n "$google" ]
      then
        echo "$ip OK" | tee -a $LOG
      else
        echo "$ip ERROR" | tee -a $LOG
      fi
    done
  else
    echo "Can't resolve the host: $HOST" | tee -a $LOG
  fi
  sleep 2
done

Updated on 15th Jan 2010:
升级了一下脚本,使得: 1.通过反向解析检测Google是否遭到域名劫持; 2. 支持同时检测多个Google旗下的域名.

#!/bin/bash
export PATH="/bin:/usr/bin"

LOG=~/google.log
TMP=/tmp/google.index.html
FMT="+%Y-%m-%d %H:%M:%S"
GOOGLE_DOMAINS=("www.g.cn" "www.google.cn" "www.google.com")
NS=202.96.209.5

while [ 1 ]
do
  date "$FMT" | tee -a $LOG
  for domain in ${GOOGLE_DOMAINS[@]}
  do
    echo $domain | tee -a $LOG
    ips=`dig @$NS $domain \
      | sed -n -r 's/^.*IN\s+A\s+(.*)$/\1/p'`
    if [ -n "$ips" ]
    then
      for ip in $ips
      do
        server=`nslookup -q=ptr $ip \
          | sed -n -r 's/^.*name = (.*)$/\1/p'`
        is_server_1e100=`echo $server \
          | sed -n -r 's/^.+\.1e100\.net\./OK/p'`
        if [ "$is_server_1e100" = "OK" ]
        then
          wget -O $TMP $ip > /dev/null 2> /dev/null
          is_google=`cat $TMP \
            | sed -n -r "s/^.*Google.*$/OK/p" | sed -n -r '1p'`
          if [ -n "$is_google" ]
          then
              echo "$ip OK" | tee -a $LOG
          else
              echo "$ip ERROR: NOT GOOGLE PAGE" | tee -a $LOG
          fi
        else
          echo "$ip ERROR: DONAME HIJACK" | tee -a $LOG
        fi
      done
    else
      echo "ERROR: STOP RESOLVE: $domain" | tee -a $LOG
    fi
  done
  echo "" | tee -a $LOG
  sleep 2
done

on January 13th, 2010 | No Comments »

Notice : The music in the above article is offered just for trial, and not published under the general license of this blog.

bash script notes (1)

I tied to study the shell script a few times. But on that time, I didn’t get enough knowledge to deal with it. Now, I’m back.

Situation 1

I want to startup the vpn service when the computer startup. I added pon vpn_name to rc.local. However, this can be failed because when this line execute, the network may not be ready. So I decide to write a script to do this. It will wait until the Internet up. Here’s it.

#!/bin/bash

sleep_time=1
export PATH=$PATH:/usr/bin

while [ 1 ]
do
  ping_success=`ping -c 1 -n vpn.yegong.net | \
    sed -n -r 's/^.* (.) received.*$/\1/p'`
  if [ $ping_success -eq 1 ]
  then
    pon yegong
    exit 0
  fi
  sleep $sleep_time
  sleep_time=$(expr $sleep_time + 1)
done
  1. export PATH is very important.
  2. `ping -c 1 -n vpn.yegong.net | sed -n -r ’s/^.* (.) received.*$/\1/p’`, using ` surround the command to get the output text.
  3. sed -n -r ’s/^.* (.) received.*$/\1/p’, using sed to simplify the output. It’s the regular expression. Very similar with VIM replace syntax, isn’t it?
  4. if [ $ping_success -eq 1 ], [ condition ] means the test program, equivalent to test condition, so man test will show more usage.
  5. sleep_time=$(expr $sleep_time + 1), it seems that every variable in shell is a string, so you can’t simply write x=x+1. Instead of, expr program read a string expression and output the results. Please notice the space in the expression.

Situation 2

For each text files in a directory, convert the file encoding to utf8.

There’s a simple util can guess the file encoding. It can be found at Wu Yongwei’s Programming Page (Can’t access when I write the article). Then I just need a script to combine the tellenc and iconv.

TMP_FILE="/tmp/utf8lize.output"
ENCODING="utf-8"

if [ $# == 0 ]
then
  echo "Usage: utf8lize FILES"
  exit 1
fi

for f in "$@"
do
  if [ -f "$f" ]
  then
    enc=`tellenc "$f"`
    if [ $enc != $ENCODING ] && [ $enc != "binary" ]
    then
      echo $f : $enc
      cp "$f" "$f.bak"
      iconv -f "$enc" -t "$ENCODING" -o "$TMP_FILE" "$f"
      cp "$TMP_FILE" "$f"
      rm -f "$TMP_FILE"
    fi
  fi
done
  1. $# results the number of arguments when it been executed.
  2. for f in “$@” is the for-each loop. And “$@” indicates the all program arguments. In addition, when you run the program utf8lize *, * will convert to filenames array. Using the ” to surrounding $@ can deal the filename with space.

on December 15th, 2009 | No Comments »

Reset the layout/views setting of eclipse

It’s the second time to get such a bug of eclipse.

It seems that eclipse can’t work fine with the newer/older version of XulRunner other than 1.9.* in many distribution of Linux. Ubuntu is in the list.

The result is that I can’t open the javadoc view of eclipse. Everytime when I click the tab, a error dialog will pop. Then I switch to other tab and go on coding. But this time I’m not so lucky. I just close the eclipse. It got crazy then. Eclipse can’t startup any more. Eclipse can’t startup because it want to restore the last state. But it failed to open the javadoc view, and then break.

Too many article about this problem from Google. The keyword can be found at %workspace%/.metadata/.log.

SWTError, XPCOM

Some solutions looks good, and proved by the follow comments. However, none of them works on my machine.

Surely, I can find a way for me. I just need to reset the layout of eclipse. Then Google tell us it’s controlled by this file, %workspace%/.metadata/.plugins/org.eclipse.ui.workbench/workbench.xml. I just delete it and then everything works fine now.

In addition, I also remove the javadoc view to prevent the accident happen. This can’t be done from eclipse UI because XulRunner problem. However, you can do it in the config file. Open the workbench.xml, and delete this one

<view id=”org.eclipse.jdt.ui.JavadocView”/>

on December 1st, 2009 | No Comments »

VIM tips (2)

  1. 利用”ayy复制后,可使用”Ayy继续复制,新的内容会添加到寄存器a的尾部。
  2. 除了利用*来匹配当前光标下的单词外,还可以用表示向后查找的#。此外还有g*和g#,区别在于*,#要求严格匹配一个单词。
  3. 启动参数:
    vim + filename,vim打开后自动把光标移动到文末
    vim +/pattern filename,vim打开后自动查找pattern
  4. 多缓冲区编辑:
    利用:n和:N在缓冲区间移动
    利用:e filename来新开一个缓冲区
    利用:e#回到上一个缓冲区,其中#表示上一个被编辑的缓冲区,类似的%表示当前缓冲区
  5. 外部命令:
    :! command,执行外部命令
    :!!,执行最近一次执行的外部命令
  6. 录制与回放:
    当执行了:命令后,命令会自动存入:缓冲区,可以用@:回放
  7. :r !command与!!command有类似的作用,区别在于前者是插入而后者是替换

on November 13th, 2009 | 8 Comments »

Ctrl Tips

Control键对于国人来说主要就是Ctrl+C和Ctrl+V。

今天要说的是Ctrl+Backspace, Ctrl+Delete, Ctrl+Left, Ctrl+Right。

其实很简单,就是在四个键原有的功能上加上的“按单词…”的限定。例如:Ctrl+Backspace,从光标所在处开始向前删除一个单词。

  1. 这在写英文时比较有用
  2. 写代码时也很方便
  3. 中文词组没有空格,因此其效果变为按标点符号进行删除和移动。也许有时会有用,一旦误操作要及时Ctrl+Z喔
  4. 大多数的文本编辑器都支持这一操作,以及Firefox, Internet Explorer, eclipse等,你甚至可以在Windows Explorer的地址栏这样做。Notepad支持除Ctrl+Backspace以外的三个组合键。
  5. 对于Vim,需要自行定义inoremap <C-BS>    <C-W>在.vimrc中

PS. 今天中午发现,我键盘上磨损的顺序是Space > F > IAJ > K;N > LEUON。这是Vim的效果么?

on November 9th, 2009 | 1 Comment »

悼念那八年

昨晚,和胖娃聊到他和现任女友的事情,把本来我基本隔天就在想起的那些事又过了一遍。胖娃1点睡了之后,我老是睡不着,一睡不着就开始胡思乱想。这十几天来,psp内播放得最多的就是“对不起谢谢”“goodbye to romance””don’t look back in angers”。前天从猩猩宿舍出来本来说晚上去看陈绮贞的演唱会,但是一方面走嘿累了,一方面确实不想和同事去听容易挑起情绪的歌。每次听到“你到过下雪的北京”就莫名的伤感。

2001冬天开始,2004年带着一种得意的心情去了成都,2009年不晓得朗阁的就拉爆了。其实我晓得,小的矛盾因为我到了北京就放得无穷大了,09年春节的经历真的让我好痛苦。8年就这样过了给。也没得愤怒,就是带着遗憾就过了。一直在想,关于自己,8年做了好多错事,做了好多能做好没做好的事,想起都让个人包眼睛水。胖娃昨天说以后我老婆比他老婆幸福,我能理解人。其实胖娃只是把好处放大了,却没看到我对ex做的那些哈事。

昨晚把签名档改了之后,今早ex来问我,聊了几句之后突然好想哭,30分钟前就跑到厕所隔间里面偷偷的抹眼睛水。我把ex从一个相信爱情的小妮子变成了一个不相信永远的女人,给人家画了好大个美丽的前景然后又自己撕了。经常想起大学四年P事没做,如果我能留在重庆又是另外的境遇,如果我能学点能养活自己的东西而不是在毕业后才开始想学习可能又是另外的境遇。所以现在只想好好对家人朋友也是自己该遭。无聊之余有asn的比赛看也算是个慰籍。

昨天和胖娃说的两件事情我是真的在冷个想,一是有认真的考虑去当老师,小学老师,中学老师都行;一是如果最好的兄弟们都结婚了,快的话30岁之前送完所有人应该没问题吧,我想自己去个新的地方。

呵呵,刚才聊完,ex说以后就叫我慧能了,我突然觉得好好笑。又哭又笑,黄狗撒尿。

end

on November 9th, 2009 | 2 Comments »

Repair the network infrastructure of the rental house

Today, I fixed the cables of my house.

The main reason that drives me to do is the weak signal of the wireless network.

I have a Netgear WGT624 v4, which is sending a very bad signal.1 One week ago, it’s about -50dB when I check it from the iwconfig. But yesterday, it’s just -76dB.

It’s true that there’re some antennae can work with the bad signal. But most of them just can gain 10dB. So this can’t be a final solution. Then I checked the preserved cables of house. It’s nice that they all roads to the SCTR weak current box. However, the cables is not connected, with no sign, and even have no RJ45 connector. It took me about two hours to pair the cables and make connectors for them.

Quite a heavy work. I learnt the order of the subcables from it. In Chinese, it’s 白橙 橙, 白绿 蓝, 白蓝 绿, 白棕 棕.

Actually, you can’t make connectors with the customized order. Because the subcables are in twisted pair, not just simple lines. So the electronic signal will interfere with each other if you got a wrong order.

Notes,

1. I want to but don’t know how to setup a DD-WRT or OpenWrt on it. Many appreciation if someone can help.

PS,

I’ve made a very nice configuration of compiz and terminator. It’s amazing to key down the shell commands/vim on the beautiful desktop background. Maybe I will try the awesome next month.

chars and words is now floating before the cloud.

chars and words is now floating before the cloud.

on November 4th, 2009 | 3 Comments »

Experience of ubuntu karmic and the fix of eclipse

Thanks, Arsenek, for your nice prologue.

I just upgraded the ubuntu to 9.10 rc for the machine in office this week.

It’s cool.

The new input method is easy to use.You just need to apt-get purge ibus and reinstall them. I think the package in the cd image is out-of-date.

Also, I installed a new icon theme, Nerdy Lines. Then I found much icons didn’t work correctly, so I fixed most of them. As the license of origin work is cc-by-nc-nd 2.0, so my modification can’t be released.

icon-nerdy-liines

Only got one problem of karmic today, I found that eclipse can’t create the new project / import a existed project / quick search.

Lots guys reported the same issue everywhere. It seems a problem about the gtk library. A easy solution is

export GDK_NATIVE_WINDOWS=true
cd ~/share/eclipse/
./eclipse

Please copy it to a shell script in the PATH to replace the eclipse symbol link.

If you got another problem which is in the eclipse starting up stage — eclipse dies at one small blank popup window. I just can tell it’s caused by the XULRunner. You can uninstall it and get a standalone version of firefox.

Update Oct.31 2009 : The home machine is also updated to the karmic.

on October 30th, 2009 | No Comments »

第一篇

刚看完阿森纳对AZ的欧冠小组比赛,最后一分钟被扳平,还真是让人感叹现实不能完美。原本这场比赛也不会关系到小组出线,更不是什么重要比赛,我说不完美,是因为接下来的那天,即10月22日,是Arsene Wenger的60岁生日。天朝60岁能把全国都折腾一下,可Wenger’s baby却没给他们的boss带来一场胜利,足够遗憾,也足够我感叹贵党真是能集中力量干大事。。。

根据cooper的说法,15号就进这个店了,一直也憋不出来什么,趁着看完比赛,就继续说阿森纳好了。阿森纳官方有推出一个类似访谈的纪录片系列,当然球员会有昔日在球场上驰骋的片段。这个系列叫Legend,我收藏了Arsene Wenger,Herbert Chapman,Dennis Bergkamp,Patrick Vieira,Thierry Henry,当然都是网上下载的。我不知道什么时候能收到一封官方的邮件给我说:arsenek你好,请去购买(下载)Legend——Robert Pires,啊哈哈哈,Pires很赞。

再来介绍下自己的名字,Arsenek,一个很简单的拆字就明了了,Arsene+K,需要说一下K是Keane的K,而且还是Roy Keane的K,不过反正两个成名的Keane,不论Roy还是Robbie都是阿森纳的死敌,这也是个怪事,哈哈。对此我又想到一点,我坚信不疑喜欢上阿森纳就是命运。98年才开始看球的时候是支持意大利的,可是荷兰的精彩演出,让我记住了Bergy,一个球风十分令人愉悦的大叔。98年让我记住的另一支球队就是法国,不过我不喜欢98年的冠军和亚军,一个也不。再来就是初中班队要买球服了,一群人跑到大田湾的市场,兴冲冲的想买Liverpool的金黄色比赛服,可是老板说货不够。于是就换了同样是黄色的阿森纳比赛服,那时候的赞助商还是SEGA。这件球衣一直都留着,虽然是30多块买的盗版,初中完了我妈想扔,高中完了我妈也想扔,到了大学完了我妈懂了,搬家后都帮我好好收着的,嘿嘿嘿。所以说,对于一个信奉Once be…Always be…句式的男人来说,喜欢上阿森纳就是水到渠成的事了。

Btw,2002年让我真心喜欢上爱尔兰这支球队,而不仅仅是因为Keane的原因,一个团队通过合作去和强大的对手竞争总让人感到高兴,至于胜利,见鬼去吧。04年无所谓支持,06、08年支持德国,这是另一个大叔Jens Lehmann的故事。希望接下来的世界杯预选赛附加赛中爱尔兰能搞死法国,让我明年也有个主队,哈哈哈哈。

说到阿森纳,那就要说一下几句和阿森纳有关的话。第一句是Victoria Concordia Crescit,这个等于队训的东西,翻译成英语是Victory though harmory,涛哥估计要说成“胜利来自和谐”,个人认为很好的解释了我对足球的理解。Btw,你可以是C.Ronaldo和Messi那样的超人,也可以是巴萨和曼联那种宇宙队,但是见鬼去吧,哈哈。第二句和第三句都是来自Arsene Wenger的妙语,原文记不住了,翻译过来是“整个生命的成功是我们通过态度引发的幸福巧合”,以及“相互适应的完美,远胜于独秀于世的华丽”;这基也是我现在的人生观了。好了,说完这两句话,这段没有了,下段开始说Wenger。

Arsene Wenger,阿塞温格,一般我和一个阵营的同学聊天,总称之为温父,一个固执、儒雅、温情的完美主义追求者。对,固执排第一位,原因不说了,我懒。可以这样说,Wenger的足球不仅是表面上的美丽和青春,Wenger的足球透露出公平,诠释着团队,更演绎着智慧。Wenger可能永远不会取得和Sir.Fergy一样多的冠军,也有种说法叫“历史只记得成功者”,但是我想说的是:冠军?见鬼去吧,win in arsenal’s style才是最重要的。我觉得我是有信仰的,因为从12、3岁开始整个人生都开始涂上阿森纳的红色,但是说我信仰Arsenal,不如说是信仰Arsene。虽然我只了解Arsene在足球场上的一面,但我能从中感受到他的态度:公平竞争、团队足球、功利面前保持的个性和温情、责任感。法国队报的采访说Wenger只恨一个人,马赛队的前老板,一个用钱收买裁判球员获得冠军的痞子。其实我想说,每个人喜欢的东西总是代表了对现实的情绪。前天cooper让我看了阮一峰的《为何我喜欢编程》(大概是这个题目),里面提到代码的世界是公平的,我想我的态度也大概如此,在球场上,在一定的规矩内公平竞争,如同人生,只有公平再好不过。

隐约记得,当初不是想写这些的。好了,6点了,睡2个小时该上班了。突然发现国庆回来后,上班时打了鸡血的状态没得了。。。岁月催人懒啊

on October 21st, 2009 | 4 Comments »