LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1630|回复: 4

问题挑战:写脚本输出http://proxy.ipcn.org/中的密码

[复制链接]
发表于 2007-8-30 12:49:52 | 显示全部楼层 |阅读模式
用perl或php也可以, SHELL更好.
这是一些内容记录, 需要注意的这里给出的已经剥离了HTML标记, 原来还有些HTML标记如<u>和注释.


  1. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  2. proxy need authorization proxy user:proxy Pass:ipcnproxylist不能用时重起浏览器 密码2小时更换

  3. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  4. auth required 用户名(U) proxy当前密码串 proxyipcn不能用时重起浏览器 不要乱用、滥用 密码2小时更换

  5. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  6. proxy need authorization Username:proxy Password:ipcnsoft不能用时重起浏览器 密码2小时更换

  7. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  8. Authorization Required 代理用户名proxy代理密码串ipcnproxy不能用时restart浏览器 密码2小时更换 如10:02 12:02

  9. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  10. need authorization proxyuser: proxy Password: soft.ipcn不能用时重起浏览器 密码2小时更换 如10:02 12:02


  11. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  12. Authorization Required 代理用户: proxy代理密码: domain不能用时restart浏览器 密码2小时更换 如10:02 12:02

  13. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  14. Authorization Required 代理用户:proxy代理密码:ipcn不能用时restart浏览器 密码2小时更换 如10:02 12:02

  15. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  16. proxy need auth proxy username:proxy proxy pass:searchipcn不好用时restart浏览器 2小时更换password

  17. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  18. auth required 当前用户名 proxy密码 smth.org不可用时重起浏览器 不要乱用、滥用 密码2小时更换

  19. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  20. 需认证 代理用户 proxy代理密码 ipcn不能用时restart浏览器 认证密码2小时更换

  21. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  22. Authorization Required User: proxy Pass: domain.ipcn不能用时restart浏览器 密码2小时更换 如10:02 12:02

  23. 使用自动配置脚本 http://pac.ipcn.org/thu.pac (供清华/教育网用户出国用 教育网出国代理)
  24. need authorization 当前用户名(U):proxy密码(P):ipcn.domain不能用时重起浏览器 密码2小时更换 如10:02 12:02
复制代码
发表于 2007-8-30 14:19:07 | 显示全部楼层
因为输出太不规范,因此不能用脚本简单的处理,因此需要对每一个记录分别进行处理,比较烦
回复 支持 反对

使用道具 举报

发表于 2007-8-30 14:26:12 | 显示全部楼层
我看楼主是来骗代码的,要大家来接受这个所谓的挑战可以,楼主你先抛砖引玉吧。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-8-30 17:30:50 | 显示全部楼层
确实想看看别人的代码, 也不用说骗吧.
我用PHP写了个, 需存为UTF-8编码文件:
  1. #!/usr/bin/php
  2. <?php
  3. function ipcn_get_token(&$str)
  4. {
  5.     $ret[0]=-127;
  6.     $ret[1]="";
  7.     if (strlen($str) == 0)
  8.         return $ret;
  9.     $chr = substr($str, 0, 1);
  10.     $str = substr($str, 1);
  11.     $ichr = ord($chr);
  12.     $gchr = ipcn_get_group($ichr);
  13.     $ret[0]= $gchr;
  14.     $ret[1]= $chr;
  15.     while (strlen($str) && $gchr==ipcn_get_group(ord($str{0}))) {
  16.         $ret[1].= $str{0};
  17.         $str = substr($str, 1);
  18.     }
  19.     return $ret;
  20. }
  21. function ipcn_split_tokens($str)
  22. {
  23.     $ret[0]=array();
  24.     $ret[1]=array();
  25.     while (strlen($str)) {
  26.         $tk = ipcn_get_token($str);
  27.         //echo "{".$tk[0].":".$tk[1]."}";
  28.         array_push($ret[0], $tk[0]);
  29.         array_push($ret[1], $tk[1]);        
  30.     }
  31.     return $ret;
  32. }
  33. function ipcn_get_group($ichr)
  34. {
  35.     if ($ichr >= ord('a') && $ichr <= ord('z')) return 10;
  36.     if ($ichr >= ord('A') && $ichr <= ord('Z')) return 10;
  37.     if ($ichr == ord('.')) return 10;
  38.     if ($ichr > 127) return 20;
  39.     if ($ichr == ord(' ')) return 40;
  40.     if ($ichr == ord('(')) return 100;
  41.     if ($ichr == ord(')')) return 200;
  42.    
  43.     return 10000;
  44. }
  45. function ipcn_getpw($ipcn)
  46. {
  47.     $tka=ipcn_split_tokens($ipcn);
  48.     $tkg=$tka[0];
  49.     $tks=$tka[1];
  50.     $len = count($tkg);
  51.     $state = 0;
  52.     $passwds = array();
  53.     for ($i=0;$i<$len;++$i) {
  54.         $g = $tkg[$i]; $s=$tks[$i];
  55.         //echo "prestate:$state, now str[$g]:{$s}\n";
  56.         if ($state<100 && strpos($s, "出国代理")!==false) {
  57.             $state=100;
  58.             continue;
  59.         }
  60.         if ($state >= 100 && $state < 1000) {
  61.             if (strcasecmp($s, 'user') == 0 ||
  62.                 strcasecmp($s, 'username') == 0||
  63.                 strcasecmp($s, 'proxyuser') == 0 )
  64.                 $state = 1000; // after username;
  65.             elseif (strpos($s, "用户")!==false)
  66.                 $state = 1000;
  67.             continue;
  68.         }
  69.         if ($state == 1000) {
  70.             if ($g == 10 && strcasecmp($s, "proxy") ==0 ) {
  71.                 $state = 2000;// after proxy, now seek for password
  72.             }
  73.             continue;
  74.         }
  75.         if ($state == 2000) {
  76.             if (strcasecmp($s, 'pass') == 0 ||
  77.                 strcasecmp($s, 'password')==0)
  78.                 $state = 3000;
  79.             elseif(strpos($s, "密码") !== false)
  80.                 $state =3000;
  81.             continue;
  82.         }
  83.         if ($state >= 3000 && $state < 4000) {
  84.             if ($g == 100) {
  85.                 $state = 3100;
  86.             } else if ($g == 10) {
  87.                 if ($state == 3100) {
  88.                     if (strlen($s) > 2) {
  89.                         array_push($passwds, $s);
  90.                         $state = 0;
  91.                     }
  92.                 } else {
  93.                     array_push($passwds, $s);
  94.                     //echo "get passwd: $s\n";
  95.                     $state = 0;
  96.                 }
  97.             } else if ($g == 200) {
  98.                 $state = 3200;
  99.             }
  100.             continue;
  101.         }
  102.     }
  103.     return $passwds;
  104. }
  105. function ipcn_textfrmhtml($html)
  106. {
  107.     $start="使用自动配置脚本";
  108.     $stop="IE 菜单 工具(T)";
  109.     $html = strstr($html, $start);
  110.     $html = substr($html, 0, strpos($html, $stop));
  111.     $html = preg_replace(array("/<!--.*?-->/",
  112.                                "/<\\/?(br|p|div)>/",
  113.                                "/<script type=.*?<\\/script>/",
  114.                                "/<[^>]*>/",
  115.                                ),
  116.                          array("","\n", "", "", " "),  $html);
  117.     $fs = "abcdefghijklmnopqrstuvwxyz"
  118.         ."ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  119.     $hs ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  120.     $fa = str_split($fs, 3);
  121.     $ha = str_split($hs);
  122.     $html = str_replace($fa, $ha, $html);
  123.     $html = html_entity_decode($html, ENT_NOQUOTES, "UTF-8");
  124.     $html = str_replace(" ", " ", $html);
  125.     return $html;
  126. }
  127. // first get the html
  128. $ch = curl_init();
  129. curl_setopt($ch, CURLOPT_URL, "http://proxy.ipcn.org/");
  130. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  131. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  132. $ipcnhtml = curl_exec($ch);
  133. if ($ipcnhtml === false) {
  134.     echo curl_error($ch);
  135.     die("\nget html failed!, sorry.\n");
  136. }
  137. curl_close($ch);
  138. $ipcnhtml = iconv("GBK", "UTF-8", $ipcnhtml);
  139. $ipcntext = ipcn_textfrmhtml($ipcnhtml);
  140. $passwds = ipcn_getpw($ipcntext);
  141. if (count($passwds)!=1) {
  142.     print_r($passwds);
  143.     die("sorry, get none or more passwords?\n");
  144. }
  145. $passwd = $passwds[0];
  146. echo "$passwd\n";
  147. ?>
复制代码
回复 支持 反对

使用道具 举报

发表于 2007-9-13 15:30:28 | 显示全部楼层
这是我的 perl 代码,只提取其中的密码。

对于最后一行,没有想到好办法,只好单独设置正则表达式。
哪位有更好的方法,请不吝赐教。
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Encode;
  5. use utf8;
  6. my $usage = "$0 <file>";
  7. my $file = shift or die $usage, "\n";
  8. my $line_id = 0;
  9. open (FH, "<:utf8", $file) or die "%!\n";
  10. while (my $line = <FH>) {
  11.     $line_id++;
  12.     chomp($line);
  13.     if ($line =~ /密码[\w:\s]*?([a-zA-Z.]+)/) {
  14.         print $line_id, "\t==>\t",$1, "\n";
  15.     }
  16.     elsif ($line =~ /[pP]ass\w*:\s*([a-zA-Z.]+)/) {
  17.         print $line_id, "\t==>\t",$1, "\n";
  18.     }
  19.     elsif ($line =~ /\(P\):([a-zA-Z.]+)/) {
  20.         print $line_id, "\t==>\t",$1, "\n";
  21.     }
  22.     else {
  23.         next;
  24.     }
  25. }
  26. close(FH);
复制代码
输出结果
  1. 2       ==>     ipcnproxylist
  2. 5       ==>     proxyipcn
  3. 8       ==>     ipcnsoft
  4. 11      ==>     ipcnproxy
  5. 14      ==>     soft.ipcn
  6. 18      ==>     domain
  7. 21      ==>     ipcn
  8. 24      ==>     searchipcn
  9. 27      ==>     smth.org
  10. 30      ==>     ipcn
  11. 33      ==>     domain.ipcn
  12. 36      ==>     ipcn.domain
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表