Archive for the ‘linux’ Category

随机密码生成器

Wednesday, January 6th, 2010

花了一天时间用Bash写了个生成随机密码的脚本。生成的密码符合以下条件:

  1. 长度为8~20个字符;
  2. 包含至少一个大写字母;
  3. 包含至少一个小写字母;
  4. 包含至少一个数字;
  5. 包含至少一个特殊字符。

总的来说,我还是比较喜欢这个脚本的,够简单也够简洁。

PW_LEN=$((8 + (RANDOM % 12)))
DIGITS='0 1 2 3 4 5 6 7 8 9'
LCHARS='a b c d e f g h i j k l m n o p q r s t u v w x y z'
UCHARS='A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'
SCHARS='! @ # $ % ^ & ` ( ) { } [ ] ; : " , . < > ? / \ | ~'

DISPTB=(DIGITS LCHARS UCHARS SCHARS)
DISPTB_LEN=${#DISPTB[*]}

# randomly select one character from randomly selected char table
function random_select()
{
    local table=${DISPTB[$((RANDOM % DISPTB_LEN))]}
    local n_ele=
    local clist=

    eval clist=(\$$table)   # clist contains char table
    n_ele=${#clist[*]}      # length of array `clist'

    echo -n ${clist[$((RANDOM % n_ele))]}
}

function pwgen()
{
    local passwd=

    for ((cnt=0; cnt<PW_LEN; cnt++)); do
        passwd=$passwd random_select
    done

    echo $passwd
}

# check whether given password conforms to policy
function is_valid()
{
    local pw_len=$(expr length "$1")
    local nr_lower=$(echo "$1" | fold -w1 | grep -c '[[:lower:]]')
    local nr_upper=$(echo "$1" | fold -w1 | grep -c '[[:upper:]]')
    local nr_digit=$(echo "$1" | fold -w1 | grep -c '[[:digit:]]')
    local nr_schar=$((pw_len - nr_lower - nr_upper - nr_digit))

    [ $nr_lower -eq 0 -o $nr_upper -eq 0 -o \
      $nr_digit -eq 0 -o $nr_schar -eq 0 ] && return 1

    return 0
}

while true; do
    pw=$(pwgen); if is_valid "$pw"; then
      echo "$pw"; exit 0
    fi
done

Ubuntu - The Killing App of Linux Platform

Monday, July 20th, 2009

第一次正式接触的Linux发行版是RedHat 7.1,2002年。后来用的比较长时间的是RedHat 7.3,因为只有在那个版本我才能使我台式机的内猫正常工作。后来用奖学金买了RedHat 7.3/8.0,价格都是RMB 68,但基本上都是给同学安装了。实习后买了本本,开始装的SuSE,而放在老家的台式机上跑的是Debian 3.0。后来转向ubuntu,从5.10至今,一直apt-get dist-upgrade到Jaunty 9.04,基本没有出现任何问题。

当年TA问我为什么用ubuntu,是不是因为它polished,我说不是,只是用着比较方便、顺手 — 我不想回到手工配置一切,到处找驱动的时代。实际上,过去的一年多我的本本里面就跑了个wmii,怎么看怎么原始,但是方便、快捷,还自我感觉有点酷。

前几天开机发现fsck,不放心,于是重新格式化硬盘,重装了系统。试了ArchLinux, Debian,最后还是用了ubuntu。装完之后才发现原来如今的ubuntu看上去已经如此养眼。有句话是这么说的:You can’t sell a platform without a killing app on it.  Linux内核无论做的多么牛B,如果没有杀手级应用,那么也仅仅寥胜于无吧。用ubuntu作桌面系统无疑是让人快乐的,我又从wmii回到Gnome桌面。

valgrind against glib

Thursday, July 2nd, 2009

上午同事说valgrind在扫描基于glib的程序时有些明显的内存泄漏没有报过出来。有点不相信,因为之前每次怀疑valgrind的时候最终总是证明是自己的代码写的不对。于是,将信将疑之下用valgrind扫了一下下面的程序:

GList* list = NULL;
GList* i = NULL;

list = g_list_append(list, g_strdup("first"));
list = g_list_append(list, g_strdup("second"));

for(i = list; i != NULL; i = g_list_next(i)){
    printf("%s\n", (char *)i->data);
}

令人难以置信的是,其结果居然是:
==6509== LEAK SUMMARY:
==6509== definitely lost: 0 bytes in 0 blocks.
==6509== possibly lost: 0 bytes in 0 blocks.
==6509== still reachable: 4,569 bytes in 10 blocks.
==6509== suppressed: 0 bytes in 0 blocks.

两次g_strdup()出来的内存显然没有释放,而且链表本身占有的内存也没有释放,但valgrind的definitely/possibly lost居然都是0!如果把上面的printf()函数换成glib里面的g_print()函数,结果又有所变化:
==6519== LEAK SUMMARY:
==6519== definitely lost: 0 bytes in 0 blocks.
==6519== possibly lost: 744 bytes in 3 blocks.
==6519== still reachable: 5,407 bytes in 18 blocks.
==6519== suppressed: 0 bytes in 0 blocks.

这次不但多了744字节的possibly lost,还多出了838字节的still reachable。Google了一把,发现这个问题似乎和glib的slice allocator有关系。参考这里。如果想让valgrind展现出正确的行为,请将环境变量G_SLICE设为always-malloc。


# G_SLICE=always-malloc valgrind --leak-check=yes ./foo
...
==6522== LEAK SUMMARY:
==6522== definitely lost: 12 bytes in 1 blocks.
==6522== indirectly lost: 25 bytes in 3 blocks.
==6522== possibly lost: 0 bytes in 0 blocks.
==6522== still reachable: 1,638 bytes in 9 blocks.
==6522== suppressed: 0 bytes in 0 blocks.

X11 forwarding in Arch

Wednesday, July 1st, 2009

ArchLinux里sshd的X11 forwarding默认似乎没有打开。嗯,那就修改一下/etc/ssh/sshd_config吧。
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes

iwconfig

Monday, June 1st, 2009

这几天网络常常莫名奇妙的会断掉,考虑到LP仍然能正常上网,这应该不是AP的问题。有点怀疑自己本本的无线网卡是不是大限将至。重载ipw2200后再重启网络,似乎一切又恢复正常。只是过段时间后,复又如是,莫名其妙。

刚突然想起,搬到新家后周围有个无线网络的essid和咱的一样,都是”TP-LINK” — 懒人遇上懒人了。怀着“死马当活马医”的心理,把:
iwconfig eth1 essid "TP-LINK" key s:******

改成:
iwconfig eth1 ap 00:1D:0F:XX:YY:ZZ key s:******

暂时似乎一切OK.