最近在使用 OpenWrt 的 DDNS 发现,如果使用 write_log
来实现日志输出,中文不会显示出来,会出现格式错误。对 dynamic_dns_functions.sh
的源代码进行细读发现,write_log()
函数会调用 urlencode()
函数,而该函数是不支持中文的。
对该函数改为支持中文的URL编码即可,将 urlencode(){}
改写代码如下:
注:dynamic_dns_functions.sh
文件地址一般为 /usr/lib/ddns/
dynamic_dns_functions.sh1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| urlencode() { local __ENC [ $# -ne 2 ] && write_log 12 "Error calling 'urlencode()' - wrong number of parameters" __ENC=$(echo "$2" awk 'BEGIN { split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ") hextab [0] = 0 for (i=1; i<=255; ++i) { ord [ sprintf ("%c", i) "" ] = i + 0 } } { encoded = "" for (i=1; i<=length($0); ++i) { c = substr ($0, i, 1) if ( c ~ /[a-zA-Z0-9.-]/ ) { encoded = encoded c # safe character } else if ( c == " " ) { encoded = encoded "+" # special handling } else { # unsafe character, encode it as a two-digit hex-number lo = ord [c] % 16 hi = int (ord [c] / 16); encoded = encoded "%" hextab [hi] hextab [lo] } } print encoded }') eval "$1=\"$__ENC\"" return 0 }
|
目前试过了多种方法,还是需要将 urlencode()
函数增加中文支持,直接更改 write_log()
函数还是会报错,应该是ddns某些内容需要进行url编码才能正确读取。