May 25, 2011

set time zone on Ubuntu

Modify time zone on Ubuntu 10.04.

$ sudo tzselect

$ sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

$ sudo ntpdate

# disable UTC
$ sudo sed -i 's/^UTC=yes/UTC=no/' /etc/rcS

setup vpn server on Amazon EC2

My environment: Amazon free EC2, Ubuntu 10.04 64bit

You should modify some hard code.

server side

  • $ cat
    # install pptpd
    apt-get install -y pptpd
    # config pptpd
    echo "localip" >> /etc/pptpd.conf
    echo "remoteip" >> /etc/pptpd.conf
    echo "ms-dns" >> /etc/ppp/options.pptpd
    echo "ms-dns" >> /etc/ppp/options.pptpd
    # generate user's password
    passwd=`openssl rand 10 -base64`
    if [ "$1" != "" ]
    then passwd=$1
    # setup authenticated user
    echo "user pptpd $passwd *" >> /etc/ppp/chap-secrets
    # for ipv4 forwarding
    sed -i 's/^#net.ipv4.ip_forward = 1/net.ipv4.ip_forward = 1/' \
    sysctl -p
    # use iptabes to get the net forward
    iptables -t nat -A POSTROUTING -s -j SNAT \
     --to-source $(ifconfig  | grep 'inet addr:'| \
     grep -v '' | cut -d: -f2 \
     | awk 'NR==1 { print $1}')
    iptables -A FORWARD -p tcp --syn -s \
     -j TCPMSS --set-mss 1356
    service iptables save
    # make the iptables and pptpd auto start
    chkconfig iptables on
    chkconfig pptpd on
    service iptables start
    service pptpd start
    echo "VPN service is installed"
    echo "VPN username is user,VPN password is $passwd"

client side

$ cat

apt-get install -y pptp-linux

# modify those variable for your environment

echo "pty \"pptp $vpn_server --nolaunchpppd\"
name $user
remotename pptpd 
file /etc/ppp/options.pptp
ipparam pptpd" > /etc/ppp/peers/pptpd

echo "$user pptpd $passwd *" >> /etc/ppp/chap-secrets

echo "setup vpn client done."
echo -e "now, you shoule run:"
echo -e "\t$ pon pptpd # start pptpd"
echo -e "\t$ pon pptpd debug dump logfd 2 nodetach # debugging"
echo -e "\t$ poff pptpd # stop pptpd"
echo -e "\t$ ifconfig # check network config"

May 20, 2011

awk after the number of field or the value of field changed

# from awk manpage
References to non-existent fields (i.e. fields after $NF) produce the null-string.
However, assigning to a  non-exis‐tent field (e.g., $(NF+2) = 5) increases the value of NF,
creates any intervening fields with the null string as their value, and causes the value
of $0 to be recomputed, with the fields being separated by the value of  OFS.

References to negative numbered fields cause a fatal error.

Decrementing NF causes the values of fields past the new value to be lost,
and the value of $0 to be recomputed, with the fields being separated by the value of OFS.

Assigning a value to an existing field causes the whole record to  be  rebuilt  when  $0  is  referenced.
Similarly, assigning a value to $0 causes the record to be resplit, creating new values for the fields.

1. 引用不存在的字段,会使增加NF的值,$0要根据OFS的值重新创建,中间的字段设置为空字符串, 然后根据FS的值重新对$0进行分割.
$ echo 'a b c' | awk '{print $0; $6=60; OFS=":"; print $0}'
a b c
可以清楚的看到, 第4,5字段的值都是空字符串.

2. 引用编号小于零的字段,会引起错误.
$ cal | awk '{print $-1}'
awk: (FILENAME=- FNR=1) fatal: attempt to access field -1

3. 减少NF的值,编号大于NF的字段将丢失,$0根据OFS的值重新创建,然后根据FS的值重新分割记录.
$ echo 'a b c d e f' | awk '{print $0; NF=3; print $0}'
a b c d e f
a b c

4. 给存在的字段赋值,使$0根据OFS的值重新创建,然后根据FS的值重新分割记录.
$ echo 'a b c' | awk '{print $0; $1=$1;OFS=":"; FS=":"; print $0; print $1}'
a b c

5. 给$0赋值,$0现在是被赋予的新值,$0根据FS的值重新分割记录.
$ echo 'a b c' | awk '{print $0; $0=$0;OFS=":"; FS=":"; print $0; print $1}'
a b c
a b c

echo 'a b c' | awk '{print $0; $0="c d e"; print $0; print $1}'
a b c
c d e

  a. 4, 5是 Shell 编程大赛的第8题.
  b. 可以发现当字段个数或者字段被修改后,都要根据OFS的值进行$0重建, 然后,$0根据FS的值重新分割.
  c. 只修改$0, 使$0用新值根据FS的值重新分割.

May 9, 2011

build Android 2.3, ld cannot find -lz

Build Android 2.3 (gingerbread), report error: /usr/bin/ld: skipping incompatible /usr/lib/ when searching for -lz /usr/bin/ld: skipping incompatible /usr/lib/libz.a when searching for -lz /usr/bin/ld: cannot find -lz collect2: ld returned 1 exit status fix it: $ sudo apt-get install lib32z-dev