Python Challenge 之 BASH 解法

环境:windows7 cygwin

 

第零关

网址: http://www.pythonchallenge.com/pc/def/0.html

解法: 首先我看到了"Hint: try to change the URL address.",然后进入到了http://www.pythonchallenge.com/pc/def/1.html得到提示"2**38 is much much larger. ",得知这个数字应该是上页图片上显示的2的38次方而不是1.

$ bc <<< 2^38
274877906944


第一关

网址: http://www.pythonchallenge.com/pc/def/274877906944.html

解法: 从标题的明显提示"What about making trans?",结合图片里的三组转换得到这个凯撒密码的转换规则,被转换的显然是下面的几行红字,转换得到提示后知道最终应该转换map这个单词.

$ echo "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj." | tr "[a-z]" "[c-zab]"
i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.
$ echo map | tr "[a-z]" "[c-zab]"
ocr


第二关

网址: http://www.pythonchallenge.com/pc/def/ocr.html

解法: 由页面上的提示"MAYBE they are in the page source.",进入源代码,注释"find rare characters in the mess below:"告诉我们,这关的任务就是从这些标点符号中提取仅存的几个字母出来,我没有把这些乱码另存为文件,而是复制到剪切板里,使用getclip命令直接获取,这样更方便点.

$ getclip | grep -o "[a-z]" | tr -d "\n"
equality


第三关

网址: http://www.pythonchallenge.com/pc/def/equality.html

解法: 由页面上的提示"两边各被三个大写字母包围的小写字母"以及图片中形象的比喻,得知这关又是一个提取操作,grep最适合不过了.同样复制源代码中的注释到剪切板.

$ getclip | grep -oP '(?<=[a-z][A-Z]{3})[a-z](?=[A-Z]{3}[a-z])' | tr -d "\n"
linkedlist


第四关

网址: http://www.pythonchallenge.com/pc/def/linkedlist.html

解法: 跳到linkedlist.php,点击图片链接进入,发现这是一个不少于400次(源代码注释)的链接跳跃游戏.wget和curl都可以胜任这样任务,这里使用纯awk脚本.

#!/usr/bin/awk -f
BEGIN{
	num=12345;
	while(num=m[!!match(($0=getHtml("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="num)),"is ([0-9]+)",m)]){
	}
	num/=2;
	while(num=m[!!match(($0=getHtml("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="num)),"is ([0-9]+)",m)]){
	}
	print $NF;
}
function getHtml(url){
	host=gensub("^http://|/.*","","g",url);
	path=gensub("^http://[^/]*/?","/","g",url);
	socket="/inet/tcp/0/"host"/80";
	print "GET "path" HTTP/1.0\r\nHost: "host"\r\n\r" |& socket;
	html="";
	while((socket |& getline) > 0){
			html=html$0"\n";
	}
	close(socket);
	return html;
}
posted @ 2012-02-24 13:10  紫云飞  阅读(1322)  评论(1编辑  收藏  举报