BUAAZP.

在shell脚本中实现urlencode

June 13, 2014

在shell脚本中实现urlencode

@招牌疯子

urlencode说白了就是字符串替换,用php实现很easy,用脚本来就相对比较麻烦。先看一下这个脚本:

source $1  
id=$2  
pass=$3  
nonce=0

pa=`echo "$nonce$pass" | openssl rsautl -encrypt -inkey test.pub -pubin |openssl base64`

function encodeurl()  
{
    encoded_str=`echo "$*" | 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
    }' 2>/dev/null`
}

encodeurl $pa  
p=$encoded_str

postdata="id=$id&p=$p"  
curl  $phpurl/index.php/login  -d $postdata  

id和pass是用户名和密码,经过公钥和base64加密后再进行urlencode,用法就是encodeurl $pa然后取函数体中的$encoded_str就行了。

招牌疯子

Coder, OpenSource, DataStorageEngineer. Work@ByteDance
开源爱好者,zimg作者,大规模数据存储工程师。